Forum Moderators: coopster
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?
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