Jump to content

php Array to html table

- - - - -

  • Please log in to reply
7 replies to this topic

#1
rushhh

rushhh

    Newbie

  • Members
  • Pip
  • 9 posts
Hi everyone. I'm fetch data from mysql then displaying it in html table.

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" its displaying everything, but in table "5 LBS" its disaplaying 4 names of "2 LBS", how can I get table "5 LBS" to only display what in "5 LBS"?

Here my 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>";
}}


Thanks

Edited by John, 08 March 2009 - 06:27 PM.


#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
Better to use order by size and do an if clause that if size is changed, add a new header for that new size.

You don't have to re-store the mysql data into your own array.
with an order by clause, the code could look like this:
$size = 0;
while ($row1 = mysql_fetch_array($query2) {
    if ($size != $row1['size'] {
        echo "<br>".$row1['size']."<br>";
        $size = $row1['size'];
    }
    echo $row1['name']."<br>";
}

__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
rushhh

rushhh

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks for replying orjan. It almost works, but how would I be able to display it in 2 column rather then 1 column?

#4
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
$size = 0;
$cnt = 0;
while ($row1 = mysql_fetch_array($query2) {
    if ($size != $row1['size'] {
        if (!($cnt % 2)) {  // to do a new line when last item was on first row.
            echo "<br>";
        }
        echo "<br>".$row1['size']."<br>";
        $size = $row1['size'];
        $cnt=0;  // reset, so it keeps in sync
    }
    echo $row1['name'];
    $cnt++;
    if ($cnt % 2) {
        echo "<br>";
    }
}

($cnt % 2) returns 1 if odd, 0 if even.
if you want more than 2 per line, change the number to the number you want.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#5
rushhh

rushhh

    Newbie

  • Members
  • Pip
  • 9 posts
Thanks again Orjan, Its working :) Where would I insert the table code?
echo "<table border='1' width='400'>";
echo "<tr><td>";
Would it be
$size = 0;
$cnt = 0;
while ($row1 = mysql_fetch_array($query2)) {
if ($size != $row1['size']) {
if (!($cnt % 2)) { // to do a new line when last item was on first row.
echo "<br>";
}
echo "<br>".$row1['size']."<br>";
$size = $row1['size'];
$cnt=0; // reset, so it keeps in sync
}
echo "<table border='1' width='400'>";
echo "<tr><td>";
echo $row1['name'];
$cnt++;
if ($cnt % 2) {
echo "</td><td>";
}
echo "</td></tr>";
echo "</table>";
}


#6
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
No, that would create a new table every row.

to use tables, it needs some rewriting.

$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>";


__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#7
rushhh

rushhh

    Newbie

  • Members
  • Pip
  • 9 posts
Lol, ya it looked like a messed up table the way I did it. Thanks again it works :).

#8
mcanous

mcanous

    Newbie

  • Members
  • Pip
  • 2 posts
I have this code to get a xml file and put it into an array, now I want to output it into a table, how can I do this

Thankyou

<META HTTP-EQUIV="REFRESH" CONTENT="20">

<?php

$xmlUrl = "https://secure.ifbyphone.com/ibp_api.php?api_key=6a9212f2af5a0ee41df847fd59eb63acf219404c&action=report.call_detail&start_date=20110517&end_date=20201231&format=csv&date_added=1&phone_label=1&dnis=1&ani=1&call_duration=1&transfer_to_number=1&call_type_filter=All"; // XML feed file/URL

$xmlStr = file_get_contents($xmlUrl);

$filearray = explode("\n", $xmlStr);

while (list($var, $val) = each($filearray)) {

        ++$var;

        $val = trim($val);

        print "Line $var: $val<br />";  



}

Edited by Orjan, 19 May 2011 - 12:35 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users