Display Category Name not Category ID

9 posts by 4 authors in: Forums > CMS Builder
Last Post: March 15, 2011   (RSS)

Hello,

For some reason I am unable to display the category name of a listing on my listings.php page.

I am using the following code:

<?php foreach ($listingRecords as $listing): ?>

<li class="mark">
<strong><?php echo $listing['category'] ?></strong>
<h3><a href="<?php echo $listing['_link'] ?>"><?php echo $listing['product'] ?></a></h3>
<div class="text">
<?php foreach ($listing['uploads'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<a href="<?php echo $listing['_link'] ?>"><img class="image" src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" border="0" title="<?php echo $upload['info1'] ?>" alt="<?php echo $upload['info1'] ?>" /></a><br/>
<?php break ?>
<?php endif; ?>
<?php endforeach ?>


The following part of the code...

<?php echo $listing['category'] ?>

... displays the category ID number, not the name of the category.

Can you help me out please?

Thank you,
Greg

Re: [gversion] Display Category Name not Category ID

By Damon - September 19, 2010

Hi Greg,

If you are using version 2.04 or higher of CMS Builder you can use this code:

$listing['category:label']

Instead of displaying the category number, it will display the category label (name).

Give that a try. Hope that helps.
Cheers,
Damon Edis - interactivetools.com

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

Re: [Damon] Display Category Name not Category ID

Hi,

Thanks for the message.

I am using v2.06 and inserted the code as you suggested but now receive the following error:

Notice: Undefined index: category:name in /home/sites/mydomain.co.uk/public_html/listings.php on line 81

I'd be very grateful if you could help me fix this.

Thank you,
Greg

Re: [gversion] Display Category Name not Category ID

Hi Damon,

Thanks again for your help. I tried updating the website again and this time it worked!

Thanks very much!!

Greg

Re: [gversion] Display Category Name not Category ID

By Damon - September 20, 2010

Thanks for the update!

Glad to hear it is now working.
Cheers,
Damon Edis - interactivetools.com

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

Re: [gversion] Display Category Name not Category ID

By Chris - September 21, 2010

Hi Greg,

The problem you're having is because you're using trying to output fields of the $categoryRecords variable, but that variable is a list of records. The $categoryRecord variable is your record.

That said, there's no ":label" support for Category Menu records just yet. Here's how to get the parent category's name:

list($categoryRecords, $selectedCategory) = getCategories(array(
'tableName' => 'category',
'categoryFormat' => 'breadcrumb',
));
$categoryRecord = @$categoryRecords[sizeof($categoryRecords) - 1]; // get last record in list
$parentCategoryRecord = @$categoryRecords[sizeof($categoryRecords) - 2]; // get second to last record in list


getCategories() is an alternative to getRecords() which provides some extra functionality for Category Menu sections. The 'breadcrumb' option returns only the categories that are the parents (or "ancestors", if you will) of the selected category. The last record in the list will be the selected category, and the second-to-last will be the immediate parent category.

Then you can display your categories like this:

<?php echo $parentCategoryRecord['name'] ?> - <?php echo $categoryRecord['name'] ?>

...but you'll probably want some logic in there to deal with situations in which there is no parent category (because the selected category is a top-level category) or when there's no selected category at all:

<?php if ($parentCategoryRecord): ?>
<?php echo $parentCategoryRecord['name'] ?> &gt;
<?php endif ?>
<?php if ($categoryRecord): ?>
<?php echo $categoryRecord['name'] ?>
<?php else: ?>
No category selected / top level
<?php endif ?>


I hope this helps! Category Menus can be more complex than Multi and Single Record sections. Please let me know if you have any questions.
All the best,
Chris

Re: [Chris] Display Category Name not Category ID

By Djulia - March 15, 2011

Hi Chris,

I have a problem with getCategories.

My URL has a numeric value and getCategories seems to use this first value (2 in example) :

list.php/card-2d?cat=11

Do You think that it is possible to circumvent this problem ?

Is It perhaps possible to use the example of Dave ?
http://www.interactivetools.com/iforum/gforum.cgi?post=80852;search_string=%20%24lastNumber%20%3D%20%24matches%5B1%5D;t=search_engine#80852

Thanks for your suggestion ! :)

Djulia

Re: [Chris] Display Category Name not Category ID

By Djulia - March 15, 2011 - edited: March 15, 2011

The problem is corrected with your post :
http://www.interactivetools.com/forum/gforum.cgi?post=82675#82675

Thanks ! :)

Djulia