SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Using arrays and mysql with complex question?

    Well... I found a couple of errors in my php-code and made some changes to the DB. I finally think I'm on the right track. The question is... I create an array from a query and put it in an array, but how do I get all the parts into the array?

    I'm pulling info from three tables and push that into an array (I think). I then output it after choosing what date I want to show it from.

    Here is the query:
    PHP Code:
    function get_order($datum)
    {
        
    $conn db_connect();
        
    $query "select orders.orderid, orders.datum, orders.lev_namn, orders.lev_adress, 
            orders.lev_postnr, orders.lev_ort, orders.lev_land, bocker.titel, order_bok.antal 
            from orders, order_bok, bocker 
            where orders.orderid = order_bok.orderid 
            and order_bok.bokid = bocker.bokid 
            and orders.datum = '
    $datum'";
        
    $result mysql_query($query);
        if (!
    $result)
            return 
    false;
        
    $num_cats = @mysql_num_rows($result);
       if (
    $num_cats ==0)
          return 
    false;  
       
    $db_result db_result_to_array_row($result); //function specified elsewhere (see below)
            
    return $db_result;

    db_result_to_array_row
    PHP Code:
    function db_result_to_array_row($result)
    {
        
    $res_array = array();
        
        
    $count 0;
        while(
    $row mysql_fetch_row($result))
        {
            
    $res_array[$count++] = $row[0];
        }
            
        return 
    $res_array;

    And the function from the output-file:
    PHP Code:
    function display_order($order_array)
    {
    if (!is_array($order_array))
      {
         echo "<br />Det finns inga order.<br />";
      }
    ?>
    <table>
    <thead>
    <tr><th colspan="8">Order</th></tr></thead>
    <tbody>
    <tr><td>OrderID</td><td>Namn</td><td>Adress</td><td>Postnummer</td><td>Ort</td><td>Land</td><td>Titel</td><td>Antal</td></tr>
    <?php
    foreach ($order_array as $row)
    {
        echo 
    '<tr><td>'.$order_array['orderid'].'</td>
            <td>'
    .$order_array['lev_namn'].'</td>
            <td>'
    .$order_array['lev_adress'].'</td>
            <td>'
    .$order_array['lev_postnr'].'</td>
            <td>'
    .$order_array['lev_ort'].'</td>
            <td>'
    .$order_array['lev_land'].'</td>
            <td>'
    .$order_array['titel'].'</td>
            <td>'
    .$order_array['antal'].'</td></tr>';
    }
    ?>
    <tr><td colspan="8"><?php print_r ($order_array//Check array output for testing ?></tr>
    </tbody>
    </table>
    <?php
    }
    The result from print_r is: Array ( [0] => 2 [1] => 2 [2] => 3 [3] => 3 )
    Nothing gets filled into the table cells.

    So... I'm not using the array properly or creating it properly. I guess I have to make some kind of an assosiative array, but I'm a little confused on how to do it. The array gets populated with only orders.orderid. Can someone please help me figure it out?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  2. #2
    SitePoint Guru
    Join Date
    Aug 2001
    Location
    Amsterdam
    Posts
    788
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A few things are going wrong ...

    1. foreach usage..
    please read the following text and examples ...
    http://www.php.net/manual/en/control...es.foreach.php

    but even bigger problem is the filling of the array ...

    here:
    PHP Code:
    while($row mysql_fetch_row($result)) 

    $res_array[$count++] = $row[0]; 

    return 
    $res_array
    you only fill $res_array[1] is $row[0] this means array[1] = the 1st field of this row..
    What you can do is copy the array or just use $row or fill new array's for each database field one array.. that's up to you ...
    good luck,
    Peanuts
    the neigbours (free) WIFI makes it just a little more fun

  3. #3
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried with this instead and got a whole lot of output from print_r but nothing in my <td>:s. I changed the function-call accordingly.
    PHP Code:
    function db_result_to_array($result)
    {
        
    $res_array = array();
        
        for (
    $count=0
            
    $row = @mysql_fetch_array($result);
            
    $count++
            )
        
    $res_array[$count] = $row;
            
        return 
    $res_array;

    I know my use of arrays are a little confused, but I'm learning it from a book, and using examples together with my own ideas. I knwo the query works fine and gives me what I want when I run it dierctly through SQL, so all I need to do is understand how to put it into an array. I'm not sure about the different alternatives you say I could use... Is the one I just posted one of them?
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...

  4. #4
    Kitty Lizard reptilianfeline's Avatar
    Join Date
    Aug 2002
    Location
    Somewhere in Cyberspace
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK... I'm looking into multi-dimentional arrays, arrays inside arrays. Now I'm even more confused... Why doesn't the examples show how to work with it from an SQL point of view?

    Well I finally figure it out...
    I created a multi array using $res_array = array(array());
    then I built this output:
    PHP Code:
    function display_order($order_array)
    {
    if (!is_array($order_array))
      {
         echo "<br />Det finns inga order.<br />";
      }
    ?>
    <table>
    <thead>
    <tr><th colspan="8">Order</th></tr></thead>
    <tbody>
    <tr><th class="alternate1">OrderID</th><th>Namn</th>
    <th class="alternate1">Adress</th><th>Postnummer</th>
    <th class="alternate1">Ort</th><th>Land</th>
    <th class="alternate1">Titel</th><th>Antal</th></tr>
    <?php
    foreach ($order_array as $row =>$value)
    {
        echo 
    '<tr><td class="alternate1">'.$value['orderid'].'</td>
            <td>'
    .$value['lev_namn'].'</td>
            <td class="alternate1">'
    .$value['lev_adress'].'</td>
            <td>'
    .$value['lev_postnr'].'</td>
            <td class="alternate1">'
    .$value['lev_ort'].'</td>
            <td>'
    .$value['lev_land'].'</td>
            <td class="alternate1">'
    .$value['titel'].'</td>
            <td>'
    .$value['antal'].'</td></tr>';
    }
    ?>
    </tbody>
    </table>
    <?php
    }
    It worked.
    Last edited by reptilianfeline; May 20, 2003 at 07:01.
    Reptilian Feline
    | www.reptilian-feline.net | Art | Tutorials | Pets | Music |
    Webdesign the hard way...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •