...

View Full Version : php Array to html table



rushhh
03-08-2009, 10:37 PM
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

daemonkin
03-09-2009, 01:17 PM
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:


$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:



<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.

rushhh
03-09-2009, 07:57 PM
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.

rushhh
03-10-2009, 07:45 AM
Hi daemonkin, I got my answer from another fourm.
Here the code if somone wants to uses it in the near future.



$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....:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum