IF inside while PHP/MYSQL

Asked

Viewed 742 times

0

I’m listing some results within while my goal is that when any of the variables I list in while come empty, it lists only those that come with content.

ps. is a polling system, I put to register 4 answers, but some questions have only yes or no as answers, so I want to list only those variables that come with content, follows the code below:

<?

      $consulta = "SELECT * FROM perguntas ORDER BY id ASC   " ;
      $resultado_consulta = mysql_query($consulta) or die("Erro no SQL: ".mysql_error());
    while($lista = mysql_fetch_array($resultado_consulta)){

    $id = anti_injection($lista['id']);
    $validacao_campo = anti_injection($lista['$validacao_campo']);
    $pergunta_principal = anti_injection($lista['$pergunta_principal']);
    $primeira_opcao = anti_injection($lista['$primeira_opcao']);
    $segunda_opcao = anti_injection($lista['$segunda_opcao']);
    $terceira_opcao = anti_injection($lista['$terceira_opcao']);
    $quarta_opcao = anti_injection($lista['$quarta_opcao']);

     ?>


    <fieldset>
        <br>
        <h2 class="fs-title"> <?php echo $lista['pergunta_principal']; ?> </h2>
        <br>
        <h3 class="fs-subtitle"></h3>

        </fieldset>

        <?php

if (!empty("$lista['primeira_opcao'];")){
echo"<?php echo $lista['primeira_opcao']; ?> <input type='radio' value='<?php echo $lista['primeira_opcao']; ?>' 
name='<?php echo $lista['validacao_campo']; ?>'> ";
}
if (!empty("$lista['$segunda_opcao'];")){
echo"<?php echo $lista['segunda_opcao']; ?> <input type='radio' value='<?php echo $lista['segunda_opcao']; ?>' 
name='<?php echo $lista['validacao_campo']; ?>'>";
}
if (!empty("$lista['$terceira_opcao'];")){
echo"<?php echo $lista['terceira_opcao']; ?> <input type='radio' value='<?php echo $lista['terceira_opcao']; ?>'
 name='<?php echo $lista['validacao_campo']; ?>'>";
 }
if (!empty("$lista['$quarta_opcao'];")){
echo"<?php echo $lista['quarta_opcao']; ?> <input type='radio' value='<?php echo $lista['quarta_opcao']; ?>' 
name='<?php echo $lista['validacao_campo']; ?>'>";
}
endif;
?>

        <br><br>
        <input type="button" name="previous" class="previous action-button" value="Anterior" />
        <input type="button" name="next" class="next action-button" value="Proxima" />
    </fieldset>

    <?php } ?>
  • And the problem is which?

  • is giving error in showing time,

  • I think it’s the final montage.. I’ll edit the code on the question that was wrong.

  • What mistake is it? could you edit the question with it.

  • Posting a minute

  • Where I edit my post ?

  • Below that blue comic (php, if, while) has the edit link, so code get formatted select it and press the button { }

  • Click on [Edit]

  • edited, is better for you to see ?

Show 4 more comments

1 answer

2


Remove the quotes and the point and comma inside these if’s:

!empty("$lista['primeira_opcao'];")

So he’ll stay that way:

!empty($lista['primeira_opcao'])

In your echo’s, you’re printing everything wrong. Change:

echo "<?php echo $lista['primeira_opcao']; ?> <input type='radio' value='<?php echo $lista['primeira_opcao']; ?>' name='<?php echo $lista['validacao_campo']; ?>'> ";

To:

echo $lista['primeira_opcao'] . "<input type='radio' value='" . $lista['primeira_opcao'] . "' name='" . $lista['validacao_campo'] . "'> ";
  • It’s just that it’s only showing the first echo.. when I want him to look at the four the variable that comes empty does not appear, only the one that comes.. in the case of my test, I passed one with four values.. just listed the first Alexandre

  • 1

    Solved this doubt thanks to all!

  • You have to make these changes to all values.

Browser other questions tagged

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