foreach help

So i have a mysql data-base that holds the data that i need, consists of the id, location, date, time and price.

I have a script that will generate a table for me with all the correct cells and headers.

now i am a bit stuck, i can pulll the data out the mysql data base, but i cant get them to display correctly.

if this sums it up, for every id it needs to make a new row and display the correct information from that id,

here is what i got:

[php]
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect server “);
mysql_select_db(”$db_name”)or die(“cannot select DB”);

$result = mysql_query(“SELECT * FROM $tbl_name”);

while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {

$tbl = new HTML_Table(null, ‘display’, 1, 0, 4);

$tbl->addRow();
$tbl->addCell(‘Location’, ‘first’, ‘header’);
$tbl->addCell(‘Price’, null, ‘header’);
$tbl->addCell(‘Date’, null, ‘header’);
$tbl->addCell(‘Time’, null, ‘header’);

foreach($row as $newdata) { 
    list($row["location"], $row["date"], $row["time"], $row["price"] ) = $newdata; 
    $tbl->addRow(); 
        $tbl->addCell($row["location"]); 
		$tbl->addCell($row["date"]);
        $tbl->addCell($row["time"]); 
		$tbl->addCell($row["price"]); 
} 

echo $tbl->display();
}

mysql_free_result($result);
[/php]

now if i show you in html how that is meant to display incase you dont understand me,

[code]

LOCATION CELL DATE CELL TIME CELL PRICE CELL
[/code]

Obviously replace the text with the data that is in the mysql data-base.

I thought $row would work but all i get is a load of messy no no.

Help very much appriciated!

Hi,

My guess would be as to why your using list and why your using foreach personally i only use foreach for arrays. i tend to use while loop, I’m not sure but i’d sugest firsts should you get rid of the list and use $newdata[‘location’] or use try using the while loop? hope this has a little help

I do not have access to the class that you are using, so I cannot test this, but see if this works for you:[php]mysql_connect("$host", “$username”, “$password”)or die(“cannot connect server “);
mysql_select_db(”$db_name”)or die(“cannot select DB”);

$result = mysql_query(“SELECT * FROM $tbl_name”);

$tbl = new HTML_Table(null, ‘display’, 1, 0, 4);

$tbl->addRow();
$tbl->addCell(‘Location’, ‘first’, ‘header’);
$tbl->addCell(‘Price’, null, ‘header’);
$tbl->addCell(‘Date’, null, ‘header’);
$tbl->addCell(‘Time’, null, ‘header’);

while ($row = mysql_fetch_assoc($result)) {
$tbl->addRow();
$tbl->addCell($row[“location”]);
$tbl->addCell($row[“price”]);
$tbl->addCell($row[“date”]);
$tbl->addCell($row[“time”]);
}
}
echo $tbl->display();

mysql_free_result($result);[/php]

Also, make sure that your are declaring $tbl_name at some point prior to executing any of this.

The data was originaly in an array, the MYSQL data was all defined i just dident post it up so tbl_name was defined. I actually found a better method for doing this while i was waiting on a reply which is so much simpler! but your attempt is no waste of time, makes me understand the code better which is what it is all about :smiley:

i moved on to something like this

[code]








<?php
while ($row = mysql_fetch_array($query)) {
echo “”;
echo “";
echo “";
echo “";
echo “";
echo “”;
}
        ?>
    </table>[/code]

which is, almost the same, and displays exactly the same just a bit more easier, so i could drop the whole A4 page class with just a few lines of code :slight_smile:

ID Location Date & Time (day/month/year) Price
”.$row[“id”]."”.$row[“location”]."”.$row[“dateandtime”]."” . “£”.$row[“price”].“p”."

Glad you got it figured out! This is much closer to how I would do it myself (bypassing the HTML_table class entirely.

You aren’t using a , which I would recommend. Also, you are using depreciated markup. It probably works just fine (for now), but I would consider doing something like this instead:[php]

<?php while ($row = mysql_fetch_array($query)) { ?> <?php } ?>
ID Location Date & Time (day/month/year) Price
<?php echo $row["id"]; ?> <?php echo $row["location"]; ?> <?php echo $row["dateandtime"]; ?> £<?php echo $row["price"]; ?>p
[/php]

You would then include the following css (either in your page head or in a css file. Note: if using a file, it should be named with the .css extension and you would remove the opening and closing tags:<style type="text/css"> table#productLoc{ width: 100%; } table#productLoc th{ background: #000000; font-weight: 600; color: #FFFFFF; } .c1{ width: 5%; } .c2{ width: 45%; } .c3{ width: 40%; } .c4{ width: 10%; } #productLoc td{ text-align: center; } table#productLoc td:nth-of-type(4){ text-align: right; padding: 0 15px 0 0; } </style>

The only additional note is that I do not know what your “striped” and “header” css classes look like, so you might need to do a little more with the css to duplicate what you were doing before.

The solution above not only is more manageable, but it is fully html5 compliant and validated, so it should work well into the future.

Let me know if you have any questions.

jay

Sponsor our Newsletter | Privacy Policy | Terms of Service