简体   繁体   中英

How to put variable in dropdown menu correctly

I have a variable known as $moduleHTML in my dropdownmenu, $moduleHTML outputs all the modules from a course. Eg Course: ICT, Modules: Web Programming, Modern Database Applications, E-Commerce.

Now when I output $moduleHTML, it works out perfectly as it outputs all the modules from the course. But if I try to put $moduleHTML in a dropdown menu, it only outputs one module as an option in the drop down menu (which is Web Programming). How am I suppose to implement it so that it shows multiple modules as multiple options in the dropdown menu?

Below is code:

<?php
$query = "SELECT cm.CourseId, cm.ModuleId, 
                     c.CourseName,
                     m.ModuleName
                     FROM Course c
                     INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
                     JOIN Module m ON cm.ModuleId = m.ModuleId
                     WHERE
                     (c.CourseId = '".mysql_real_escape_string($courseid)."')
                     ORDER BY c.CourseName, m.ModuleId";

$num = mysql_num_rows($result = mysql_query($query));
$dataArray = array();

while ($row = mysql_fetch_array($result)) { 
    $dataArray[$row['CourseId']]['CourseName'] = $row['CourseName']; 
    $dataArray[$row['CourseId']]['Modules'][$row['ModuleId']]['ModuleName'] = $row['ModuleName']; 
}

foreach ($dataArray as $courseId => $courseData) {
    $output = ""; 
    $output .= "<p><strong>Course:</strong> " . $courseId .  " - "  . $courseData['CourseName'] . "</p>";

    foreach ($courseData['Modules'] as $moduleId => $moduleData) { 
        // elaborate module data 
        $moduleHTML = "";
        $moduleHTML .= "<p>" . $moduleId . " - " . $moduleData['ModuleName'] ."</p>"; 
         $output .= $moduleHTML;
    }
}

echo $output;
?>


<form action="create_session.php" method="post" name="sessionform">
   <table>
     <tr>
        <th>7: Module:</th>
        <td><select name="module" class="modulesDrop">
             <option value=""><?php echo $moduleHTML; ?></option>
     </tr>
   </table>
</form>

Every module should have it's own option . You're putting the full HTML into one option now.

With a simple loop you can go through all modules and put them between options , put this inside the select tags.

foreach ($courseData['Modules'] as $moduleId => $moduleData) { 
  echo '<option value="">'.$moduleId.' - '.$moduleData['ModuleName'].'</option>'; 
}

This way you will get an option for every module looped through.

Run a foreach to create an option element for each module:

foreach ($dataArray as $courseId => $courseData) 
{
    foreach ($courseData['Modules'] as $moduleId => $moduleData) 
    {
        echo '<option value="' . $moduleId . '">' . $moduleData['ModuleName'] . '</option>';
    }
}

You have to do it in the same way:

$output = '<select name="module" class="modulesDrop">'.PHP_EOL;
foreach ($courseData['Modules'] as $moduleId => $moduleData) { 
             $output .= "<option value=".$moduleId.">".$moduleData."</option>".PHP_EOL;
         }
     }
$output .= '</select>';

Should show a dropdown :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM