views:

205

answers:

3

I am trying to code a function which creates a dropdown of school names selected from a database. It is doing fine creating a dropdown but it is not putting anything in the dropdown. Here is the code:

function schoolDD($name, $selected){
   $select = '';
   if( $selected != null )
   {
      $select = $selected;
   }

     $qry = "select * 
       from  school
       order by name, id
       where display = 'Y'";

     $schools = _execQry($qry);


   $html = '<select name="'.$name.'" >';

   foreach( $schools as $s ){
      $html .= '<option value="'. $s['id'] .'"';
      if( $select == $s['name'] ){
         $html .= 'selected="selected"';
      }
      $html .= '>'. $s['name'] . '</option>';

   }
   $html .= '</select>';
   return $html;
}
A: 

Problem solved. It was because in the query I had order before where. It should have been:

$qry = "select *   
             from   school
             where display = 'Y'
             order by name, id";

Not:

 $qry = "select *   
             from   school
             order by name, id
             where display = 'Y'";
Josh Curren
:) you should probably modify your _execQry function to throw an exception if the query is invalid. Look for mysql_error
David Caunt
It does check mysql_error, which it then sends an email to me with the problem. What it told me for this was:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where display = 'Y'' at line 4but I could not figure out the issue there.
Josh Curren
A: 

Looks OK but hard to say without knowing what _execQry does.

If you add the line

print_r($schools);

after you call _execQry, are you definitely retrieving results from the database?

David Caunt
_execQry just performs the query so all error catching is one spot. _execQry works fine. I have been using it for over a year without any issues.
Josh Curren
A: 

Im glad you found your answer, but i haaattee php and html mixed together like that.

what about tsomethign like this...

<?php

$schools = getSchools();
$selectedSchool = 'sfgsfgd';
$name = 'asdfsafd';

?>

<select name="<?= $name ?>">
<?php foreach( $schools as $s ): ?>
    <option value="<?= $s['id'] ?>"<?php if( $s['name'] == $selectedSchool ): ?> selected="selected"<?php endif; ?>><?= $s['name'] ?></option>
</select>
Galen