Filter items by category

6 posts by 2 authors in: Forums > CMS Builder
Last Post: September 1, 2016   (RSS)

By dwellingproductions - August 30, 2016


I am trying to do something similar to this post:

Unfortunately, I can't seem to make this work and am reaching out for help to save my sanity.  :-)

Essentially, I am trying to arrange FAQ questions and answers underneath their respective category heading.  So, when working, it should appear like this.

Category 1

  • Question 1
    Answer 1
  • Question 3
    Answer 3

Category 2

  • Question 2
    Answer 2
  • Question 5
    Answer 5

Category 3

  • Question 4
    Answer 4
  • Question 6
    Answer 6

For this example, in CMSB, I have a multi editor where the client add questions (faq_questions) and they can select a category radio button which is supplied by a category multi-editor (faq_topics).

My full implementation will include toggles for each question, as seen here:

But, for now, I've pared down the code to try and get it working before I add the toggle code.  You can see my "progress" here:  Right now, the topic headings display, but no questions and answers display.

The code I currently have is:

<?php $questionsByTopic = array_groupBy($faq_questionsRecords, 'topic', true); ?>

<?php foreach (getListOptions('faq_questions', 'topic') as $value => $label): ?>

  <h4><?php echo $label;?></h4>

  <?php $faq_questions['topic'] = @$questionsByTopic[$value]; ?>
  <?php if (!$faq_questions) { $faq_questions = array(); } ?>

  <?php foreach ($faq_questions as $record): ?>

        <?php echo $record['question'] ?><br>
        <?php echo $record['answer'] ?><br><br>

  <?php endforeach ?>

<?php endforeach ?>

It would also be nice if the category (topic) heading would not display if there were no questions associated with it (as shown in this post:  I left that code out for now because it wouldn't display anything when I added it.  :-)

In case they are needed, the field names I'm working with are:

For Categories: faq_topics: 'title'
For FAQ Items: faq_questions: 'question', 'answer', 'topic' (the list which pulls topic titles from the above 'faq_topics")

Hopefully that all makes sense.  Thanks, in advance, for any assistance!

- Jeremy


Dwelling Productions

By dwellingproductions - August 31, 2016

Hi Daryl!

Thanks for your reply!  Unfortunately, I'm getting an error with this line:

<?php foreach ($records => $record): ?>

The error I'm receiving is: "syntax error, unexpected T_DOUBLE_ARROW"

I tried several things to fix it, but can't seem to get anywhere.  I've uploaded the PHP page I'm working with, in case you want to take a look.  Also, I've prepared a scaled back page (faq-simple.php), with all my other code stripped out, for testing purposes.  This page is viewable online here:

I've also posted some screenshots of my CMSB pages, to ensure that I'm referencing fields and editor content properly.

I appreciate any help.  :-)

- Jeremy


Dwelling Productions

By dwellingproductions - August 31, 2016

Well, I may not need help after all. :-)  I decided to change my method of presentation, which I based on this forum post:

I'm actually much happier with this approach.  I'm not sure why I didn't think of it to begin with.  :-)  

So, everything is working perfectly now.  You can see it here:

Thanks IT for this great forum!


Dwelling Productions

By Daryl - September 1, 2016

It's a typo. I was typing too fast. It should be:

<?php foreach ($records AS $record): ?>

Anyway, I'm glad you got it working.


Daryl Maximo
PHP Programmer -

By dwellingproductions - September 1, 2016

Actually, thanks so much for following up on that, because I can use the original approach on something else I have coming up.  :-)  That's going to come in really handy.

Thanks Daryl!

- Jeremy


Dwelling Productions