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

?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.