php-仅从foreach循环中回显一个类别

时间:2018-07-02 10:16:39

标签: php mysql sql html5


我知道这是一个奇怪的问题。
但我想知道
如果我有这样的foreach循环。
并且我想显示类别和FAQ问题(使用bootstrap Grid System)
而不会循环类别
并且不显示任何一条记录。
我该怎么办?
这是我的代码:

            $stmt = $con->prepare("SELECT * FROM `FAQs` ORDER BY `ID` DESC");
            $stmt->execute();
            $rows   = $stmt->fetchAll();
            foreach ($rows as $row) {
                echo '<article class="row cat-' . $row["Category"] . '"><section class="FAQ-category col-lg-6 col-md-6 col-sm-12 col-xs-12"><h3 class="h1">' . $row["Category"] . '</h3></section><section class="FAQ-Questions col-lg-6 col-md-6 col-sm-12 col-xs-12"><div class="panel-group" id="faqAccordion-' . $row["ID"] . '"><div class="panel panel-default"><div class="panel-heading accordion-toggle question-toggle collapsed" data-toggle="collapse" data-parent="#faqAccordion-' . $row["ID"] . '" data-target="#question-' . $row["ID"] . '"><h4 class="panel-title"><a href="#faqAccordion-' . $row["ID"] . '"class="ing">' . $row["Question"] . '</a></h4><i class="fa fa-fw fa-chevron-down" aria-hidden="true"></i></div><div id="question-' . $row["ID"] . '" class="panel-collapse collapse" style="height: 0px;"><div class="panel-body"><p class="lead">' . $row["Answer"] . '</p></div></div></div></div></section></article>';
            }

编辑: 对不起,我很抱歉
我的意思是喜欢这张照片。 Like This

编辑: 为了更清晰
我的代码正在输出此内容!
虽然我希望输出像第一个img(没有类别重复)  ) 我的意思是,如果它们具有相同的类别,请不要重复该类别并将其排序为第一个图像。 enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

你是说那样吗?

<div class="panel-group" id="faqAccordion">
  <?php foreach ($rows as $row) : ?>
    <article class="row cat-<?= $row['Category']; ?>">
      <section class="FAQ-category col-sm-6 col-xs-12">
        <h3><?= $row['Category']; ?></h3>
      </section>
      <section class="FAQ-Questions col-sm-6 col-xs-12">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h4 class="panel-title">
              <a href="#question-<?= $row['ID']; ?>" data-toggle="collapse" data-target="#question-<?= $row['ID']; ?>">
                <?= $row['Question']; ?>
              </a>
            </h4>
          </div>
          <div id="question-<?= $row['ID']; ?>" class="panel-collapse collapse">
            <div class="panel-body">
              <p class="lead"><?= $row['Answer']; ?></p>
            </div>
          </div>
        </div>
      </section>
    </article>
  <?php endforeach; ?>
</div>

编辑:确定,然后按照索引{Category”对数组进行分组,如here所述:

function groupByIndex($array, $index) {
    $result = array();
    foreach($array as $val) {
        $result[$val[$index]][] = $val;
    }
    return $result;
}

$faqsByCategory = groupByIndex($rows, 'Category');

然后,您可以在2个嵌套循环中输出常见问题解答,如下所示:

<?php foreach ($faqsByCategory as $category => $faqs) : ?>
<article class="row cat-<?= $category ?>">
  <section class="FAQ-category col-sm-6">
    <h3><?= $category; ?></h3>
  </section>
  <section class="FAQ-Questions col-sm-6">
    <div class="panel-group">
    <?php foreach ($faqs as $key => $faq): ?>
      <div class="panel panel-default">
        <div class="panel-heading">
          <h4 class="panel-title">
            <a href="#question-<?= $faq['ID']; ?>" data-toggle="collapse" data-target="#question-<?= $faq['ID']; ?>">
              <?= $faq['Question']; ?>
            </a>
          </h4>
        </div>
        <div id="question-<?= $faq['ID']; ?>" class="panel-collapse collapse">
          <div class="panel-body">
            <p class="lead"><?= $faq['Answer']; ?></p>
          </div>
        </div>
      </div>
    <?php endforeach; ?>
    </div>
  </section>
</article>
<?php endforeach; ?>