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: [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

Apologies! It does work. My mistake...

:0)

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