Colton.Wagner Posted August 21, 2012 Share Posted August 21, 2012 I am trying to generate the table below by pulling the information out of the database. The only issue I am having is the <tr> (Row) should have two <td> (Columns) inside it. <table> <tr> <td> Column #1 Row #1 </td> <td> Column #2 Row #1 </td> </tr> <tr> <td> Column #1 Row #2 </td> <td> Column #2 Row #2 </td> </tr> </table> Hopefully that makes sense here is the code I have so far: <?php $query = mysql_query("SELECT * FROM Doctoral_Biography ORDER BY Last_Name"); while($row = mysql_fetch_array($query)){ echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>"; echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>"; } ?> Any help would be greatly appreciated. I thought about adding either a ternary operator or a for loop. Please let me know what the most efficient option would be. Thanks in advanced! Quote Link to comment https://forums.phpfreaks.com/topic/267382-dynamic-table-using-php/ Share on other sites More sharing options...
scootstah Posted August 21, 2012 Share Posted August 21, 2012 If i understand correctly, something like this should work: <?php $count = 1; echo '<tr>'; while($row = mysql_fetch_array($query)){ echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>"; echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>"; if ($count % 2 == 0) { echo '</tr><tr>'; } $count++; } Quote Link to comment https://forums.phpfreaks.com/topic/267382-dynamic-table-using-php/#findComment-1371159 Share on other sites More sharing options...
ialsoagree Posted August 21, 2012 Share Posted August 21, 2012 Is there a problem with just adding the table rows into your current loop? echo '<table>'; while($row = mysql_fetch_array($query)){ echo '<tr>'; echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>"; echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>"; echo '</tr>'; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/267382-dynamic-table-using-php/#findComment-1371160 Share on other sites More sharing options...
Colton.Wagner Posted August 21, 2012 Author Share Posted August 21, 2012 If i understand correctly, something like this should work: <?php $count = 1; echo '<tr>'; while($row = mysql_fetch_array($query)){ echo "<td align=\"center\"><h3><img src=\"". $row['Image'] ."\" alt=\"" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "\" width=\"100\" height=\"139\" class=\"docphotos\" /></h3>"; echo "<h3><a href=\"./doctors.php?action=bio&name=" . $row['Last_Name'] ."\">" . $row['First_Name'] . " " . $row['Last_Name'] . ", " . $row['Prefix'] . "</a></h3></td>"; if ($count % 2 == 0) { echo '</tr><tr>'; } $count++; } After I posted this I found a solution myself. It's your code almost exactly but a little bit more messy. Thank you very much for your assistance. Quote Link to comment https://forums.phpfreaks.com/topic/267382-dynamic-table-using-php/#findComment-1371162 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.