Categories and records grouped on same page

17 posts by 4 authors in: Forums > CMS Builder
Last Post: May 13, 2009   (RSS)

I have a multi record page that displays categories and records under each category. These are all on the same page. In the admin I created a List Option that contains the categories. This way, each record can be assigned multiple categories. I was able to get it to work using this code from another post:
<?php if ($record['category'] != @$lastCategory): ?>
<?php $lastCategory = $record['category']; ?>
<h1><?php echo $record['category'] ?></h2>
<?php endif ?>

But the problem I am having is that, on the viewer page, the categories are being grouped together whenever a record has more than one category assigned to it. I want to be able to show each category separately and multiple instances of a record if it exists in various categories.

Re: [sparks] Categories and records grouped on same page

By Dave - February 25, 2009

Hi sparks, welcome to the CMS Builder forum! :)

That ones a little tricky and requires a bit of custom code. Can you attach your section schema file (found in cmsAdmin/data/schema/yourSection.ini.php) and your viewer file? (Attach them to the post, don't copy and paste their contents into the message).

I'll see if I can write up something that will work for you.

Hope that helps!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Categories and records grouped on same page

Thanks for the help. You guys are the best. I've been using your cms for over a year now and this is the first time I need something that isn't built in. I am attaching the files. I appreciate the help.

Re: [sparks] Categories and records grouped on same page

By ross - February 26, 2009

Hi Sparks

I have some code for you to try out. It's actually a little more advanced than what we normally post but it is copy and pasteable so you should be ok. Here's the code:

<?php
$html = "";

// loop over categories
$categoryOptions = getListOptions('meet_our_advertisers', 'category'); // tablename, fieldname
foreach (array_keys($categoryOptions) as $categoryName) {
$heading = "<br><br><strong>$categoryName</strong><br>";

// loop over records
$rows = "";
foreach ($meet_our_advertisersRecords as $record) {
$isRecordInCategory = strpos($record['category'], "\t$categoryName\t") !== false;
if ($isRecordInCategory){ $rows .= $record['title'] . "<br/>";}
}

// add to html
if ($rows) {
$html .= $heading . $rows;
}
}

//
print $html;
?>


Just paste that where you want the list of categories and articles to appear. It's setup to only display a category name if there is actually an article in it.

Let me know how you make out :).
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Categories and records grouped on same page

Great. I am almost there. The code has created the categories and records correctly. Now, how do I link each record to its detail page?

Re: [sparks] Categories and records grouped on same page

By ross - March 2, 2009

Hi.

To get the articles linking, you would need to change this line of code:

if ($isRecordInCategory){ $rows .= $record['title'] . "<br/>";}

Try replacing that with this:

if ($isRecordInCategory){ $rows .= "<a href='" . $record['_link'] . "'>$record['title'] . "</a><br/>";}
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

Re: [ross] Categories and records grouped on same page

By 28sparks - March 3, 2009

Ross:

The code did not work. The page no longer loads. Any ideas?

Thanks.

Re: [sparks] Categories and records grouped on same page

By Dave - March 4, 2009

Sparks,

Try this instead:

if ($isRecordInCategory){ $rows .= "<a href='{$record['_link']}'>{$record['title']}</a><br/>"; }
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Categories and records grouped on same page

By 28sparks - March 4, 2009

You guys are the best. It worked perfectly. This is the type of support that makes your product so valuable. I would have had to pay a programmer several hours of work to figure this out. Instead, I think I'll spend that money on some new CMS Builder licenses. Thanks again.