0

I'm gettin' a headache on that. I need to put if statement inside an echo (this echo is in a function, it's for a form submit actually)

Here is an example on a partial of my code. In this situation, how can I put theses if statement inside my echo??

 <?php echo '<td><select id="depuis" name="depuis"> <option value=\'4\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'4\'){ echo \'selected\'; } else { echo ''; } ?> ></option> <option value=\'1\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'1\'){ echo \'selected\'; } else { echo ''; } ?> >2 ans et moins</option> <option value=\'2\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'2\'){ echo \'selected\'; } else { echo ''; } ?> >2 &agrave; 5 ans</option> <option value=\'3\' <?php if(isset($_POST[\'depuis\']) && $_POST[\'depuis\'] == \'3\'){ echo \'selected\'; } else { echo ''; } ?> >5 ans et plus</option> </select> </td>' ; ?> 
0

6 Answers 6

6

Everything is php so need to use more than the first <?php Finish each echo before checking with if. Like this:

<?php echo '<td><select id="depuis" name="depuis"> <option value="4"'; if(isset($_POST['depuis']) && $_POST['depuis'] == '4') { echo ' selected'; } echo ' >Something here maybe?</option>...etc 
Sign up to request clarification or add additional context in comments.

Comments

5

Use an inline if statement:

echo 'Based on your score, you are a ',($score > 10 ? 'genius' : 'nobody'); 

Comments

3

This would work - although I'm sure it could be streamlined:

<?php $out = ' <td> <select id="depuis" name="depuis"> <option value="4" '; if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ $out .= 'selected'; } $out .= ' ></option> <option value='1' '; if(isset($_POST['depuis']) && $_POST['depuis'] == '1'){ $out .= 'selected'; } $out .= ' >2 ans et moins</option> <option value=\'2\' '; if(isset($_POST['depuis']) && $_POST['depuis'] == '2'){ $out .= 'selected'; } $out .= ' >2 &agrave; 5 ans</option> <option value='3' '; if(isset($_POST['depuis']) && $_POST['depuis'] == '3'){ $out .= 'selected'; } $out .= ' >5 ans et plus</option> </select> </td> '; echo $out; ?> 

Meilleurs voeux...

Comments

2

You will want to use the a ternary operator which acts as a shortened IF/Else statement:

echo '<option value="'.$value.'" '.(($value=='United States')?'selected="selected"':"").'>'.$value.'</option>';

Comments

1

You shouldn't escape the array keys in the string

if(isset($_POST[\'depuis\']) ... 

Should be

if(isset($_POST['depuis']) && ... 

Also you could clean up the the generation of the options with a array and a loop

$options = array( 'label1' => 1, 'label2' => 2, 'label3' => 3, 'label4' => 4 ); $value = (isset($_POST['depuis'])) ? $_POST['depuis'] : 0; foreach ($options as $label => $optionValue) { $selected = ($optionValue == $value) ? ' selected' : ''; printf('<option value="%s"%s>%s</option>', $optionValue, $selected, $label); } 

2 Comments

If the order is important, you might say foreach (['4', '1', '2', '3'] as $x).
(BTW, all your options have value="4".)
0

Not sure you can do that like you're asking. You'd just need to use regular if/else if block and output the outcome separately.

<?php echo("<td><select id=\"depuis\" name=\"depuis\">"); echo("<option value='4'"); if(isset($_POST['depuis']) && $_POST['depuis'] == '4'){ echo("selected"); } echo("></option>"); [ ...etc... ] ?> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.