PHP中的多级for循环 [英] Multi level for loop in PHP

查看:156
本文介绍了PHP中的多级for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表侧栏,其中包含以下中包含4个不同的条目

I have a table sidebar which contains the following

我要创建一个Accordian引导程序,其标题从sidebar_category获取,并且body部分包含所有带有与accodian标题相似的sidebar_category的sidebar_title。我想使所有不同的sidebar_category值的并发。

i want to create an Accordian in bootstrap whose title is fetched from sidebar_category and the body part contains all the sidebar_title with the similar sidebar_category to that on the accodian title. I want to make accordian of all the distinct sidebar_category values.

如何使用foreach循环,因为我使用Codeigniter。
我想让代码如下:

How can this be achieved using foreach loop, as i'm using Codeigniter. I want the code to go like:

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
          <div class="panel panel-danger">
            <div class="panel-heading" role="tab" id="headingOne">
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-controls="collapseOne">
                  //sidebar_Category Should go here
                </a>
              </h4>
            </div>
            <div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
              <div class="panel-body">
              <h5><a href="#">//Sidebar_title should go here</a></h5>
              </div>
            </div>
          </div>

          <div class="panel panel-warning">
            <div class="panel-heading" role="tab" id="headingTwo" class="active">
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseTwo" aria-controls="collapseTwo">
                 //Sidebar_category should go here
                </a>
              </h4>
            </div>
            <div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
              <div class="panel-body">
                  <h5><a href="#">//Sidebar_Title Goes Here</a></h5>
              </div>
            </div>
          </div>
</div>

我可以尝试这样:
1)使用这个不同的SQL Query

I could try this: 1) Either use this distinct SQL Query

SELECT DISTINCT `sidebar_category` FROM `sidebar`

将从表中给出不同的sidebar_category值。让我们假设它为4.这可以用于外部for循环迭代以显示accordian标题的类别。另一个查询将简单地提取整个表。我在这里面临的问题是我如何识别为下一个sidebar_category运行另一个循环

that will give me distinct sidebar_category values from the table. Lets assume it to be 4. This could be used for outer for loop iteration to display the categories at the accordian title. The other query would simply fetch the whole table. The problem i'm facing here is how will i identify that run another loop for the next sidebar_category

推荐答案

我做出了一个逻辑,完全适合我的需要。数据被适当地传播。但是默认情况下,accordian面板不会折叠。我为此提出了一个新问题。 href=\"http://stackoverflow.com/q/32557357/2303664\">链接

After lots of thinking, i've made a logic that suits my need perfectly. The data gets spreaded in the accordian properly. But the accordian panels are not collapsed by default. I'm making a new question for that. Heres the Link

 <?php 
            $fistForLoopEnds = false;
            $secondForLoopEnds = false;
            $thirdForLoopEnds = false;
            $fourthForLoopEnds = false;
             ?>
          <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
           <?php foreach ($this->data2['distinct'] as $key) { ?>

          <div class="panel panel-<?php if ($fistForLoopEnds != true) {
              echo "danger";
              $panelId = "headingOne";
              $accodianId = "collapseOne";
              $accordionExpanded = "true";
          } elseif ($fistForLoopEnds == true && $secondForLoopEnds != true) {
              echo "warning";
              $panelId = "headingTwo";
              $accodianId = "collapseTwo";
              $accordionExpanded = "false\" class=\"collapsed";
          } elseif ($fistForLoopEnds == true && $secondForLoopEnds == true && $thirdForLoopEnds != true) {
              echo "info";
              $panelId = "headingThree";
              $accodianId = "collapseThree";
              $accordionExpanded = "false\" class=\"collapsed";
          } else { echo "success";
              $panelId = "headingFour";
              $accodianId = "collapseFour"; 
              $accordionExpanded = "false\" class=\"collapsed";
              }?>">
            <div class="panel-heading" role="tab" id="<?php echo $panelId; ?>">
              <h4 class="panel-title">
                <a role="button" data-toggle="collapse" data-parent="#accordion" href="#<?php echo $accodianId; ?>" aria-expanded="<?php echo $accordionExpanded; ?>" aria-controls="<?php echo $accodianId; ?>">
                  <?php echo $key->sidebar_category; ?>
                </a>
              </h4>
            </div>
            <div id="<?php echo $accodianId; ?>" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="<?php echo $panelId; ?>">
              <div class="panel-body">
            <?php 
                if ($fistForLoopEnds != true) {
                    foreach ($this->data2['results_railways'] as $key) { ?>
                        <h5><a href="<?php echo base_url(); ?>home/railways/<?php echo $key->post_id; ?>"><?php echo $key->sidebar_title; ?></a></h5>
                    <?php }
                    $fistForLoopEnds = true;
                    goto a;
                } 
                elseif ($secondForLoopEnds != true) {
                    foreach ($this->data2['results_ssc'] as $key) { ?>
                         <h5><a href="<?php echo base_url(); ?>home/ssc/<?php echo $key->post_id; ?>"><?php echo $key->sidebar_title; ?></a></h5>
                     <?php  }
                    $secondForLoopEnds = true;
                    goto a;
                }
                elseif ($thirdForLoopEnds != true) {
                    foreach ($this->data2['results_banks'] as $key) { ?>
                         <h5><a href="<?php echo base_url(); ?>home/banks/<?php echo $key->post_id; ?>"><?php echo $key->sidebar_title; ?></a></h5>
                     <?php  }
                    $thirdForLoopEnds = true;
                    goto a;
                }
                elseif ($fourthForLoopEnds != true) {
                    foreach ($this->data2['results_upsc'] as $key) { ?>
                         <h5><a href="<?php echo base_url(); ?>home/upsc/<?php echo $key->post_id; ?>"><?php echo $key->sidebar_title; ?></a></h5>
                     <?php  }
                    $fourthForLoopEnds = true;
                    goto a;
                }
                a:
             ?>
              </div>
            </div>
          </div>
           <?php } ?>
          </div>

这篇关于PHP中的多级for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆