1

I have a variable known as $moduleHTML in my dropdownmenu, $moduleHTML outputs all the modules from a course. E.g. 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>

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

While all the answers here are the same, I'm upvoting this for being most clear IMHO. :)
0

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 :)

Comments

0

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>';
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.