Automatic population of second drop down from mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robscriven
Forum Newbie
Posts: 6
Joined: Sun Jan 08, 2006 5:38 pm

Automatic population of second drop down from mysql

Post by robscriven »

Hi,

I've got three tables, one with teachers information in, one with lesson types in and one with which teacher performs which lesson. This information is returned in two recordsets:

Code: Select all

//returns the teachers for teachers menu
$query_teachermenu = "SELECT teacherid, teacher_name FROM teacher WHERE cancelled = 'N'";
$teachermenu = mysql_query($query_teachermenu, $connection) or die(mysql_error());
$row_teachermenu = mysql_fetch_assoc($teachermenu);
$totalRows_teachermenu = mysql_num_rows($teachermenu);

//returns the lessonsid and lessons name for lessons menu
$query_lessontypes = "SELECT * FROM teacherslessons, lessontype WHERE teacherslessons.lessontypeid = lessontype.lessontypeid";
$lessontypes = mysql_query($query_lessontypes, $connection) or die(mysql_error());
$row_lessontypes = mysql_fetch_assoc($lessontypes);
$totalRows_lessontypes = mysql_num_rows($lessontypes);
These recordset are then displayed in two drop down menus, one displaying teacher and one the lessons, like so:

Code: Select all

<form method="post" name="form1">
        <select name="teacherid">
          <?php 
do {  
?>
          <option value="<?php echo $row_teachermenu['teacherid']?>" ><?php echo $row_teachermenu['teacher_name']?></option>
          <?php
} while ($row_teachermenu = mysql_fetch_assoc($teachermenu));
?>
        </select>
      </div></td>
    <tr>
    <tr valign="baseline">
      <td nowrap align="right"><div align="left">Lesson:</div></td>
      <td><div align="left">
        <select name="lessontypeid">
          <?php 
do {  
?>
          <option value="<?php echo $row_lessontypes['lessontypeid']?>" ><?php echo $row_lessontypes['lessontype_name']?></option>
          <?php
} while ($row_lessontypes = mysql_fetch_assoc($lessontypes));
?>
        </select>
</form>
Currently these have no relation and i would like it so the options displayed in the second drop down menu are dependent on the teacher selected in the first. So you would only get the lessons that the teacher teaches in the second drop down. I've look on the net and the examples ive found dont really suit my problem.

Can anyone help?

Thanks,

Rob
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

viewtopic.php?t=29084 may be of interest.
Post Reply