mercredi 20 mai 2015

Pre-Highlighting Options in multiple select if categories already assigned to post mysql

So I have the following Code

<select name='classes' class='students_classes' multiple>
<?php

$currentStudentID = (int) $_GET['studentID']; //Variable to get student ID From URL

$queryGetAllClassesPivot = mysql_query("SELECT * FROM students_classes"); //Get all classes in pivot table

$queryCheckAssignedClasses = mysql_query("SELECT studentID FROM students_classes"); //Get all student ID's from pivot table
$CheckAssignedClasses = mysql_fetch_assoc($queryCheckAssignedClasses); //Query


$queryAssignedClasses = mysql_query("SELECT c.Title FROM classes c
                          INNER JOIN students_classes sc ON c.ID = sc.classID
                          INNER JOIN students s on s.ID = sc.studentID
                          WHERE s.ID in ('$currentStudentID')");
$assignedClasses = mysql_fetch_assoc($queryAssignedClasses);


$getAllClasses = mysql_query("SELECT * FROM classes");


   while($allStudentClasses = mysql_fetch_assoc($getAllClasses)){
      if ($allStudentClasses['ID']==$assignedClasses['classID']){ ?>
         <option selected="selected" value="<?php echo $assignedClasses['ID']; ?>"><?php echo $assignedClasses['Title']; ?></option>
      <?php
      }else{ ?>
         <option value="<?php echo $allStudentClasses['ID']; ?>"><?php echo $allStudentClasses['Title']; ?></option>
      <?php
      }
   }?>
</select>

My problem is that I want to populate a multiple select showing all the available classes which can be assigned to a student but I also want the already assigned classes to be selected when the page loads so that the person assigning new classes to that student will know that the specific student already has those selected classes assigned to him...

Like for example if student "ONE" is assigned to classes "A","B" & "C" and the whole list of classes is "A","B","C","D","E".... I want to show a multiple select box with "A","B","C" Selected and "D","E" unselected.

Thanks ;)

Aucun commentaire:

Enregistrer un commentaire