Show field from linked category section

4 posts by 2 authors in: Forums > CMS Builder
Last Post: September 1, 2010   (RSS)

almost live with my site. But still some problems…

I have a multi record section called "links" with the following fields:
title, url, category. The category is a drop down list populated from another multi record section called "links_categories"
The "links_categories" has the fields: title, icon. The icon is an upload field… pretty basic...

Everything is working in the back and front-end only one thing: how do i show the icon from the category with the links?

list($linksRecords, $linksMetaData) = getRecords(array(
'tableName' => 'links',
'perPage' => '10',
'orderBy' => 'RAND()',
'allowSearch' => '0',
));


body:

<?php foreach ($linksRecords as $record): ?>
(here i want to show the icon from the category that is linked to the title)
<?php echo $record['title'] ?>
<?php echo $record['url'] ?>
<?php endforeach ?>



thanks!

Re: [videopixel] Show field from linked category section

By Jason - August 31, 2010

Hi,

What you need to do is to create an array of categories that you can then access when you're outputting your links records. Exactly how you do this will depend on exactly how you're linking your categories to your links (ie, what is the value field of the dropdown?). In this example, it's assuming you're using the num field in the links_category table.

example:

You can put this code up where you're selecting your records
list($categoryRecords,$categoryMetaData) = getRecords(array(
'tableName' => 'links_category',
'allowSearch' => false,
));

$categoryNumToRecord=array();
foreach($categoryRecords as $category){
$categoryNumToRecord[$category['num']]=$category;
}


The variable $categoryNumToRecord is an array of category records, using their 'num' field as an index.

You can then get at the correct category record when outputting your content like this:

<?php foreach ($linksRecords as $record): ?>

<?php $categoryRecord = $categoryNumToRecord[$record['category']];?>

<?php echo $record['title'] ?>
<?php echo $record['url'] ?>
<?php endforeach ?>


You can now use the variable $categoryRecord to output any information stored in that category record.

Hope this helps get you started.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [videopixel] Show field from linked category section

By Jason - September 1, 2010

Hi,

The array for the category is not just the category name, but the whole category record, this would include the icon field. Your other record will just have the category name, but no other information about the category.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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