Jump to content

[SOLVED] Loop Problems


dhorn

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

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