I am designing and developing a form whose function is to edit the data of an item in a table, I already obtain all the data from it through an id that is the primary key in the table and I save each one of them in different variables. Once I have obtained the data, I show it in the input type=text
in input type=radio
and in a <select>
.
Being the form for editing, I have found an inconvenience with select
it since I would like it to show all the options that are described in another table that only contains in this case the descriptions of the categories with their respective code but that Initially, the option appears that is the same as the one previously consulted and that has already been saved in another variable.
In this way I have the html code with php embedded and that is how I show it in another form for the creation of the item:
<select id="categoria" name="categoria">
<?php
while ($cat = mysqli_fetch_array($query1)) {
//echo $cat;
?>
<option value="<?php echo $cat['id_categoria'].','.$cat['descripcion_categoria'] ?>" ><?php echo $cat['descripcion_categoria'] ?></option>
<?php
}
?>
</select>
This is the html code in the form for editing the item; In this code I show the variables where the data that the item originally has in the database table has been saved.
<select id="categoria" name="categoria">
<option value="<?php echo $idCat.','.$nomCat; ?>" ><?php echo $nomCat; ?></option>
</select>
How can I show all the options in the <select>
but initially show the one that is equal to the variables with the data obtained in the query?
Starting from the two examples that you have given, you would only have to concatenate them in the following way, so that they appear together:
if you are looking to automate the select option:
There is also another way to do it (the one I usually use):