Display Category Name not Category ID

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

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

By gversion - September 19, 2010

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

By gversion - September 20, 2010

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: [Damon] Display Category Name not Category ID

By gversion - September 21, 2010

Hi Damon,

I have now created a page called viewCats.php, which I would like to display all the listings in a specific category.

I have got the listings to appear correctly, so if I use the URL viewCats.php?category=6, only listings belonging to the category with id 6 are displayed.

However, on this page I would like the heading to be:

Browse: {{Parent Category}} - {{Category Name}}

I have inserted the following code so far but it's not working correctly:

===========================

<?php

require_once "init.php";

list($listingRecords, $listingDetails) = getRecords(array(
'tableName' => 'listings',
'joinTable' => 'homepages',

));

?>

<?php
require_once "cmsAdmin/lib/viewer_functions.php";
list($categoryRecords, $categoryMetaData) = getRecords(array(
'tableName' => 'category',
'where' => whereRecordNumberInUrl(1),
));
$categoryRecord = @$categoryRecords[0]; // get first record
?>

<?php include "include_header.php"; ?>


<h3>Browse: <?php echo $categoryRecords['parentNum'] ?> - <?php echo $categoryRecords['name'] ?></h3>


... code continues to display listings...

==========================

Can you give me some more help please?

Despite reading the support docs, I still haven't quite got my head around what's required in order to display field values on a page. Any explanation would be really appreciated.

Thank you very much.

Best wishes,
Greg

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