display of additional category content with headings

5 posts by 2 authors in: Forums > CMS Builder
Last Post: September 26, 2014   (RSS)

By Deborah - September 23, 2014

Hi. I've not been able to find an exact recipe for this in the forum and am looking for help. I want to display a category description beneath each category heading, followed by the list of services the admin has chosen to associate with that category.

My 'services' table has a list field 'category', with selections populated by 'Get options from database':
Table name: services_categories
Option values: num
Option labels: title

The 'services_categories' table contains a field for 'title' and one for 'description'.

I can display each category title with it's respective list of services, but I don't know how to display the 'description' field for the category beneath the category headings.

My code:

HEAD
  // categories for headings
    list($services_categoriesRecords, $services_categoriesMetaData) = getRecords(array(
    'tableName'   => 'services_categories',
    'allowSearch' => false,
  ));

 // list of services to display beneath headings
    list($servicesRecords, $servicesMetaData) = getRecords(array(
    'tableName'   => 'services',
  ));
   

HTML
<?php $servicesByCategory = array_groupBy($servicesRecords, 'category', true); ?>
<?php foreach (getListOptions('services', 'category') as $value => $label): ?>

     <?php $services = @$servicesByCategory[$value]; ?>
     <?php if (!$services) { continue; } // skip empty categories ?>
     <?php echo $label;?></br>

     <?php foreach ($services as $service): ?>
          <?php echo htmlencode($service['title']) ?>
     <?php endforeach ?>

<?php endforeach ?>

Probably an easy thing, but I'm not getting there. Any help would be greatly appreciated.

~ Deborah

By claire - September 24, 2014

Hi Deborah

You can't really do this with getListOptions if you need more than the title from the service categories section. What you'll need to do is load the categories records, as you're doing below, then sort them by the value (not the label) being used in the services section to associate each service with a category.

So ideally what you'll want to end up with is an array of categories where you can get the relevant category info by selecting it like so:

$categoriesArray[$service['category']]

Then all you'll need to do is echo the title and description from this as needed.

Does that make sense?

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

Claire Ryan
interactivetools.com

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

By Deborah - September 24, 2014

Hi, Claire and thanks for the reply.

I completely understand and appreciate the "why" of your explanation, but my various attempts of implementation result in a tangle of errors. If you were to modify the code I provided, exactly what would that look like?

I should tell you that my PHP knowledge is based primarily from examples that I can copy and understand after the working code is in place. I then keep a library of code solutions to recycle for various CMSB projects.

If this is beyond standard forum assistance, Claire, please let me know.

And thanks again.
Deborah

By Deborah - September 26, 2014

Claire, I can't thank you enough for taking the time to prepare that explanation and example. I can now see how powerful arrays can be and will try to gain a better understanding of the principles of using them.

Thanks again, Interactive Tools for your expert assistance!

~ Deborah