dongmaqiu6084 2018-10-30 04:38
浏览 115

选择类别和相关品牌产品未显示在我的电子商务网站上

I am creating a small e-commerce website.

  • If I click the category the relevant products displayed successfully.
  • If I click the brand relevant product brand displayed successfully so far did.

My problem is if I click the category as TV and choose the brand as LG the relevant product should display. but I couldn't do the stuff what I tried so I attached below.

Category

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)
                }

            });
        }

Brand

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);
        }
    });
}

Click Event of category and brand

$( document ).ready(function()
    {
    console.log( "ready!" );
     $('#categories a.list-group-item').click(function() {
        var cid = $(this).attr('cid');
       get_product(cid);
    });
        $('#brands a.list-group-item').click(function() {
            var bid = $(this).attr('bid');
            get_product(bid);
        });

     });

Products

function get_product(cid,bid){

    $.ajax({
        type: 'post',
        url: 'get_product.php' ,
        data: {cid:cid,bid:bid},
        dataType: 'JSON',
        success: function (data) {
            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>");
            }
        },
        error: function (xhr, status, error) {
            alert(xhr.responseText);
        }
    });
        }

get_product.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 = ? order by RAND() LIMIT 0,6");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);

 $cid = $_POST["cid"];
$bid = $_POST["bid"];
$stmt->bind_param("s", $cid);
$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();
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?
    • ¥15 matlab(相关搜索:紧聚焦)
    • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
    • ¥15 路易威登官网 里边的参数逆向
    • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
    • ¥50 需求一个up主付费课程