1

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)

6
  • Thanks, I never can get the formatting right on these sites... Commented Nov 27, 2012 at 4:07
  • It is illegal. You can use a ternary instead: $output3 .= '<option value="'. $info['company_code'] . (($result['company']==$info['description']) ? 'selected="selected"' : "") . $info['description'] . '</option>'; Commented Nov 27, 2012 at 4:08
  • Highlight your code block and ctl-k to format it as code. Or indent every line 4 spaces manually. Commented Nov 27, 2012 at 4:08
  • I tried to indent with 4 spaces but it didn't take apparently. I've never used a 'ternary' before. Does the question mark/colon have a sort of logic of "if the ternary operation is true, then do this"? I apologize for my lack of clarity. I had a crash course into PHP the past week or so. Commented Nov 27, 2012 at 4:13
  • Ah - to indent you must also have a blank line above the code block. So always start with an empty line, then indent 4. Commented Nov 27, 2012 at 4:15

3 Answers 3

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

I pretty much copy and pasted this and it worked right away! Thanks!
the simplest answers are usually the best
0

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;

2 Comments

I knew there was probably some logic that I could understand like this but I just wasn't coming up with much. Just a bit of confusion though, doesn't your code miss $info['description']. '</option>' ?
Lol just making sure :) . Thanks for the idea on how to fix it since I know this one will work if I can't get the ternary suggestions to work.
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.