Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

php Array to html table

array

  • Please log in to reply
7 replies to this topic

#1 rushhh

rushhh

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 08 March 2009 - 05:16 PM

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.

  • 0

#2 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 09 March 2009 - 06:53 AM

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

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#3 rushhh

rushhh

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 March 2009 - 09:30 AM

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

#4 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 09 March 2009 - 09:59 AM


$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.
  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#5 rushhh

rushhh

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 March 2009 - 10:20 AM

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

  • 0

#6 Orjan

Orjan

    CC Mentor

  • Moderator
  • 2918 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#

Posted 09 March 2009 - 10:44 AM

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

  • 0

I'm a System developer at XLENT Consultant Group mainly working with SugarCRM.
Please DO NOT send mail or PM to me with programming questions, post them in the appropriate forum instead, where I and others can answer you.


#7 rushhh

rushhh

    CC Lurker

  • Just Joined
  • Pip
  • 9 posts

Posted 09 March 2009 - 11:16 AM

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

#8 mcanous

mcanous

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts

Posted 18 May 2011 - 06:13 AM

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.

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download