Jump to content

help w/ simplifying display of data from MySQL table


webguync

Recommended Posts

I there a way of simplifying this? I am wanting to display a row of data from a mysql table into an HTML table with each column in a <td> and a new row for each record. Also a header for each column. Started doing it like below but thinking this is really cumbersome and there is prolly a better way :-)

 

while ($row = mysql_fetch_assoc($result)) {
echo "<tr>";

echo"<th>Name</th>";

echo "</tr>";
echo "<tr>";
echo "<td>";
    echo $row["name"];
echo "</td>";
echo "</tr>";
        echo "<tr>";

echo"<th>Answer 1</th>";

echo "</tr>";
echo "<tr>"
echo "<td>";
    echo $row["Answer1"];
echo "</td>";
echo "<td>";
    echo $row["Answer2"];
echo "</td>";
echo "<td>";
echo $row["Answer3"];
echo "</td>";
echo "<td>";
echo $row["Answer4"];
echo "</td>";
echo "<td>";
echo $row["Answer5"];
echo "</td>";
echo "<td>";
echo $row["Answer6"];
echo "</td>";
echo "<td>";
echo $row["Answer7"];
echo "</td>";
echo "<td>";
echo $row["Answer8"];
echo "</td>";
echo "<td>";
echo $row["Answer9"];
echo "</td>";
echo "<td>";
echo $row["Answer10"];
echo "</td>";
echo "<td>";
echo $row["Answer11"];
echo "</td>";
echo "<td>";
echo $row["Answer12"];
echo "</td>";
echo "<td>";
echo $row["submit_timestamp"];
echo "</td>";
echo "</tr>";
}

mysql_free_result($result);

?>

Way too many echo statements. There are a number of ways to shorten this. Here's one way:

<?php
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><th>Name</th></tr><tr></td>{$row['name']}</td></tr>,<tr><th>Answer 1</th></tr><tr>";

for ($i =1;$i<13;++$i) {
    echo "<td>{$row['Answer'.$i]}</td>";
  }
echo "<td>{$row["submit_timestamp"]}</td></tr>";
}
mysql_free_result($result);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.