Select dropdown from database

I use this code to refill the dropdown 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 works like a charm, but my question is, are they more elegant solutions?

+3
source share
4 answers

I always use the function, since the selection boxes are what 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 do a query loop to build the array as follows:

$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 of creating HTML:

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

Hope this helps!

0
source

Besides improving the indentationcode, this is normal.

$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';
}
+3
source

, "controller", , //. "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>

In addition, I highly recommend using PDO to access the database.

+1
source
  • mysql_fetch_assoc in mysql_fetch_array
  • add correct comments
  • use standard php ezsql class or simple tuts class

    $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 
    
0
source

Source: https://habr.com/ru/post/1762873/


All Articles