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

By Deborah - June 17, 2013

I have a 'resources' list editor that allows category selection from a separate 'resource_categories' table. The site owner needs to be able to update the categories table, so I can't hard-code the viewer categories onto the page.

Desired outcome is a list of categories with resources displayed beneath them:

Category 1 Resource A details Resource B details Resource C details
Category 2 Resource A details Resource B details Resource C details
(etc)

My CMSB tables are set up as:

Table 1) 'resource_categories' with 'title' text field

Table 2) 'resources' contains a pulldown list w/options from 'resource_categories' table option values = num option labels = title

Is this outrageously simple and I just haven't found the right forum search?

A BIG thanks in advance.

~ Deborah

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 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/