display list of items beneath selected category

4 posts by 2 authors in: Forums > CMS Builder
Last Post: June 20, 2013   (RSS)

By Jason - June 18, 2013

Hi Deborah,

The approach I would take would be to group all the resources by their category, and then output them under their respective category headings.  In the below code snippet, I've made the following assumptions:

  • We have a variable called $resources that has all the resource records that we want to output.
  • The drop down field in this section is called "category" and it's a single value drop down.

First we group all the resources by their category field and also get a list of all the resouce_categories records, grouped by record num.  We do this to make it easy to an individual category record without extra database calls:

<?php
  $resourcesByCategory = array_groupBy($resources, 'category', true);
  $categoriesByNum     = array_groupBy(mysql_select("resource_categories"), 'num');
?>

After that, it's just a matter of outputting:

<?php foreach ($resourcesByCategory as $category => $resources): ?>
  <!-- output category title -->
  <p><?php echo $categoriesByNum[$category]['title'];?></p>
  
  <!-- output all resources associated with that category -->
  <?php foreach ($resources as $resource): ?>
    // output resource fields here.
  <?php endforeach ?>
  
<?php endforeach ?>

Please note that this code hasn't been tested, and you may have to change some variable names to match what you have on your page.

Hope this helps get you started.

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By Deborah - June 19, 2013

Jason,

I attempted the code in your post, but got an error I couldn't get past (possibly something I did wrong). However, your clue led me to an older post of yours (http://www.interactivetools.com/forum/forum-posts.php?postNum=2217490#post2217490) and between the two, I was able to arrive at what I needed.

My resulting code (field names different than original post): 

<?php $museumsByCategory = array_groupBy($museumsRecords, 'category', true); ?>
<?php foreach (getListOptions('museums', 'category') as $value => $label): ?>
<?php $museums = @$museumsByCategory[$value]; ?>

<?php echo $label;?> 
 
<?php foreach ($museums as $museum): ?>
<?php echo htmlencode($museum['title']) ?>
<?php endforeach ?> 
 
<?php endforeach ?>

 You really got me out of a spot! Thank you so much!

~ Deborah

By Jason - June 20, 2013

Hi Deborah,

Excellent!  Glad to hear everything is working for you now.  Please let me know if you have any other questions.

Thanks,

---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/