嵌套的foreach循环类别和产品

时间:2019-08-06 15:22:34

标签: php mysql foreach

我有下表:

类别: category_id,category_name

enter image description here

产品: id,category_id,product_name

enter image description here

我正在使用引导程序4,我想将每个类别显示为手风琴标题,并在手风琴主体中列出属于该类别的所有产品。

使用php 7

    function getProducts(){
        $db = new DB();
        $rows = $db->db_select("SELECT category.category_id, category.category_name, products.id, products.category_id, products.product_name
                FROM products 
                INNER JOIN category ON products.id = category.category_id 
                ORDER BY category.category_name 
                ");
        if($rows === false){
            $error = db_error();
        } else {
            return $rows;
        }
    }   

$data = new CORE();
$products = $data->getProducts();



<div class="accordion" id="accordionExample">
<? foreach($products as $p){ ?>
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          <? echo $p['category_name'] ?>
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        <? echo $p['product_name'] ?>
      </div>
    </div>
  </div>
<? } ?>
</div>

数据库数据:

3 个答案:

答案 0 :(得分:1)

您可以将查询更改为以下内容:

SELECT category.category,GROUP_CONCAT(products.id, '|', products.name ) AS product FROMINNER JOIN产品ON category.category_id=products.category_id GROUP BY category.category

它会给您类似的结果:


category product Hats 8|Snapback,9|Fitted Pants 6|Jogging,7|Cut offs Shirts 1|Polo,2|Long Sleeve,3|Vneck Shoes 5|Boots,4|Converse


那么您要做的就是在foreach数组上的productsplit/explode数据并显示它。首先用逗号split/explode ,,然后用管道split/explode的每个结果|。您仍然需要2个foreach,但需要执行一次SQL查询。

答案 1 :(得分:0)

首先,我需要两个函数来按如下所示获取类别和产品。

function getCategories(){
        $db = new DB();
        $rows = $db->db_select("SELECT category.category_id, category.category_name FROM Categories");
        if($rows === false){
            $error = db_error();
        } else {
            return $rows;
        }
    }   
    function getProducts(){
        $db = new DB();
        $rows = $db->db_select("SELECT products.id, products.category_id, products.product_name
                FROM products ");
        if($rows === false){
            $error = db_error();
        } else {
            return $rows;
        }
    }   

$data = new CORE();
$categories = $data->getCategories();
$products = $data->getProducts();

然后我在foreach循环中递归地使用这些数组。关键是在这里我使用了两个1foreach`循环,第一个循环一直等到第二个循环(内部)完成该过程。

//Take the first value of array and then second value and so on.

     foreach($Categories as $Category){

     echo $Category['name'];

//Take the first value of array and then second value and so on.

foreach($Products as $Product){

//Check the current category id with current product id. In here **category id dosent get changes untill products foreach loop completed**.

if( $Category['category_id'] == $Product['category_id'] ) {

  echo $Product['name'];
}
}
}

答案 2 :(得分:0)

如果将商品归类到类别,请尝试此查询

$this->db->join('products','products.category_id = categories.category_id')
->order_by('categories.category_id')->get('categories')->result_array();