Filter items by category

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

Hello!

I am trying to do something similar to this post: http://www.interactivetools.com/forum/forum-posts.php?postNum=2217470#post2217470

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: http://dwellingproductions.com/clients/cvrs/faq.php

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: http://dwellingproductions.com/clients/cvrs/faq2.php  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): ?>

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

  <?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: http://www.interactivetools.com/forum/forum-posts.php?postNum=2217490#post2217490).  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

www.dwellingproductions.com

By Daryl - August 30, 2016 - edited: November 22, 2016

Hi Jeremy,

Use array_groupBy() function on your faq_questions records array.
For example:

$faqsTopicsToRecords= array_groupBy($faq_quesitons, 'topics:label', true);

// to see how the new array looks like
showme($faqsTopicsToRecords);

Your foreach loop should now look something like this:

<?php foreach ($faqsTopicsToRecords as $topic => $records): ?>
  <h1><?php echo htmlencode($topic) ?></h1>

  <?php foreach ($records as $record): ?>
    <p>
      <?php echo $record['question'] ?><br>
      <?php echo $record['answer'] ?><br><br>
    </p>
  <?php endforeach ?>

<?php endforeach ?>

Hope this helps!

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

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: http://dwellingproductions.com/clients/cvrs/faq-simple.php

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

www.dwellingproductions.com

Well, I may not need help after all. :-)  I decided to change my method of presentation, which I based on this forum post: http://www.interactivetools.com/forum/forum-posts.php?postNum=2230107#post2230107

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: http://www.dwellingproductions.com/clients/cvrs/faq.php

Thanks IT for this great forum!

---------------------------

Dwelling Productions

www.dwellingproductions.com

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.

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com