【问题标题】:Generate html according to category through php or jquery通过php或jquery按类别生成html
【发布时间】:2016-02-01 07:55:35
【问题描述】:

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

在数据库中,我有两个表productproduct_category 都与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。

【问题讨论】:

  • 那么,问题出在哪里?
  • 已更新,请查看
  • 抱歉,So 不是“为我编写代码”网站。向我们展示您尝试过的内容以及您遇到的问题
  • 我不是要你为我写代码,我是在问提示或逻辑......我可以通过这个 json 生成像我的图像一样的 html

标签: php jquery html mysqli


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-09-27
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    相关资源
    最近更新 更多