0

I have a select box that when the page loads it sets a value selected based upon a variable. I also have 2 IF statements that checks the variable and returns the results. Here is my code I am trying to do:

<select id="satbreak1" name="satbreak1">
<?php if ($trimmed_title[0] == "Guest")
  {
    echo ('<option selected value="#">Select One</option>');
    echo ('<option value="y">Yes</option>');
    echo ('<option value="n">No</option>'); 
  }
 if     ($trimmed_title[0] != "Guest")
  {
    echo ('<option <?php if($trimmed_satBreakfast[0] == "") echo ("selected ") ; ?> value="#">Select One</option>');
    echo ('<option <?php if($trimmed_satBreakfast[0] == "y") echo ("selected ") ; ?> value="y">Yes</option>');
    echo ('<option <?php if($trimmed_satBreakfast[0] == "n") echo ("selected ") ; ?> value="n">No</option>');
  }
 ?>
 </select>

What I need to do is if $trimmed_title[0] == "Guest" then echo out the first part, but if $trimmed_title[0] != "Guest") then echo out the second part and also check what the status of $trimmed_satBreakfast[0] is and set the selected value.

What is the best / correct way to handle this?

The first part works fine but having an php script in a echo seems to fail.

1
  • You can't just re-insert PHP logic into an echo. Echo tells the PHP to output some string to the final page output. If you want those if statements to work, you would have to build the string output and then echo it. Commented Nov 12, 2015 at 17:39

3 Answers 3

1

I think this might do the trick - not tested but theoretically looks ok

<select id="satbreak1" name="satbreak1">
    <?php 
        if ( $trimmed_title[0] == "Guest" ) {
            echo '
                <option selected value="#">Select One</option>
                <option value="y">Yes</option>
                <option value="n">No</option>';

          } else {

            $tsb=$trimmed_satBreakfast[0];
            echo '
                <option selected value="#" '.( $tsb=='' ? 'selected' : '' ).'>Select One</option>
                <option value="y" '.( $tsb=='y' ? 'selected' : '' ) .'>Yes</option>
                <option value="n"'.( $tsb=='n' ? 'selected' : '' ).'>No</option>';

        }
    ?>
 </select>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you @RamRaider this was a big help and solved my issue.
1

Something like this:

<?php
$options = array(
  'values' => array('', 'y', 'n'),
  'titles' => array('Select One', 'Yes', 'No')
)

$index = $trimmed_title[0] == 'Guest' ? 0 : array_search($trimmed_satBreakfast[0], $options['values']);
//if ($index === false) $index = 0;

$optHtml = array();
foreach($options['values'] as $i => $val) {
  $selected = $index == $i ? 'selected' : '';
  $optHtml[] = "<option $selected value=\"$val\">{$options['titles'][$i]}</option>";
}
$optHtml = implode("\n", $optHtml);
?>

<select id="id" name="name">
  <?php echo $optHtml?>
</select>

or any other style to split html from php:

//if ($index === false) $index = 0;
...
?>

<select id="id" name="name">
<?php foreach($options['values'] as $i => $val) {?>
  <option <?=$index==$i?'selected':''?> value="<?=$val?>">
    <?=$options['titles'][$i]?>
  </option>
<?php } ?>
</select>

In this case HTML not used in the common php code, but php used in HTML (only general template features: loop through data and echo it).
Common purpose of this is possibility to extract template part to separate file.

Comments

0

It is already PHP so you have to remove the php tags

echo ('<option '.($trimmed_satBreakfast[0] == "")  ? : '"selected"' : ''.' value="#">Select One</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "y") ? : '"selected"' : ''.' value="y">Yes</option>');
echo ('<option '.($trimmed_satBreakfast[0] == "n") ? : '"selected"' : ''.' value="n">No</option>');

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.