I have a problem with a select (type combo or dropdown), there I load with php a series of categories, the issue is that by an error of armed of the original base since the categories can not be more than 20/30 , they are always called by their category field (text) and not by their ID. In this case, I charge the select both in value and in the text to be displayed with a string or name of the category, since the query to show the articles of the category I do it by a like.
I choose the category, I make a query, it reloads the page with the new query, and I lose the location of the select, it returns to the first element, although I need to be exactly in the category that was chosen last. Any idea how to do it? I show you the part of the code:
<form id="guarda_combo" method="POST" action="categoria.php">
<span> Seleccione una Categoría </span>
<select id="combo_categorias" name="cat" onchange="location.href='categoria.php?num=' + this.value +'1';">
<?php
if ($result = mysqli_query($con, $consulta)) {
/* obtener array asociativo */
while ($filas = mysqli_fetch_assoc($result)) {
$idCategoria=$filas['idCategoria'];
$descCategoria=$filas['descCategoria'];
echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
}
?>
</select>
</form>
sanzante, for a question of design, maybe not very good, when choosing a category of the I go category.php which is where I take the value sent and process the view. Right there, I matched the category with a session variable called "cat_elected". I adapted the solution to the following:
while ($filas = mysqli_fetch_assoc($result)) {
$idCategoria=$filas['idCategoria'];
$descCategoria=$filas['descCategoria'];
$currentcategoria=$_SESSION["cat_elegida"];
// echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
if ($descCategoria == $currentCategoria)
{
$selected = 'selected="selected"';
echo "<option $selected value='".$descCategoria."'>" . $descCategoria."</option>";
}else{
echo "<option value='".$descCategoria."'>" . $descCategoria."</option>";
}
}
for some mistake or reason it does not work for me,