dhorn Posted June 17, 2009 Share Posted June 17, 2009 I have this code that uses nested loops to display tables, and populate them from a mySql database, but as of now..when it executes it displays a blank item if the sql table is empty, and I would prefer if it didn't. What would be the best method to solve this problem. Here is the code: <?php do { $query_info = "SELECT * FROM info WHERE course = ". $row_categories['id']; $info = mysql_query($query_info) or die(mysql_error()); $row_info = mysql_fetch_assoc($info); ?> <table border='1'> <tr> <th colspan='3' class = 'head'><?php echo $row_categories['name']; ?></th> </tr> <tr> <th>Item</th> <th>Name</th> <th>Delete?</th> </tr> <?php do { ?> <tr> <td align="center"><?php echo $row_info['item']; ?></td> <td align="center"><?php echo $row_info['first_name'];?></td> <td align="center"><input type='checkbox' name='checkbox[]' id='checkbox[]' value='<?php echo $row_info['id']; ?>'></td> </tr> </td> <?php } while ($row_info = mysql_fetch_assoc($info)); ?> </table> <br /> <br /> <?php } while ($row_categories = mysql_fetch_assoc($categories)); ?> Link to comment https://forums.phpfreaks.com/topic/162620-solved-loop-problems/ Share on other sites More sharing options...
rhodesa Posted June 17, 2009 Share Posted June 17, 2009 just use regular WHILE loops instead of DO/WHILE: <?php while ($row_categories = mysql_fetch_assoc($categories)) { $query_info = "SELECT * FROM info WHERE course = ". $row_categories['id']; $info = mysql_query($query_info) or die(mysql_error()); $row_info = mysql_fetch_assoc($info); ?> <table border='1'> <tr> <th colspan='3' class = 'head'><?php echo $row_categories['name']; ?></th> </tr> <tr> <th>Item</th> <th>Name</th> <th>Delete?</th> </tr> <?php while ($row_info = mysql_fetch_assoc($info)) { ?> <tr> <td align="center"><?php echo $row_info['item']; ?></td> <td align="center"><?php echo $row_info['first_name'];?></td> <td align="center"><input type='checkbox' name='checkbox[]' id='checkbox[]' value='<?php echo $row_info['id']; ?>'></td> </tr> </td> <?php } ?> </table> <br /> <br /> <?php } ?> Link to comment https://forums.phpfreaks.com/topic/162620-solved-loop-problems/#findComment-858300 Share on other sites More sharing options...
dhorn Posted June 17, 2009 Author Share Posted June 17, 2009 I tried using regular while loops, but for some reason the loops skipped the first items in the table. For example, if I use regular while loops, the page only displayed the last 4 out of 5 tables. Link to comment https://forums.phpfreaks.com/topic/162620-solved-loop-problems/#findComment-858320 Share on other sites More sharing options...
rhodesa Posted June 17, 2009 Share Posted June 17, 2009 ooops...forgot to add...remove the single fetch_assoc()s: $row_info = mysql_fetch_assoc($info); Link to comment https://forums.phpfreaks.com/topic/162620-solved-loop-problems/#findComment-858326 Share on other sites More sharing options...
dhorn Posted June 18, 2009 Author Share Posted June 18, 2009 Yep, that was the key. Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/162620-solved-loop-problems/#findComment-859070 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.