tags:

views:

56

answers:

4

I`m using this code to repopulate drop down list from the database :

      $city_id = 15;
  while($row = mysql_fetch_assoc($result)) { 
          $selected = ($row['city_id'] == $city_id) ? 'selected="selected" ' : NULL;
          echo '<option value="'.$city_id .$selected . '">"'.$row['city_name'].'"</option>\n';

  }

It`s work like a charm but my question is are they more elegance solutions ?

+3  A: 

Other than improving the indentation of the code, this is fine.

$city_id = 15;
while($row = mysql_fetch_assoc($result))
{ 
    $selected = ($row['city_id'] == $city_id) ? ' selected="selected"' : NULL;
    echo '<option value="' . $row['city_id']. '"' . $selected . '>'.$row['city_name'].'</option>\n';
}
shamittomar
A: 
  1. mysql_fetch_assoc to mysql_fetch_array
  2. add proper comments
  3. use standard php class ezsql or simple class tuts

    $query="SELECT name,id FROM student";
    
    
    /* You can add order by clause to the sql statement if the names are to be displayed in alphabetical order */
    
    
    $result = mysql_query ($query);
    echo "<select name=student value=''>Student Name</option>";
    // printing the list box select command
    
    
    while($nt=mysql_fetch_array($result)){//Array or records stored in $nt
    echo "<option value=$nt[id]>$nt[name]</option>";
    /* Option values are added by looping through the array */
    }
    echo "</select>";//Closing of list box 
    
JapanPro
+1  A: 

Well, a more elegant solution would be to have a "controller" file that fetches all the cities an puts them into an array/object list/whatever. Then, in a "view" file, you iterate over that variable. That way, you separate a bit more the presentation from the logic.

In view:

<select name=student value=''>Student Name</option>
    <?php foreach($cities as $city): ?>
        <option value="<?php echo $city->id ?>" ><?php echo $city->name ?></option>
    <?php endforeach; ?>
</select>

Also, I'd highly recommend using PDO for DB access.

ign
Pretty much what I do, and what I was going to submit as a solution.
Martin Bean
Thanks. I don't think echoing stuff can be called "elegant". Anyways, even more elegant would be to use a proper framework.
ign
A: 

I always use a function, since select boxes are something I end up creating a lot...

function select($name, $default, $values, $style='', $param='') {
        $html = '<select name="'.$name.'" style="'.$style.'" '.$param.' >';
        foreach($values as $i => $data) {
            if (isset($data['noFormat'])) { 
                $html .= '<option value="'.$data['value'].'" '.(($data['value']==$default)?'SELECTED="SELECTED"':'').' '.
                         (isset($data['style']) ? ' style="'.$data['style'].'" ' : '').'>'.$data['text'].'</option>';
            } else {
                $html .= '<option value="'.htmlentities($data['value']).'" '.(($data['value']==$default)?'SELECTED="SELECTED"':'').' '.
                         (isset($data['style']) ? ' style="'.$data['style'].'" ' : '').'>'.htmlentities($data['text']).'</option>';
            }
        }
        $html .= '</select>';
        return $html;                 
    }

Then loop through your query to build the array like this:

$default[] = array('value' => '0',   'text' => 'Select a City...');
while($row = mysql_fetch_assoc($result)) {  
    $list[] = array('value' => $row['city_id'], 'text' => $row['city_name']);
}
$list = array_merge($default,$list);

And finally an example to create the HTML:

select('select','form_el_name',$list['0'],$list,'font-size:12px;','onChange="document.forms[0].submit();"');

Hope it helps!

Mikey1980
who down votes the accepted answer and doesn't leave a comment?
Mikey1980