webguync Posted May 17, 2010 Share Posted May 17, 2010 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); ?> Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 17, 2010 Share Posted May 17, 2010 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 Quote Link to comment Share on other sites More sharing options...
webguync Posted May 17, 2010 Author Share Posted May 17, 2010 that is quite an improvement, thanks! Quote Link to comment Share on other sites More sharing options...
milliclark Posted May 21, 2010 Share Posted May 21, 2010 This Script really works for me.Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.