Show Image from Parent Category

9 posts by 2 authors in: Forums > CMS Builder
Last Post: August 26, 2010   (RSS)

Hello, Gang -

I'm suffering from brain-ache so here's a little puzzler for our resident bright sparks...!

First here's the mini site structure:

Category
- sub-category 1 (icon: red square)
- - Item 1
- - Item 2
- sub-category 2 (icon: blue triangle)
- - Item 3
etc...

We are viewing the Category page which is showing us a simple (uncategorised) list of all the items.

However, I would like each entry to show the icon (an upload) associated with its parent category. Something like this...

(red square) Item 1
(red square) Item 2
(blue triangle) Item 3

How would that work?

:0)

Perch

Re: [Perchpole] Show Image from Parent Category

By Chris - August 25, 2010

Hi Perch,

Each category has a field with the "num" of its parent category (called "parentNum".) Using this, it's possible to look up any fields associated with the parent.

To make these lookups possible, the simplest solution is to create an array for lookups:

list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
));
$categoriesRecordsByNum = array_combine(array_pluck($categoriesRecords, "num"), $categoriesRecords);


Now that we can do lookups, let's look up the parent of each category as we're going through them:

<?php foreach ($categoriesRecords as $categoryRecord): ?>
<?php $parent = @$categoriesRecordsByNum[$categoryRecord['parentNum']] ?>
...


Note that some categories (namely top-level ones) won't have a parent, so $parent will be null.

Before we fiddle with uploads, let's make sure everything works with a simpler field, like "name". Let's output the parent category's name in parenthesis beside the current category's name:

<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a> (<?php echo @$parent['name'] ?>)

Make sure that works before moving on, then get rid of it.

For simplicity, I'm going to assume that you want to display a custom <img> if the parent category has one uploaded, otherwise a default <img src="bullet.gif">. If you need to style the <li>s, that'd be a bit more complicated. Let me know if that's the case.

First we check to make sure there is a parent and the parent's upload field (I'm assuming it's called "icon" here) has at least 1 image in it; if so, we snag the first upload record and display it, otherwise we output the default image:

<?php if ($parent && sizeof($parent['icon']) > 0): ?>
<?php $icon = $parent['icon'][0] ?>
<img src="<?php echo $icon['urlPath'] ?>">
<?php else: ?>
<img src="bullet.gif">
<?php endif ?>

<a href="<?php echo $categoryRecord['_link'] ?>"><?php echo $categoryRecord['name'] ?></a>


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

Re: [chris] Show Image from Parent Category

Hi, Chris -

Thanks for such a speedy and detailed response.

:0)

I think we're nearly there but I've encountered a couple of problems. Here's the code I'm using...
<?php foreach ($categoriesRecords as $lookupRecord): ?>
<?php $parent = @$categoriesRecordsByNum[$lookupRecord['parentNum']] ?>
<?php if ($parent && sizeof($parent['icon']) > 0): ?>
<?php $icon = $parent['icon'][0] ?>
<img src="<?php echo $icon['urlPath'] ?>">
<?php else: ?>
<img src="red.gif">
<?php endif ?>
<a href="<?php echo $lookupRecord['_link'] ?>"><?php echo $lookupRecord['name'] ?></a> (<?php echo @$parent['name'] ?>)<br />

The first issue related to the && sizeof($parent['icon']) > 0. This condition seemed to be preventing the script from functioning - so I just stripped it out leaving just if($parent).

Sure enough, the script then sparked ino life!

However, I then received a Notice about an Undefined offset: 0 - which referred to the $parent['icon'][0] - so I stripped [0] out too.

This resulted in another, this time related to an Undefined Index: urlPath...

So it's close but it's not quite right.

:0)

Perch

Re: [Perchpole] Show Image from Parent Category

By Chris - August 25, 2010

Hi Perch,

The later problems you were having were meant to be avoided by the code you removed. ;)

What do you mean "preventing the script from functioning"?
All the best,
Chris

Re: [chris] Show Image from Parent Category

Hi, Chris -

When && sizeof($parent['icon']) > 0 was left in the script didn't seem able to detect the presence of the uploads (called "icon") - even though they were present.

:0/

Perch

Re: [Perchpole] Show Image from Parent Category

Hi, Chris -

Thanks for the code. It's almost what I want - but it does things the wrong way round...!

I need to list the records themselves - not the categories. As each entry appears in the list it needs to lookup the icon assigned to its parent category*.

In practice (I imagine) it would work/look similar to the code Jason proposed at the bottom of this page:

Blog, Highlight Current Category?

The only difference is that instead of showing the name of the parent category* it would show the icon.

* In this instance, "parent category" refers to the category to which the record is assigned.

Thanks,

:0/

Perch

Re: [Perchpole] Show Image from Parent Category

By Chris - August 26, 2010

Hi Perch,

You can use a very similar approach. You'll still want $categoriesRecordsByNum, as above, but now when you're looping over your article/item records, you'll want to look up the category record associated with your article/item's category field (I'll assume you've called it "category".)

<?php foreach ($articleRecords as $record): ?>
<?php $category = @$categoriesRecordsByNum[$record['category']] ?>


You can create the icons as above too, but note that I've changed the category record variable name to $category instead of $parent.

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

Re: [chris] Show Image from Parent Category

Exactly!

Thanks Chris.

:0)

Perch