类别和品牌同时在php中搜索

时间:2018-11-15 13:53:05

标签: php jquery ajax

我正在创建一个电子商务网站。例如,如果我单击“电视”类别和“ LG”品牌,则相关产品应显示。但效果不佳。未显示错误 我到目前为止尝试过的内容写在下面。

表单设计

类别

<div align="left">
  <li class="list-group-item list-group-item-action active" > 
   <h4>Category</h4></li>
 <li class="list-group-item list-group-item-action" id="categories"></li>
  </div>

品牌

<div align="left" >
    <li class="list-group-item list-group-item-action active" ><h4>Brand</h4></li>
        <li class="list-group-item list-group-item-action" id="brands"></li>
</div>

产品

   <div class="panel-heading">
                Products
            </div>
            <div class="panel-body" id="Products" >

            </div>

jquery

类别

 function getCategory(){
        $.ajax({
            type: 'GET',
            url: 'get_category.php' ,
            dataType: 'JSON',
            success: function (data)
            {
                for (var i = 0; i < data.length; i++)
                {
                    var catname = data[i].catname;
                    var id = data[i].id;
                    $('#categories').append('<a href="#" cid= '+ id + '  class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');
                }
            },
            error: function (xhr, status, error)
               {
                console.log(xhr.message)
            }

        });
    }

品牌

   function getBrand(){
        $.ajax({
            type: 'GET',
            url: 'all_brand.php' ,
            dataType: 'JSON',
            success: function (data) {
                console.log(data);
                for (var i = 0; i < data.length; i++)
                {
                    var id1 = data[i].id;
                    $('#brands').append('<a href="#"  bid= '+ id1 + '  class="list-group-item list-group-item-action">' + '<b>'+ data[i].brand_name + '<b>' + '</li>');
                }
            },
            error: function (xhr, status, error) {
                alert(xhr.responseText);
            }
        });
    }

类别和品牌成功对开。

产品

如果我单击当时显示相关产品的类别和品牌。这就是我写查询的方式

$(document).ready(function() {
    $("#categories a.list-group-item").click(function () {
        var cat = $(this).attr('cid');


        var brand = $("#brands a.list-group-item").click();
        var brand = $(this).attr('bid');
        $.ajax({
            url: 'get_search.php',
            type: 'post',
           data: {cat: cat, brand: brand},
          //  data: {cat: cat},
            dataType: 'json',
            success: function (data) {
                var len = data.length;
                console.log(data);
                $("#Products").empty();
                for (var i = 0; i < data.length; i++) {
                    var price = data[i].price;
                    var image = data[i].image;
                    var description = data[i].description;
                    $("#Products").append("<div class='col-md-4'> " +
                    "<div class='panel panel-info' id='Products'>" +
                    "<div class='card-body'>" +
                    "<div class='panel-heading'>" + "<h4> " + description + "</h4> " +
                    "<p class='panel-body'>" + "<h3> " + price + "</h3>" +
                    "<p class='panel-body'> <img class='card-img-top' style='width:250px' height='250px' id='theImg' src='images/" + image + "' /> </p>" +
                    " <a href='#' class='btn btn-primary'>View More</a> </div> </div></div> </div>");
                }
            }
        });
    });
});

get_search.php

   <?php
include("db.php");

$stmt = $conn->prepare("SELECT id,cat_id,brand_id,price,description,image,keywords from products WHERE cat_id = ? AND brand_id = ? ");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);

$cat_id = $_POST["cat"];
$brand = $_POST['brand'];
$stmt->bind_param("ss", $cat_id,$brand);
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);



if ($stmt->execute()) {
    while ( $stmt->fetch() ) {
        $output[] = array ("id"=>$id, "cat_id"=>$cat_id,"brand_id"=>$brand_id,"price"=>$price,"description"=>$description,"image"=>$image,"keywords"=>$keywords);
    }

    echo json_encode( $output );
}
$stmt->close();


?>

1 个答案:

答案 0 :(得分:1)

我知道我不喜欢mysqli是有原因的。建议您改用PDO,下面的代码将为您提供所需的结果。构建数组以JSON格式返回的操作留给您完成。

$conn = new PDO('mysql:host=localhost;dbname=test;charset=utf8mb4','username','password');

$_POST["cat"]   = 'TV';
$_POST['brand'] = 'LG';

$stmt = $conn->prepare('SELECT id,description from products
                        WHERE cat_id LIKE :catid AND brand_id LIKE :brand');

$cat_id = '%' . $_POST["cat"]   . '%';
$brand  = '%' . $_POST['brand'] . '%';

$stmt->execute([':catid' => '%' . $cat_id . '%',
                ':brand' => '%' . $brand  . '%']);
$results = $stmt->fetchAll(PDO::FETCH_ASSOC);
print_r($results);