I have this function in an included file
function ListModules() {
try {
global $DBH;
$ListModules->query("SELECT * FROM modules");
$ListModules->execute();
return $ListModules->fetchAll();
} catch (PDOException $e) {
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
} }
Then I'm trying to echo them into a drop down box like this:
echo "<select name='deletelist' class='form-control'>
<option value='0'>Choose a module to delete</option>";
while($modulelist = ListModules()) {
echo "<option value='".$moduelist['id']."'>".$modulelist['name']."</option>";
}
echo "</select>";
What am I doing wrong?
ListModules()
return array
while($modulelist = ListModules())
is equal to
while(true)
having to cycle an array, in your case it is better to use foreach
$modulelist = ListModules();
echo "<select name='deletelist' class='form-control'>
<option value='0'>Choose a module to delete</option>";
foreach( $modulelist as $module ){
echo "<option value='".$module ['id']."'>".$module ['name']."</option>";
}
echo "</select>";
Well, that's because your function return an array of some sort, so your loop is the equivalent to:
while ($modulelist = array(1, 2, 3)) {
Which basically means that it would always be true.
What you probably want is a foreach
loop.