limitphp Posted February 17, 2009 Share Posted February 17, 2009 If I have a query that could return 0 or multiple results and I want to display them in a table in two columns, how do I handle it if there are an odd number of results and the second column won't get written? Ex) <table> <?php //query $count = 1 while ($row_artist = mysql_fetch_assoc($result_artist_search)) { if($count & 1) //odd { echo "<tr> <td> <?php echo $row_artist['name'] ?> </td>"; } else //even { echo "<td> <?php echo $row_artist['name'] ?> </td> </tr>"; } $count++; } ?> </table> What would be the best practice to handle displaying these artists if there are an odd number of them and I want to display them in two columns? Right now, it wouldn't close the row if there are an odd number of artists. thanks Link to comment https://forums.phpfreaks.com/topic/145583-solved-how-to-handle-query-with-unknown-number-of-results-in-2-columns/ Share on other sites More sharing options...
rhodesa Posted February 17, 2009 Share Posted February 17, 2009 after the loop, test the value of $count. if it's $even, you are good...if it's odd, print an empty cell and a close <tr> Link to comment https://forums.phpfreaks.com/topic/145583-solved-how-to-handle-query-with-unknown-number-of-results-in-2-columns/#findComment-764306 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.