Выберите категорию и соответствующий товар бренда, который не отображался на моем сайте электронной коммерции. - PullRequest
0 голосов
/ 30 октября 2018

Я создаю небольшой сайт электронной коммерции.

  • Если я нажимаю на категорию, соответствующие продукты отображаются успешно.
  • Если я нажимаю на марку, соответствующая марка товара, отображаемая до сих пор, успешно сделала.

Моя проблема в том, что если я выберу категорию телевизора и выберу марку LG, соответствующий продукт должен отображаться. но я не смог сделать то, что попробовал, поэтому приложил ниже.

Категория

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

     });

Продукты

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();
?>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...