Returning values from a PDO function

UntrustfulCat

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?

Simone Nigro

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>";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related