PHP how to let select set

Asked

Viewed 149 times

0

I have a select that takes the data from the database to make an insertion:

	<div class="row">
        <div class="form-group col-md-2">
            <label for="marca">Marca</label>
            <?php 
            $link = open_database();
            $query = "SELECT marca_id, marca_nome FROM marca";
            $queryidmarca = mysqli_query($link, $query); ?>
            <select class='form-control' id='marca' name="veiculo['marca_id']">
                <option value="">-Selecione-</option>
                <?PHP while ($tipo = mysqli_fetch_array($queryidmarca)){ ?>
                <option value="<?PHP echo $tipo['marca_id'] ?>"><?PHP echo $tipo['marca_nome'] ?></option>
				<?PHP } ?>
        	</select>
        </div>
    </div>

I wanted to know how to leave selected the information that was saved in this select in another select that is in another form.

2 answers

1


Let’s go in pieces.

You will have 2 querys for that reason:

  1. To fetch the value you saved;

  2. To generate input of the kind select with due option.

Here you search for the record that was previously saved and store in a variable (it has to be 1 value only, so you need to have the WHERE to fetch the previously saved record):

$query1 = "SELECT marca_id, marca_nome FROM marca WHERE registro = 20";
...(faz o fetch, etc)...
$opcaoSalva = $array['marca_id'];

With this variable $opcaoSalva you know the registration option you are looking for correct !? So now you will generate your field input of the kind select, and while generating the option you will check if she is the same marca_id of your variable $opcaoSalva.

$query2 = "SELECT marca_id, marca_nome FROM marca";
...
while ($tipo = mysqli_fetch_array($queryidmarca)){ ?>

   <option value="<?php echo $tipo['marca_id'] ?>" <?php echo ($tipo['marca_id'] == $opcaoSalva) ? 'selected' : '' ?>><?php echo $tipo['marca_nome'] ?></option>

So no while, you will generate all options, but while you generate, will check whether $tipo['marca_id'] is the same as $opcaoSalva, and if so, it prints selected within the tag <option> (which is standard of html that).

<?php echo ($tipo['marca_id'] == $opcaoSalva) ? 'selected' : '' ?>
  • Thank you, you solved the problem.

1

You can use the attribute selected, and make a ternary condition to print the selected.

For example:

<select name="veiculo">
  <option <?php echo ($condicao === true) ? 'selected' : ''; ?>>Opção</option>
</select>
  • This code gave sytaxe error in the option line

  • Try again, I had made a mistake. :)

  • Now yes. Plus this $condition variable, where does it come from ? Why did it appear as an undefined variable.

  • I used it as an example. That’s like a if short. It is called ternary operator. You can proceed as you need. :)

  • Sorry if I’m being too lay, but I understood the if ternary, just do not know how to make a condition that brings the selected item

Browser other questions tagged

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