Forum Moderators: coopster

Message Too Old, No Replies

Putting SQL Values into an HTML table

         

toggo

10:49 am on Nov 22, 2005 (gmt 0)

10+ Year Member



I have a script returning the data from different records in an SQL table.

if (isset($_POST["Submit"])) {

$select = mysql_query("SELECT * FROM asset_register WHERE location = '$location' ") or die(mysql_error());

while ($data = mysql_fetch_array($select)) {

print $data["unitid"];
print "<br />";
print $data["make"];
print "<br /><br />";
print $data["model"];
print "<br /><br />";

This prints each individual record for the values I select, however, I would like to put these records into a table that would dynamically change depending on the number of rows and columns needed.

Is there a way of doing this with PHP?

Is this a difficult task?

mcibor

11:07 am on Nov 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome Toggo to WebmasterWorld!

It's a fairly easy task
Here's a snipet how you can do it:

if (isset($_POST["Submit"])) {

$select = mysql_query("SELECT * FROM asset_register WHERE location = '$location' ") or die(mysql_error());

echo "<table>\n<tr>\n><th>Id</th><th>Make</th><th>Model</th></tr>\n";
while ($data = mysql_fetch_array($select)) {
print "<tr><td>".$data["unitid"].
"</td><td>".$data["make"].
"</td><td>".$data["model"].
"</td></tr>\n";
}
echo "</table>";

The . is a concatenation sign - combines two strings into one. With this you can have as many rows as you wish.

There's also another way (more complex to understand), which doesn't care how many columns there will be (in the above you have to add columns manually)

if (isset($_POST["Submit"])) {

$select = mysql_query("SELECT unitid, make, model FROM asset_register WHERE location = '$location' ") or die(mysql_error());

echo "<table>\n";
while ($data = mysql_fetch_assoc($select)) {
print "<tr><td>" . implode("</td><td>", $data) . "</td></tr>";
}
echo "</table>";

Hope this will help you
Best regards
Michal Cibor

toggo

11:13 am on Nov 22, 2005 (gmt 0)

10+ Year Member



Thank you Michal

This works just how I wanted it to, you're a gem :-)