0

I am building a project on student attendance management. I am stuck at one feature. At the time when teacher wants to mark attendance of a class, he/she must fill a form about class details and subject details. What I want is to show only those classes to teacher, of which the subjects of class are assigned to the teacher. Classes are listed in a select box. The problem is occurring when more than one subjects of a class is assigned to a teacher. It show class more than once in the select box. I just want to show each class only once.

Here's my code of select box.

<div class="form-group">
  <label for="class_id" class="control-label col-sm-2">Select Class Id :</label>
 <div class="col-xs-10">
    <select class="form-control" name="class_id" id="class_id">
       <option selected="selected" value="">Select Class Id</option>                              
       <?php 
           $q="select * from subjects where teacher_id='$_SESSION[teacher]'";
           $res=mysqli_query($con,$q);
           while($row=mysqli_fetch_array($res)){
               echo "<option class='form-control'>".$row['class_id']." </option>";
          }
      ?>
   </select>
 </div>
</div>
Nalaka526
  • 11,278
  • 21
  • 82
  • 116
Mohit kumar
  • 447
  • 2
  • 10

2 Answers2

1

Use distinct in your SQL query to ensure subject names appear only once in your select tag.

Eg -

 SELECT DISTINCT class_id FROM subjects WHERE teacher_id='$_SESSION[teacher];
Venkat D
  • 127
  • 1
  • 10
1

There might be a problem in this part of the code. It might be returning duplicate values.

$q="select * from subjects where teacher_id='$_SESSION[teacher]'";

You can change it to return distinct class_id as you are only using the class_id.

$q="select DISTINCT `class_id` from subjects where teacher_id='$_SESSION[teacher]'";

You have to change the while loop accordingly. I'm not sure about PHP. But you might just use $row instead of $row['class_id'] in

 while($row=mysqli_fetch_array($res)){
               echo "<option class='form-control'>".$row['class_id']." </option>";
          }

Cheers.

Anup Ammanavar
  • 432
  • 1
  • 3
  • 16