Search by filter using mysql

Asked

Viewed 385 times

-2

Hello! I need a search with filter relative to product page by category, that is, when looking for products by a certain category show in the sidebar filter options such as: 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:

inserir a descrição da imagem aqui

Below follows the code, however I am layman and do not know how to call the value of the chosen checkbox and the price field.

<?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>

<div class="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>

  • Add the [tag:mysql] by editing your question.

  • You’ve made some code?

  • You can use the mysqli query "SELECT".

  • I added html and css to advance.

  • HTML and CSS code without seeing php is not relevant. Have some PHP code ready ?

  • Not yet, but I’ll arrange

  • I’ve already inserted PHP

Show 2 more comments

1 answer

0


I managed to solve my problem. Despite the lack of interest in helping and the two votes as not clear or insignificant question, 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);

?>

Browser other questions tagged

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