Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php Array to html table

    Hi everyone. I'm fetch data from mysql then displaying it into 2 html tables.

    The data in the mysql is:
    Banana, 2 LBS
    Creme, 2 LBS
    Brownie, 2 LBS
    Batter, 2 LBS
    Blueberry, 2 LBS
    Milk, 2 LBS

    Brownie, 5 LBS
    Batter, 5 LBS
    Blueberry, 5 LBS
    Milk, 5 LBS

    Tring to get it to display as

    2 LBS
    Banana Creme
    Brownie Batter
    Blueberry Milk

    5 LBS
    Brownie Batter
    Blueberry Milk

    Right now in table "2 LBS" it displays everything that in "2 LBS", but in table "5 LBS"
    it disaplays 4 names of "2 LBS" , how can I get table "5 LBS" to only
    display what in "5 LBS"?


    Here my code:
    [CODE]
    $query=mysql_query("select size from $tb4_name WHERE product_id='$uid' Group by size");
    while($order=mysql_fetch_row($query))
    while (list ($key, $val) = each ($order)){

    echo $val; //echo the sizes "2 LBS" and "5 LBS" to make the header of the tables

    $query2=mysql_query("SELECT * FROM $tb4_name WHERE product_id='$uid' and size = '$val' ORDER BY size, name");
    $count = mysql_num_rows($query2);
    while ($row1=mysql_fetch_array($query2)) {
    $array1[] = ($row1['name']);

    echo "<table border='1' width='250'>";
    for($i = 1; $i <= $count; $i = $i + 2)
    {
    echo "<tr><td>{$array1[$i]}</td><td>{$array1[$i+1]}</td></tr>";
    }
    echo "</table>";
    }}

    [CODE]

    Thanks
    Last edited by rushhh; 03-09-2009 at 08:07 AM.

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    N. Ireland
    Posts
    351
    Thanks
    16
    Thanked 4 Times in 4 Posts
    I'm finding it a little hard to visualise your data. Could you post a dump of your sql so that I can try a solution for you? What fields do you have in you db table?

    ::Edited::

    I have built a table with the following fields:
    product_id,size,name

    and written the following:

    PHP Code:
    $sql "SELECT DISTINCT size FROM product";
    $result mysql_query($sql);
    $aSizes = array();
    while(list(
    $size) = mysql_fetch_row($result)){
        
    $aSizes[$size] = array();
    // while

    foreach($aSizes as $key=>$value){
        
    $sql "SELECT product_id,name FROM product WHERE size = '$key'";
        
    $result mysql_query($sql);
        while(list(
    $product_id,$name) = mysql_fetch_row($result)){
            
    $aSizes[$key][$product_id] = $name;
        } 
    // while
    }

    foreach(
    $aSizes as $key=>$value){
    echo 
    '<table>';
    echo 
    '<tr><th colspan="2">'.$key.'</th></tr>';
    $counter=0;
    foreach(
    $value as $key2=>$value2){
        if (
    $counter%2==0) {
            echo 
    '<tr>';
        }
        echo 
    '<td>'.$value2.'</td>';
        if (
    $counter%2==1) {
            echo 
    '</tr>';
        }
        
    $counter++;
    }
    echo 
    '</table>';

    to get this table:

    Code:
    <table><tr><th colspan="2">2 lbs</th></tr><tr><td>banana</td><td>creme</td></tr><tr><td>brownie</td><td>batter</td></tr><tr><td>blueberry</td><td>milk</td></tr></table><table><tr><th colspan="2">5 lbs</th></tr><tr><td>brownie</td><td>batter</td></tr><tr><td>blueberry</td><td>milk</td></tr></table>
    And displays:

    2 lbs
    banana creme
    brownie batter
    blueberry milk
    5 lbs
    brownie batter
    blueberry milk


    Regards,
    D.
    Last edited by daemonkin; 03-09-2009 at 03:05 PM.
    Daemonkin.
    If this was helpful, please add to my reputation
    Thousand Sons - Freelance Web Developer - ninetyonedegrees.com

  • #3
    New Coder
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about that, I should of post more info
    I'm building a store for a frined of mine that supplements, so there different company names and sizes.

    In my database:

    product_id product_ name name size
    16 Cytosport Banana Creme 2.48 LBS
    16 Cytosport Brownie Batter 2.48 LBS
    16 Cytosport Blueberry 2.48 LBS
    16 Cytosport Cake Batter 2.48 LBS
    16 Cytosport Banana 5.0 LBS
    16 Cytosport Chocolate 5.0 LBS
    16 Cytosport Caramel Pecan 5.0 LBS

    Cytosport "company NAME but I gave it an id 16, so product_id='$uid' is equal to 16
    Banana Creme "falour name" $name
    2.48 LBS & 5.0 LBS are "size" $size
    Don't worrie about product_ name.

  • #4
    New Coder
    Join Date
    Sep 2008
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi daemonkin, I got my answer from another fourm.
    Here the code if somone wants to uses it in the near future.

    Code:
    $query=mysql_query("select size from $tb4_name WHERE product_id='$uid' Group by size");
    		while($order=mysql_fetch_row($query)){
    			while (list ($key, $val) = each ($order)){
    			echo "<b>".$val."</b>";
    			
    			$query2=mysql_query("SELECT * FROM $tb4_name WHERE product_id='$uid' and size = '$val' ORDER BY size, fname");
    			$count = mysql_num_rows($query2);
    $size = 0;
    $cnt = 0;
    echo "<table border='1' width='400'>";
    while ($row1 = mysql_fetch_array($query2)) {
        if ($size != $row1['size']) {
            if (!$cnt % 2) { // if cnt is odd, end row as it was end of group
            echo "</tr>";
        }
            echo "<tr><th colspan=2".$row1['size']."</th></tr>";
            $size = $row1['size'];
            $cnt=0; // reset, so it keeps in sync
        }
        if (!$cnt % 2) {  // if cnt is even, start a new row
            echo "<tr>";
        }
        echo "<td>".$row1['name']."</td>";
        if ($cnt % 2) { // if cnt is odd, end row
            echo "</tr>";
        }
        $cnt++;
    }
    if (!$cnt % 2) { // if cnt is odd, end row as it was end of group
        echo "</tr>";
    }
    echo "</table>"; 
    }}
    Thanks for your help daemonkin....


  •  

    Posting Permissions

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