I’m currently trying to build a FAQ page where all the faq is inside their own category as shown in the image. I’m using a foreach which loads all the faq data which are the accordions in the image but what i’m struggling with is grouping them by category id and then placing them in the category card? Does anyone know a good approach to this?
Thankyou very much!
Faq table consists of [name, content, date, cat_id, status]
Category Table consists of [name, content, date, type, status] e.g. FAQ Cat. I'm FAQ content, 07/09/2018, 2, 1
$lastCatId = null;
foreach($rows as $row) {
if ($row['cat_id'] !== $lastCatId) {
// close previous category if one is open
// begin card for new category
}
// print question and answer
$lastCatId = $row['cat_id'];
}
$lastCatId = null;
foreach($getAllFaq as $row) {
if ($row['cat_id'] !== $lastCatId) {
// close previous category if one is open
echo'</div>';
// begin card for new category
echo'<div class="card"><div class="card-header">'.$row['cat_id'].'</div>';
}
// print question and answer
echo '
<div class="card-body">
<b>'.$row['name'].'</b> '.$row['content'].'
</div>';
$lastCatId = $row['cat_id'];
}
Ah, but you don’t always have to put </div> when the category id changed. The first time it changes from null to the first category id and there is no div opened yet…
Yes, there is a slight issue which is basically where the categories are single card instead of one card per category which is odd and why I was acting so dum above!