Skip to content
Advertisement

My nested while loop is giving me only a single row from database whereas I have many records in database

I want to create time table. Need to get subject names from database in check boxes and teachers name from database in select dropdown. I am using following code but it give me only single row. I don’t know where I am doing mistake.

<table width="100%">
   <tr>
     <th>Select Subject</th>
     <th>Period Time</th>
     <th>Teacher</th>
   </tr>
<?php 
   $query = mysqli_query($conn, "select * from subjects");
   while ($row = mysqli_fetch_array($query))
   {
   $subject_id = $row['subject_id'];
   $subject_name = $row['subject_name'];
   echo "<tr>


  <td><input type='checkbox' name='subject_id' value='$subject_id'> $subject_name</td>
    <td>
     <div class='form-group'>
     <select name='class_time' class='form-control'>
     <option>10:00</option>
     <option>11:00</option>
     <option>12:00</option>
     <option>01:00</option>
     </select>
     </div>
    </td>
    <td>
   <div class='form-group'>
      <select name='employee_id' class='form-control'>";
      $query = mysqli_query($conn, "select * from employees where designation = 'Teacher' OR designation = 'Principal'");
      while ($row = mysqli_fetch_array($query)) 
      {
         $employee_id = $row['employee_id'];
         $employee_name = $row['employee_name'];
         echo "
         <option>$employee_name</option>";
      }
         echo "</select></div>
        </td>
      </tr>";
   }

  ?>

</table>

Advertisement

Answer

The problem you have is that you use the same variable name $query for both queries, and so the second one overwrites the first one. Try using $empQuery or something else for the second one.. (same for $row -> $empRow)

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement