Collect the value of GET in a Select and leave selected the correct value

0

I need to pass the value that I receive from a get to a Select and that the value I receive is selected. So far I've done that, but I only get the option selected by the GET, it does not show me the rest of the options. I show my code in case you see I'm doing wrong.

<select class="ui fluid dropdown" id="tipo" name="tipo">
   <?php
     $result_1 = $mysqli->query("SELECT * FROM tipos WHERE id =" . $_GET['necesito']);

    while ($row_1 = $result_1->fetch_array()) {
        if ($row_1['tipo'] == $tipo) {
            echo "<option value='" . $row_1['id'] . "' selected>" . $row_1['tipo'] . "</option>";
        } else {
            echo "<option value='" . $row_1['id'] . "'>" . $row_1['tipo'] . "</option>";
        }
    }
  ?>
</select>
    
asked by Miguel 09.08.2018 в 09:51
source

1 answer

0

You are doing where id = 'loQueSea' , this means that the sentence will show you only the records where id is loQueSea . In your case, whatever comes in _GET['necesito']) .

I understand that what you need is to show all the records in the table and that whatever comes for $_GET appears.

<select class="ui fluid dropdown" id="tipo" name="tipo">
   <?php
     $result_1 = $mysqli->query("SELECT * FROM tipos");

    while ($row_1 = $result_1->fetch_array()) {
        if ($row_1['id'] == $_GET['necesito']) {
            echo "<option value='" . $row_1['id'] . "' selected>" . $row_1['tipo'] . "</option>";
        } else {
            echo "<option value='" . $row_1['id'] . "'>" . $row_1['tipo'] . "</option>";
        }
    }
  ?>
</select>

Changes are:

  • We remove the clause WHERE from the sentence since we need all the fields.

    $result_1 = $mysqli->query("SELECT * FROM tipos");
    
  • Then, in the while , as we all go through, the one that has id equal to $_GET['necesito'] we mark it as selected.

    if ($row_1['id'] == $_GET['necesito']) {
       echo "<option value='" . $row_1['id'] . "' selected>" . $row_1['tipo'] . "</option>";
    }
    
answered by 09.08.2018 / 10:59
source