我在foreach中制作了一个foreach,创建了一个类别div,其中包含与其相关的FAQ问题。现在我想隐藏现有的没有相关常见问题解答的类别。我该怎么做?
查看:
<?php foreach($faqtypetexts as $faqtypetext):?>
<div class="faqcatdiv">
<h1><?php echo $faqtypetext['Faqtypetext']['faqtypetext_name']; ?></h1>
<?php foreach($faqtexts as $faqtext):?>
<?php if($faqtypetext['Faqtype']['faqtype_id'] == $faqtext['Faq']['faq_rel_faqtype']): ?>
<section class="faq-section">
<input type="checkbox" id="q1">
<label for="q1"><?php echo $faqtext['Faqtext']['faqtext_question']; ?></label>
<p>????</p>
<p><?php echo $faqtext['Faqtext']['faqtext_awnser']; ?></p>
</section>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
控制器功能:
public function faq() {
// load the website base-layout
$this->layout = 'website';
// load relevant models
$this->loadModel('Faqtext');
$this->loadModel('Faqtypetext');
// haal relevante snippets op
$activelanguage = 2;
$faqtexts = $this->Faqtext->find('all', array('conditions' => array('Faq.faq_online' => '1', 'Language.language_id' => '2')));
$faqtypetexts = $this->Faqtypetext->find('all', array('conditions' => array('Language.language_id' => '2')));
// send snippets to screen
$this->set('faqtexts', $this->Faqtext->find('all', array('conditions' => array('Faq.faq_online' => '1', 'Language.language_id' => '2'))));
$this->set('faqtypetexts', $this->Faqtypetext->find('all', array('conditions' => array('Language.language_id' => '2'))));
// haal relevante contentitems op
// send content to screen
// haal nieuws uit de database
// send to screen
}
答案 0 :(得分:2)
三个选项:
哦,请处理你的代码格式化风格,这是可怕的来阅读。
答案 1 :(得分:0)
为了完成这项工作,我必须使用我需要的信息来压缩我必须使用的数组。我用classicExtract
做了这个//get all FAQs
$faqs = $this -> Faq -> find('all', array(
'conditions' => array(
'Faq.faq_online' => 1
)
));
$this->set('faqs', $faqs);
debug($faqs);
//get all faqtexts
$faqtypetexts = $this -> Faqtypetext -> find('all', array(
'conditions' => array(
'Faqtypetext.faqtypetext_rel_language' => $activelanguage
),
'contain' => false
));
$this->set('faqtypetexts', $faqtypetexts);
//make list more compact
$categories = Set::classicExtract($faqtypetexts, '{n}.Faqtypetext.faqtypetext_rel_faqtype');
$this->set('categories', $categories);
对于视图,我创建了一个变量,当循环通过类别时,该变量为空。如果变量为空,则允许回显类别名称,当它再次循环并且变量填充与之前相同的值时,不允许回显类别名称。一旦类别名称的值发生变化,就可以回显类别名称。
<!-- - - - - - - - - - - Repeatable FAQ Div - - - - - - - - - - -->
<?php $lastcategory = ''; ?>
<!-- - - - - - - - - - - Category Title - - - - - - - - - - -->
<?php foreach ($faqs as $faq):
$faqtypekey = $faq['Faq']['faq_rel_faqtype'];
//$categories is result from classicExtract
$categoryPosition= array_search($faqtypekey, $categories);
$categoryRecord = $faqtypetexts[$categoryPosition];
$actualcategory = $categoryRecord['Faqtypetext']['faqtypetext_name']; ?>
<?php if($actualcategory != $lastcategory){ ?>
<center>
<table border="0" width="85%" cellpadding="0" cellspacing="0" >
<img src="<?php echo FULL_BASE_URL . $this->Html->webroot . 'pics/' . $pixelimage['Image']['image_img']; ?>" height="20" width="1">
<tr>
<td bgcolor="#FFFFFF">
<div class="categorytitle"><?php echo $actualcategory; ?></div>
</td>
</tr>
</table>
</center>
<?php } ?>
<!-- - - - - - - - - - - FAQ Content - - - - - - - - - - -->
<?php $faqtexts = $faq['Faqtext']; ?>
<?php foreach ($faqtexts as $faqtext):
if ($faqtext['faqtext_rel_language'] == 2) { ?>
<center>
<table border="0" width="85%" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#FFFFFF">
<section class="faq-section" style="margin: 20px 0 0 5px; position:relative;">
<input type="checkbox" id="q1">
<h4 style="color:#925050;" for="q1"><b><span style="font-weight: lighter;">Q: </span>
<?php echo $faqtext['faqtext_question']; ?></b></h4>
<?php echo $faqtext['faqtext_awnser']; ?>
</section>
</td>
</tr>
<?php } ?>
</table>
</center>
<!-- - - - - - - - - - - End of file - - - - - - - - - - -->
<?php endforeach;
$lastcategory = $actualcategory;
endforeach; ?>
对不起,awnser帖子花了这么长时间,但我不得不将所有div重写成表格。