show similar children links from Category Section at foot of page

15 posts by 3 authors in: Forums > CMS Builder
Last Post: February 9, 2010   (RSS)

Hi guys,

Just finishing up my first category section site and have a query.

In the site, we have listed all kinds of skin conditions. We have a parent category called "Acne" and then 3 child categories called about acne, skin care advice and acne scarring.

What we want to do is if the visitor finishes reading "About Acne", we want to list the other two children records that they can read on the topic, i.e. "Skin Care Advice" and "Acne Scarring".

is this possible?

Cheers,

B

Re: [benedict] show similar children links from Category Section at foot of page

Hi guys,

Any word on this?

Re: [ross] show similar children links from Category Section at foot of page

Hi Ross,

Sure, here's the page for your perusal.

Cheers,

Benedict
Attachments:

archive_001.zip 9K

Re: [benedict] show similar children links from Category Section at foot of page

Hi Ross,

How did you go with this?

Cheers,

Benedict

Re: [benedict] show similar children links from Category Section at foot of page

By Chris - December 10, 2009

Hi Benedict,

Whoa, that's an interesting one. How about listing all the articles which belong to a category under the article's category's parent category (phew.)

Maybe something like this would do the trick?

// load the selected news record
list($newsRecords,) = getRecords(array(
'tableName' => 'news',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$newsRecord = @$newsRecords[0]; // get first record

// load the category record for this news record
list($categories,) = getRecords(array(
'tableName' => 'conditions_treatments',
'where' => "num = '" . $newsRecord['conditions_treatments'] . "'",
));
$category = @$categories[0];

// load all categories which are children of the news record's category
list($relatedCategoryRecords,) = getCategories(array(
'tableName' => 'conditions_treatments',
'rootCategoryNum' => $category['parentNum'],
));

// list news records which belong to related categories (except the selected news record)
$relatedNews = array();
$relatedCategoryNumsCSV = implode(',', array_pluck('num', $relatedCategoryRecords));
if ($relatedCategoryNumsCSV) {
list($relatedNews,) = getRecords(array(
'tableName' => 'news',
'where' => "conditions_treatments IN ($relatedCategoryNumsCSV) AND num != '" . $newsRecord['num'] . "'",
));
}


I hope this helps! Please let me know if you have any questions.
All the best,
Chris

Re: [chris] show similar children links from Category Section at foot of page

Hi Chris,

Not quite - I notice in your code you're relating news stories to conditions_treatments. These are unrelated in the site.

It is only conditions_treatments that I need to show related conditions for at the bottom of the page (i.e. other children under the same parent) so hopefully this makes it a bit simpler to acheive (?).

Re: [benedict] show similar children links from Category Section at foot of page

By Chris - December 10, 2009

Hi Benedict,

Ahh, yes. That will makes things simpler. Something like this then?

// load the selected category record
list($categories,) = getRecords(array(
'tableName' => 'conditions_treatments',
'where' => whereRecordNumberInUrl(1),
));
$category = @$categories[0];

// load all categories which are children of this category's parent
list($relatedCategoryRecords,) = getCategories(array(
'tableName' => 'conditions_treatments',
'rootCategoryNum' => $category['parentNum'],
));

All the best,
Chris

Re: [chris] show similar children links from Category Section at foot of page

Better, but still not quite. This time it is not throwing any errors, but it is listing every single category, both parent and child.

I'm using this to pull the records:


<h2>Related Items</h2>
<ul>
<?php foreach ($relatedCategoryRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo $record['name'] ?></a></li>
<?php endforeach; ?>
</ul>


Is that right? I've attache my page again, just in case.

Re: [benedict] show similar children links from Category Section at foot of page

By Chris - December 11, 2009

Hi Benedict,

You can skip the current category with the following line right after the foreach:

<?php if ($category['num'] == $record['num']) { continue; } ?>

Do you have an example I can check out to see the problem? Also, I think you forgot to post your page.
All the best,
Chris