2

I'm creating my page builder but I have little problem with select box.

This is how I create a selectbox via jQuery

    var item_opts = '<select>'
        + '<option value="red">Red</option>'
        + '<option value="green">Green</option>'
        + '<option value="blue">Blue</option>'
        + '<option value="yellow">Yellow</option>'
    + '</select>'

$('.items_options').append(item_opts);

And this is how I call it in PHP

<select>
  <?php if ( $pb_item['message_color'] == "red" ) { ?>
    <option selected="selected" value="red">Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
    <option value="yellow">Yellow</option>
  <?php } elseif ( $pb_item['message_color'] == "green" ) { ?>
    <option value="red">Red</option>
    <option selected="selected" value="green">Green</option>
    <option value="blue">Blue</option>
    <option value="yellow">Yellow</option>
  <?php } elseif ( $pb_item['message_color'] == "blue" ) { ?>
    <option value="red">Red</option>
    <option value="green">Green</option>
    <option selected="selected" value="blue">Blue</option>
    <option value="yellow">Yellow</option>
  <?php } else { ?>
    <option value="red">Red</option>
    <option value="green">Green</option>
    <option value="blue">Blue</option>
    <option selected="selected" value="yellow">Yellow</option>
  <?php } ?>                      
</select> 

Is there some better solution how how to add parametr "selected" to the selected option than I have?

thx for your time and your advices :)

2 Answers 2

5

Yes, there is better solution

<select>
    <option <? ($pb_item['message_color'] == 'red'?'selected="selected"') ?> value="red">Red</option>
    <option <? ($pb_item['message_color'] == 'green'?'selected="selected"') ?> value="green">Green</option>
    <option <? ($pb_item['message_color'] == 'blue'?'selected="selected"') ?> value="blue">Blue</option>
    <option <? ($pb_item['message_color'] == 'yellow'?'selected="selected"') ?> value="yellow">Yellow</option>                   
</select>

even if you can list options via for loop for example:

$colors = array('blue', 'red', ..);
foreach($colors as $color) {
    echo "<option ".($pb_item['message_color'] == $color?'selected="selected"')."value='$color'>" . ucwords($color) . "</option>";
}
Sign up to request clarification or add additional context in comments.

2 Comments

And yes, there is even a better solution. And psst, we have this on the website already.
Thx so much :) It deffinately helped me :)
0

Those Who Trouble Understanding Previous Selected Answer by @vlcekmi3 here some other easy & dirty explanation.

$colors = array(
    1 => 'blue', 
    2 => 'red', ..); //init your array with key & value

foreach($colors as $key => $color) { // a loop 

    //print <option value="red"
    echo "<option value=".$key; 

    //print selected if your selected value matched -
    //please check before you echo 'selected' add a space before that
    //you will thank me later
    if($pb_item['message_color'] == $color){ echo " selected"; } 

    // close option with print '>'
    echo '>'; 

    // print your color name
    echo $color; 

    //print </option>
    echo "</option>"; 

}

what i Explained here is May be a Dirty Way but this code will help you understand how value will be returned to your view.

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.