我有一个用于存储常见问题解答类别的表,然后是一个用于存储常见问题解答的表,所以现在当我必须显示常见问题解答时,我首先遍历这些类别,然后获取该类别下的所有常见问题解答。
我希望输出像,
Category 1
FAQ1, FAQ2...
Category 2
FAQ3, FAQ4
有没有更好的方法呢?重新划分性能,比如在获取类别时使用JOIN,一起获取每个类别下的常见问题解答,所以我只需要运行一个查询。
我的代码:
<?php
if($cache->hasItem('faq_categories'))
{
$categories = $cache->getItem('faq_categories');
}
else
{
$categories = getRows(array(
'table' => 'faq_categories',
'where' => array('is_active' => 1)
));
$cache->setItem('faq_categories', $categories);
}
?>
<div class="row">
<div class="col-md-12">
<div class="globalBox">
<div class="globalBoxInside">
<h2 class="mainTitle">FAQs</h2>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<?php $c = 1; foreach($categories as $cat) : ?>
<?php
$faqs = getRows(array(
'table' => 'faqs',
'where' => array('faq_category_id' => $cat['id'])
));
?>
<div class="faq_category">
<h4 class="faq_cat_title"><?php echo $cat['title']; ?> (<?php echo $cat['questions_count']; ?>)</h4>
<ul class="faqs_questions_list row">
<?php foreach($faqs as $faq) : ?>
<li class="col-md-6"><i class="fa fa-dot-circle-o"></i> <a href="<?php echo getURI('faqs'); ?>/<?php echo encryptParam('CA' . $cat['id'] . '-' .$faq['id']); ?>"><?php echo $faq['question']; ?></a></li>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
</div>
</div>
</div>
感谢您的回答,谢谢。
答案 0 :(得分:0)
您可以选择所有类别并循环选择每个类别的常见问题解答...
伪pythonesque代码:
categories = execute('select id, name from categoryTable order by name')
for category in categories:
print category.name
faqs = execute('select question, answer from faqTable where categoryId=' + category.id)
for faq in faqs:
print faq.question + ' - ' + faq.answer