简体   繁体   English

产品未在电子商务网站上显示

[英]Product didn't diplay on ecommerce website

i created the e-commerce website. 我创建了电子商务网站。 category and brands both are displayed successfully. 类别和品牌都成功显示。 my problem is if I click the category. 我的问题是,如果我单击类别。 the product should display based on the category what I clicked. 产品应根据我点击的类别显示。 Example: if I click Tv as a category all tv product should display.like that 例如:如果我单击电视作为类别,则所有电视产品都应显示。
what i tried so far. 到目前为止我尝试过的。 i attached the code below.when i running the programming and click the category i got the error as "Uncaught ReferenceError: cid is not defined" 我附上了下面的代码。运行程序并单击类别时,出现错误,提示为“ Uncaught ReferenceError:未定义cid”

This code I wrote for display category but all category displayed successfully. 我为显示类别编写的代码,但所有类别均已成功显示。

 $('#categories').append('<a href="#" cid= '+ id + '  class="list-group-item list-group-item-action">' + '<b>'+ data[i].catname + '<b>' + '</a>');

Full code for category 类别的完整代码

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

            });
        }


                    }

when i click the category the relevant product should be displayed I wrote the code below 当我单击类别时,应该显示相关产品,我在下面编写了代码

$("#categories").click(function ()
{
$.ajax({
    type: 'post',
    url: 'get_product.php' ,
    data: {cid:cid},
    dataType: 'JSON',
    success: function (data) {
        console.log(data);

        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 page get_product.php页面

<?php
include("db.php");
$stmt = $conn->prepare("select id,cat_id,brand_id,price,description,image,keywords from products where id = ? order by RAND() LIMIT 0,6");
$stmt->bind_result($id,$cat_id,$brand_id,$price,$description,$image,$keywords);

$cid = $_POST["cid"];
$stmt->bind_param("s", $cid);


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();
?>

I think this gives you error, you need to get clicked category id 我认为这会给您带来错误,您需要获取点击的类别ID

var cid = $(this).attr('cid'); // Use this and try
 data: {cid:cid},

You are calling click event on categories; 您正在click类别调用click事件; you should call click event on a tag; 你应该调用click事件a标签;

Replace you click event; 替换您的点击事件;

$("#categories").click(function ()
{ 
// You code here
}

with this code; 用这个代码;

$("a.list-group-item").click(function ()
{ 
var cid = $(this).attr('cid');
// You code here
}

Hope this will be useful for you! 希望这对您有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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