简体   繁体   中英

Dynamically setting the selected option in a HTML drop down list - PHP

I am aware there are examples out there that hover around the issue of dynamically setting the selected tag in a HTML option tag, but I am hitting a pretty difficult issue of break statements and quotations in what I am trying to accomplish.

while($info = mysql_fetch_array($companydesc)) 
{
$output3 .= '<option value="'. $info['company_code'] . if ($result['company']==$info['description']){echo 'selected=\"selected\"'} . '">' . $info['description'] . '</option>';
}
echo $output3;

The error that I receive is a unexpected T_IF on the line with the if statement. Is it not legal to put an if statement in there? Or is it a matter of doing proper breaks? Any help would be greatly appreciated (and hopefully the formatting for the code worked)

Use a ternary statement:

while($info = mysql_fetch_array($companydesc)) {
    $output3 .= '<option value="'. $info['company_code'].'"'.($result['company']==$info['description'] ? ' selected=\"selected\"' : '') . '>' . $info['description'] . '</option>';
}
echo $output3;

yeah, it is illegal, my suggestion is:

while($info = mysql_fetch_array($companydesc))
{
  if($result['company']==$info['description'])
  {
    $output3 .= '<option value="' . $info['company_code'] . '" selected = selected>' . $info['description'] . '</option>';
  }
  else
  {
    $output3 .= '<option value="' . $info['company_code'] . '">' . $info['description'] . '</option>';
  }
}
echo $output3;

Trying to fix everything with an individual solution in the way that you originally intended to with stuff like

    while($info = mysql_fetch_array($companydesc)) 

is, I feel, one of the big things that is holding php back and eventually makes the code extremely difficult to maintain, although it's very easy on the learning curve.

This function takes an optional selected parameter and will build out your select box. This is much much better than doing every single select box for every query with its own custom code.

There might be a syntax error in here, but generally,

    function selectBox($array, $selected=false){
        foreach($array as $name => $value){
            $options .= '
                <option value=" . $name . '"'
                . ($selected != false && $selected == $value) ? 'SELECTED' : '' 
                . '>".$name."</option>"; 
       }
   } 

Worth mentioning that select box is requiring you to pass a predefined array of key=>value pairs, so you'll need to have an additional helper function (you can't just pass it your database return)

Also probably worth mentioning is that using mysql_fetch_array($companydesc) is not abstracting the database level at all. Down the line, code like this will prohibit a migration to something like Postgres or a different database system entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM