gpt4 book ai didi

php - 通过php或者jquery根据category生成html

转载 作者:行者123 更新时间:2023-11-28 03:03:48 25 4
gpt4 key购买 nike

我在从我的网络服务中显示这样的 html 时遇到了问题。

a

在数据库中,我有两个表 productproduct_category 都与 id 相关,例如 product.category_id=product_category.id。我正在使用 mysqli 和 php。

PHP:

function get_product_by_category(){
$query = "SELECT * from product p JOIN product_category pc ON pc.id = p.category_id";
$result = $this->fetch_all($query);
if($result>0){
$data = json_encode($result);
}else{
$data = json_encode(array("status"=>false));
}
echo $data;
}

JSON:

[
{
"id": "1",
"category_id": "1",
"product_name": "Plain Shoes",
"product_price": "10000",
"product_discount": "20",
"valid_upto": "2015-10-10",
"size": "Small,Medium",
"color": "Brown,Black",
"image": "1,2,3",
"caption": "behtreen",
"description": "alal",
"details": "lush",
"created_date": "2015-10-31 22:14:44",
"category_name": "Shoes"
},
{
"id": "2",
"category_id": "2",
"product_name": "Kurta",
"product_price": "2000",
"product_discount": "10",
"valid_upto": "2015-10-10",
"size": "Small",
"color": "Blue,Sky",
"image": "1,2,3",
"caption": null,
"description": null,
"details": null,
"created_date": "2015-10-31 22:56:53",
"category_name": "Dress"
},
{
"id": "1",
"category_id": "1",
"product_name": "Shoes",
"product_price": "9000",
"product_discount": "40",
"valid_upto": "2015-10-10",
"size": null,
"color": null,
"image": null,
"caption": null,
"description": null,
"details": null,
"created_date": "2015-10-31 23:07:46",
"category_name": "Shoes"
}
]

现在我想要这个 json 来创建像我的图像一样的 html。

最佳答案

你应该生成 html,使用这样的东西:

foreach ($data as $product){
echo '<div class="product_name">' . $product['product_name'] . '</div>';
echo '<div class="product_discount">' . $product['product_discount'] . '</div>';
if (isset($product['size'])) {
echo '<div class="size">' . $product['size'] . '</div>';
}
// all other html elements that you need.
}

关于php - 通过php或者jquery根据category生成html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33460574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com