PHP显示产品按类别

时间:2020-08-27 10:07:13

标签: php arrays json

我想要的是显示下面所有ID为1的产品。 正在显示所有产品,但每个产品要循环显示3次类别。 一个类别,每个类别分别包含持有人和所有产品 这是我的代码:

$query_cat  = query("SELECT * FROM categories");
$query  = query("SELECT * FROM products WHERE product_category_id = '1'");
confirm($query_cat, $query);
$cat = fetch_array($query_cat);


while(($row = fetch_array($query))){
$category[] =   array(

        'category_id'=>$cat['cat_id'],
        'category_title'=>$cat['cat_title'],    
        
        'each' => $item[]  =    array (

        'product_id'=>$row['product_id'],
        'name'=>$row['product_title'],
        'product_tags'=>$row['product_category'],
        'partner_name_title'=>$row['partner_name_title'],
        'product_category_id'=>$row['product_category_id'],
        'image'=> $produc_img,
        'price'=>$row['product_price'],
        'discount'=>$row['product_discount'],
        'from_date'=>$row['start_discount'],
        'to_date'=>$row['finish_discount'],

        'sold'=>$row['counter_sold'],
        'desc'=>$row['product_about'],
        'location_address'=>$row['product_address'],
        'location'=>$row['product_location'],
        'partner_phone'=>$row['product_phone'],
        'partner_email'=>$row['product_email'],
        'partner_open_time'=>$row['product_open_time'],
        'partner_website'=>$row['product_website'],
        'instagram_page'=>$row['instagram_page'],
        'facebook_page'=>$row['facebook_page'],
        'product_prev'=>$row['product_prev'],
        'exp'=>$row['expire']

        )

    );
}

}
header("Content-Type: application/json");

echo json_encode(array('pro'=>$category), JSON_PRETTY_PRINT);

输出:

{
"pro": [
    {
        "category_id": "1",
        "category_title": "Food & Drink",
        "each": {
            "product_id": "84",
            "name": "Night Diner",
            "product_tags": "appetisers, food , health  food, amazing , good .",
            "partner_name_title": "Apantisi",
            "product_category_id": "1",
            "image": null,
            "price": "1",
            "discount": "-30%",
            "from_date": "2020-07-25",
            "to_date": "2020-12-25",
            "sold": "0",
            "desc": "Our restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   \r\nOur restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   \r\nOur restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   ",
            "location_address": "Glyfu 5 a",
            "location": "Nicosia",
            "partner_phone": "+357 97 832 983",
            "partner_email": "apantisi@gmail.com",
            "partner_open_time": "12:00-22:00",
            "partner_website": "www.apantisi.com.cy",
            "instagram_page": "instagram.com\/apantisi",
            "facebook_page": "facebook.com\/apantisi",
            "product_prev": "This coupon gives you discount -30%  on any reserved table in our restaurant .",
            "exp": "no"
        }
    },
    {
        "category_id": "1",
        "category_title": "Food & Drink",
        "each": {
            "product_id": "85",
            "name": "Princess Hair ",
            "product_tags": "hair , extensions hair, beauty , saloon   ",
            "partner_name_title": "Princess Hair",
            "product_category_id": "1",
            "image": null,
            "price": "1",
            "discount": "-50%",
            "from_date": "2020-07-25",
            "to_date": "2020-12-25",
            "sold": "2",
            "desc": "is the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  ",
            "location_address": " 1 Michail Georgalla Street",
            "location": "Nicosia",
            "partner_phone": "+357 97 832 983",
            "partner_email": "hair@gmail.com",
            "partner_open_time": "8:00-15:00 ",
            "partner_website": "www.hair.com.cy",
            "instagram_page": "instagram.com\/.hair",
            "facebook_page": "instagram.com\/.hair",
            "product_prev": "We offer you discount with -50% on our product bt code from coalaboom \/ use the chance to come with the code by  issue date ",
            "exp": "no"
        }
    }

但是我需要的是:

  "pro": [
    {
        "category_id": "1",
        "category_title": "Food & Drink",
        "each": {
            "product_id": "84",
            "name": "Night Diner",
            "product_tags": "appetisers, food , health  food, amazing , good .",
            "partner_name_title": "Apantisi",
            "product_category_id": "1",
            "image": null,
            "price": "1",
            "discount": "-30%",
            "from_date": "2020-07-25",
            "to_date": "2020-12-25",
            "sold": "0",
            "desc": "Our restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   \r\nOur restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   \r\nOur restaurant proposing you to spend amazing  time with you family get the taste  of Caribbeans dishes   ",
            "location_address": "Glyfu 5 a",
            "location": "Nicosia",
            "partner_phone": "+357 97 832 983",
            "partner_email": "apantisi@gmail.com",
            "partner_open_time": "12:00-22:00",
            "partner_website": "www.apantisi.com.cy",
            "instagram_page": "instagram.com\/apantisi",
            "facebook_page": "facebook.com\/apantisi",
            "product_prev": "This coupon gives you discount -30%  on any reserved table in our restaurant .",
            "exp": "no"
        },
       {
        "product_id": "85",
        "name": "Princess Hair ",
        "product_tags": "hair , extensions hair, beauty , saloon   ",
        "partner_name_title": "Princess Hair",
        "product_category_id": "1",
        "image": null,
        "price": "1",
        "discount": "-50%",
        "from_date": "2020-07-25",
        "to_date": "2020-12-25",
        "sold": "2",
        "desc": "is the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  \r\nis the wonderful saloon hair for you we offer you best quality of hair  ",
        "location_address": " 1 Michail Georgalla Street",
        "location": "Nicosia",
        "partner_phone": "+357 97 832 983",
        "partner_email": "hair@gmail.com",
        "partner_open_time": "8:00-15:00 ",
        "partner_website": "www.hair.com.cy",
        "instagram_page": "instagram.com\/.hair",
        "facebook_page": "instagram.com\/.hair",
        "product_prev": "We offer you discount with -50% on our product bt code from coalaboom \/ use the chance to come with the code by  issue date ",
        "exp": "no"
    }

    }

我想按类别1显示商品

1 个答案:

答案 0 :(得分:0)

将您的while替换为:

$category = [
    'category_id'=>$cat['cat_id'],
    'category_title'=>$cat['cat_title'],
    'each' => [],
];
while(($row = fetch_array($query))){
    $category['each'][] = [
        'product_id'=>$row['product_id'],
        // ... rest of $row attributes
    ];
}

有了这个类别,每个产品的each键中就会有一个条目。