简体   繁体   中英

Loop through looped categories

I have categories stored in a table and products in another. I have a loop to generate a bootstrap tab html layout.

There's my categories loop PHP code:

<ul id="myTabs" role="tablist" class="grid-construct nav nav-tabs nav-justified">
 <?php foreach($this->categories as $i => $category){
    <li role="presentation" class="">
      echo $category->alias;?>
    </li>
    }
 </ul>

I have 3 products in each category. Bearing in mind I'm going for a bootstrap tab layout approach, How can i display the through products while looping into each category?

You could try to join the product table in the query where you select the categories. Then you can change your code to:

<ul id="myTabs" role="tablist" class="grid-construct nav nav-tabs nav-justified">
    <?php foreach($this->categories as $i => $category): ?>
    <li role="presentation" class="">
      <?php echo $category->alias; ?>
      <?php foreach($category->products as $product): ?>
        <?php echo $product->name; ?>
      <? endforeach; ?>
    </li>
    <?php endforeach; ?>
</ul>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM