简体   繁体   中英

Dropdown menu to filter results in php get all results from sql

I have this drop down:

<select class="w-select filterdropdown" id="Tipo-de-cocina" name="tipofiltro" data-name="Tipo de cocina">
<?php
    if (isset($_GET['tipofiltro'])) {
        echo '<option value="' .$filtrocuisine.  '"> '  .$filtrocuisine. "</option>";

    } else {
        echo '<option value="Todos los tipos">Todos los tipos</option>';
    }

?>


              <option value="Japonesa">Japonesa</option>
              <option value="Mexicana">Mexicana</option>
              <option value="India">India</option>
              <option value="Mediterranea">Mediterranea</option>
              <option value="Italiana">Italiana</option>
              <option value="Americana">Americana</option>
              <option value="Asiatica">Asiatica</option>
              <option value="Thai">Thai</option>
              <option value="China">China</option>
              <option value="Francesa">Francesa</option>
              <option value="Turca">Turca</option>
              <option value="Latina">Latina</option>
              <option value="Africana">Africana</option>
              <option value="Griega">Griega</option>
              <option value="Arabe">Arabe</option>


            </select>

How can i make that when the user selects the field "Todos los tipos" my sql query returns all types? This is the sql behind:

if (isset($_GET['preciofiltro']) OR isset($_GET['preciofiltro'])) {
    $filtroprecio = $_GET['preciofiltro'];
    $filtrocuisine = $_GET['tipofiltro'];

    $sql = "SELECT Meals.Meal_ID, Meals.Name, Price, Cooks.Cook_ID 
            FROM Meals 
               INNER JOIN Cooks ON Cooks.Cook_ID = Meals.Cook_ID 
            WHERE Cooks.Area = '$area' 
              AND Meals.Capacity > 0 
              AND Meals.Price < '$filtroprecio' 
              AND Meals.Type = '$filtrocuisine'";

    $result = mysqli_query($conn, $sql);

Basically I would need something such as "AND Meals.Type = any"

Cheers!

Well there are simpler methods but without rewriting everything the simple answer is that if the user selects Todos los tipos from the dropdown what you actually want to do is remove this selection criteria AND Meals.Type = '$filtrocuisine' from the query completely ie you no longer limit the query with that criteria.

So change your script like this :-

I am of course assuming that you have taken data from the $_GET array, validated it, and cleansed it before we get to this code.

if (isset($_GET['preciofiltro']) OR isset($_GET['preciofiltro'])) {
    $filtroprecio = $_GET['preciofiltro'];
    $filtrocuisine = $_GET['tipofiltro'];

$sql = "SELECT Meals.Meal_ID, Meals.Name, Price, Cooks.Cook_ID 
        FROM Meals 
           INNER JOIN Cooks ON Cooks.Cook_ID = Meals.Cook_ID 
        WHERE Cooks.Area = '$area' 
          AND Meals.Capacity > 0 
          AND Meals.Price < '$filtroprecio'";

if ( isset($filtrocuisine) && $filtrocuisine == 'Todos los tipos' ) {
    $sql .= " AND Meals.Type = '$filtrocuisine'";
}

$result = mysqli_query($conn, $sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM