Search by filter using mysql

-2

Hello! I need a search with filter relative to the page of product by category, that is, when searching for products by a certain category show in the sidebar filter options like: new or used, minimum price up to maximum price, etc. for better understanding see the image:

Assuming the category is CARS, the siderbar would look like this:

Belowisthecode,butIamalaymanandIdonotknowhowtocallthevalueofthecheckboxchosenandthepricefield.

<?php

$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "SELECT condição FROM carros WHERE condição=$_POST['cond']";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "Aqui aparece os carros com a condição escolhida na checkbox;
    }
} else {
    echo "Nada foi encontrado.";
}


$sql = "SELECT preço FROM carros WHERE condição > $_POST['ps'] AND condição < $_POST['pe']";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo "Aqui aparece os carros com o intervalo de preço determinado;
    }
} else {
    echo "Nada foi encontrado.";
}


mysqli_close($conn);

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="style.css" rel="stylesheet" type="text/css">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script><title></title></head><body><divclass="page_listing">
<div class="grid-row">
<div class="grid-col col-1">
<aside class="site_sidebar">
				
<div id="searchmain" class="section_search">
		
<div class="section_search-by-category">
<div class="search-category" id="search-category">
					
<h4 class="search-category-title">Filtros</h4>
<div class="section_subcategory-filter">
<div class="category-nav">

</div>
<div class="search-subcategory-filter cat_9020"">


<div id="searchextras" class="subcategory-filters">
<a href="#gofilters" id="gofilters"></a>


<div id="condition" class="condition featurebox filter checklist">
    <p class="term">Novo/Usado</p>
    <ul class="list">
        <li class="item">
            <label class="form-label" for="produto_novo">
                <input class="form-field form-field-checkbox" type="checkbox" name="cond[]" title="Novo" id="condition_1" value="1" >
                    Novo
            </label>
        </li>

        <li class="item">
            <label class="form-label" for="produto_usado">
                <input class="form-field form-field-checkbox" type="checkbox" name="cond[]" title="Usado" id="condition_2" value="2" >
                    Usado
            </label>
        </li>
    </ul>
</div>

<div id="pricerange" class="pricerange featurebox filter range">
    <label class="term" for="ps">Preço</label>
    <input class="form-field form-field-text half" type="text" name="p_min" id="ps" placeholder="Mín." maxlength="13" value="a" >

    <input class="form-field form-field-text half" type="text" name="p_max" id="pe" placeholder="Máx." maxlength="13" value="b" >
    <input type="submit" class="btn btn-green btn-filter btn-disabled" value="Filtrar" id="price_bt">
</div>

</div>
</div>
</div>
</div>

</body>
</html>
    
asked by anonymous 03.06.2016 / 04:16

1 answer

0

I was able to solve my problem. Despite the lack of interest in helping and the two votes as a question unclear or insignificant, I leave here my thanks.

<?php

include "conexão.php";

$sql = "SELECT condicao FROM dados WHERE condicao = " .$_POST['condicao'];
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["condicao"]."<br>";
    }
} else {
    echo "Nada foi encontrado.";
}

$sql = "SELECT preco FROM dados WHERE preco >= " .$_POST['p_min']. " AND preco <= " .$_POST['p_max'];
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        echo $row["preco"]."<br>";
    }
} else {
    echo "Nada foi encontrado.";
}

mysqli_close($conn);

?>
    
05.06.2016 / 01:06