HTML PHP concatenation

Asked

Viewed 54 times

0

Below follows the code and has a comment where I’m having trouble concatenating html + php... basic silly error but already merged the cuckoo and do not see the problem. thank you.

<?php
              // Monta consulta MySQL para o gráfico
              $sqlano = "SELECT DISTINCT YEAR(dateent) as Year FROM `cadastro`";
              $queryano = $mysqli->query($sqlano);

?>

            <form method="GET" action="" name="menuForm">     
              <header>
                  <h3>Relatório de pedidos por mês do ano de: </h3>
                        <select id="ano" name="ano" onchange="document.forms['menuForm'].submit();">
                            <option value="2000" disabled selected> Escolha o ano </option> 

                                <?php
                                   while($dadosano = $queryano->fetch_array()){
                                   //essa linha abaixo estou com problema na concatenação mas já fundi a cabeça e não encontro o erro. 
                                   echo "<option value='".$dadosano['Year']."'  ". isset($_GET['ano']) && $_GET['ano']== $dadosano['Year']?' selected="selected"' : ' ' ;  "  > ".$dadosano['Year']. "</option>";

                                    }

                                 ?>

                          </select> 


              </header>
            </form>
  • 1

    And what would that be ; in the middle of the line, just after the ternary condition?

  • is to end the condition... condition ? code : code;

  • 1

    Not only are you ending the condition, you’re ending your echo; everything after will give syntax error (probably the error you got and omitted in the question).

  • No error in execution but did not do what you have to do, popular select and take what was selected...

1 answer

2


The error is syntax. Do:

echo "<option value='".$dadosano['Year']."'  ". isset($_GET['ano']) && $_GET['ano']== $dadosano['Year']?' selected="selected"' : ' ' ;  "  > ".$dadosano['Year']. "</option>";

How did you use a ; in the middle of the expression, it would be the same as:

echo "<option value='".$dadosano['Year']."'  ". isset($_GET['ano']) && $_GET['ano']== $dadosano['Year']?' selected="selected"' : ' ';
"  > ".$dadosano['Year']. "</option>";

This second line is not a valid PHP.

Other than that, there’s a lot of responsibility for just one expression. Simplify this and you won’t have so many problems.

$selected = (isset($_GET['ano']) && $_GET['ano'] == $dadosano['Year']) ? 'selected="selected"' : '';
echo "<option value='{$dadosano['Year']}' {$selected}>{$dadosano['Year']}</option>";
  • I had concatenated part by part and tbm did not work. I will test as you reported... thanks for enqto.

  • Now it worked out... thanks

  • And how do I reset select to value=0 in case you return the page or Reload in the page because only with the history.back or Reload button is not zeroing...

  • It will only reset when it does not exist $_GET['ano']

  • How do I reset the variable then when doing the Reload ? I tried unset( $_GET['YEAR'] ); but I must be doing something wrong.

Browser other questions tagged

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