Search ID in a select in PHP

-1
<select class="form-control" name="cliente">
    <?php if ($clientes) {
        foreach($clientes as $ind => $valor) {
     ?>
        <option value="<?php echo $valor->nome ?>"><?php echo $valor->nome ?></option>
    <?php } } ?>
</select>

I would like to understand this code, everything indicates that it does a select and brings the list to make a registration.

    
asked by anonymous 18.09.2017 / 19:16

2 answers

2

=> is used to associate key and value in a vector.

-> is used to access a method or property of an object.

<select class="form-control" name="cliente">
<?php 
   if ($clientes) {   //Se houver algum cliente ele entra no IF
      foreach($clientes as $ind => $valor) { //Para cada cliente ele criará uma opção
         ?>
            <option value="<?php echo $valor->nome ?>"> <!--Seta o valor da opção com propriedade nome do objeto valor. -->
               <?php echo $valor->nome ?> <!--Exibe a propriedade nome do objeto valor.-->
            </option>
         <?php 
      } 
   } 
?>
</select>
    
18.09.2017 / 19:34
3
<select class="form-control" name="cliente">
....
</select>

These lines are the opening and closing of the HTML tag to create a select (combo box or select) object on the HTML page, not to perform a select on a database. Probably this select was already done at an earlier point in the code, not shown in your example.

<?php if ($clientes) {
    foreach($clientes as $ind => $valor) {
 ?>
    <option value="<?php echo $valor->nome ?>"><?php echo $valor->nome ?></option>
<?php } } ?>

In this portion of code the combo box object is populated with options. In the line with if it is tested if the variable $ clients has a true value, in this case if it is the "result" of the bank search, if it is null it will be considered false and will not enter if, otherwise it probably because it has return values, enter.

In the block of foreach each record of the query result is traversed and the index placed in $ind and the record in $valor . The line that starts with the tag assembles the choice option that will be shown in the combo box object.

    
18.09.2017 / 19:27