通过php或jquery根据类别生成html

时间:2015-11-01 08:54:06

标签: php jquery html mysqli

我正面临一个问题,就是从我的网络服务中显示这样的html。

a

在数据库中,我有两个表productproduct_category都与product.category_id=product_category.id之类的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。

1 个答案:

答案 0 :(得分:0)

你应该生成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.
}