How to insert an if/Else into an echo?

Asked

Viewed 1,359 times

3

echo '<option value="0" {if/else} >Em revisão</option>';
echo '<option value="1" {if/else} >Publicado</option>';

I wish I had a condition if/else to ask the database the value and give a selected value returned. You can help me?

It would be something like: if($resultado['status'] == 1) { echo 'selected=selected'; }

  • can give more details?

  • I believe your logic is right. Only the Selected=Selected should be, in the final string, like this: Selected="Selected". Just add the quotes. Or if it’s Html5, only selected resolves

  • The problem is that I insert {if/Else} into the echo and it misses. I’m having trouble passing the right instruction inside the echo.

  • Look at this example: echo '<option value="'. $value. '". if($value=='United States') echo 'Selected="Selected"';. '>'. $value. '</option>';

  • You can do the following: echo '<option value="0" ' . ($result['status'] == 1) ? 'Selected=Selected' '. ' >Under revision</option>'; echo '<option value="1" ' . ($result['status'] == 1) ? 'Selected=Selected' '' . ' >Published</option>';

  • Jesus seems like such a simple thing ... if I put pure html I can open the tags PHP but inside the echo I can’t.

  • That there is inside a foreach?

  • 1

    You are not being clear. There is a lack of information.

Show 3 more comments

6 answers

8

Utilize sprintf and ternario

Code

$dadoBanco = 1;

$option = '<option value="%s" %s>%s</option>';

$selected = 'selected="selected"';

echo sprintf($option, 0, ($dadoBanco == 0) ? $selected : '', 'Em revisão');
echo sprintf($option, 1, ($dadoBanco == 1) ? $selected : '', 'Publicado');
  • 2

    That would be my answer xD. + 1 would only trade sprintf for printf :P

7


You can use it as follows:

echo '<option value="0" ' . (($resultado['status'] == 0) ? 'selected=selected' : '') . ' >Em revisão</option>';
echo '<option value="1" ' . (($resultado['status'] == 1) ? 'selected=selected' : '') . ' >Publicado</option>';
  • Perfect. I had already tried to do something similar but it seems that the instruction Selected=Selected was bugging the code. Thank you.

  • 3

    I don’t understand why this answer received -1. You don’t like concatenation?

  • 1

    Suggested improvement: pass the empty space of the string inside the ternary, so there is no space left in the tag if you do not have select. And in the first line, it would be == 0 nay?

7

You can use if ternário, like this:

<?php
$selected = $resultado['status'] == 1 ? ' selected="selected"' : '';
echo '<option value="1" ' . $selected . '>Publicado</option>';
echo '<option value="0" ' . $selected . '>Em revisão</option>'; 

4

For the case attribute Selected I would make an expression with the function printf.

Thus:

  printf('<select %s></select>', $resultado['status'] == 1 ? 'selected' : '');

If this were on itself HTML I would use the function print PHP, combined with an expression

Thus:

<select <?php $resultado['status'] == 1 && print 'selected' ?>></select>

Note: You can’t do the same thing with the echo

  • 1

    haaa printf \the.

  • The expression with print is also cool. Reaches things that echo doesn’t make :)

  • 1

    you get the same result with echo, you need to change the syntax a little, the same is in the chosen answer.

2

It can be done that way.

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

-3

I think this might help you

<?php

$valor_select = $_POST['select'];

$valor_compara = '1';

echo '<select name="select" id="select">';
foreach($array_select as $val){
    $sel = ($val == valor_select)?'selected="selected"':'';

    echo '<option value="'.$val.'" '.$sel.'>'.$val.'</option>';
}
echo '</select>';

?>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.