Displaying value & label for related table list items on Category Menu page
3 posts by 2 authors in: Forums > CMS Builder
Last Post: 5 hours ago (RSS)
By Deborah - December 10
Hi, All.
I need to display both $value (numeric) and $label from the related businesses list field on the same line. Right now, I can only display either $value or $label. How can I output both together?
In the 'activity' Category Menu table, the 'related_businesses' list field is set up as:
- Get options from database (advanced)
- Section Tablename: business_directory
- Field for option values: num
- Field for option labels: business_name
Desired output:
If thhe record num in the related 'business_directory' table is 23, the HTML would be:
<li><a href="directory.php#biz-23">Business ABC</a></li>
<li><a href="directory.php#biz-45">Business XYZ</a></li>
Page setup:
// load records from 'activity'
list($activityRecords, $selectedActivity) = getCategories(array(
'tableName' => 'activity', //
'categoryFormat' => 'showall',
'defaultCategory' => '1',
// advanced options (you can safely ignore these)
'rootCategoryNum' => '',
'ulAttributes' => '',
'selectedCategoryNum' => '',
'ulAttributesCallback' => '',
'liAttributesCallback' => '',
'loadCreatedBy' => false,
'loadUploads' => true,
'ignoreHidden' => false,
'debugSql' => false,
));
Current code resulting in - undefined variable "$value":
<?php foreach ($activityRecords as $record): ?>
<h2><?php echo htmlencode($record['name']) ?></h2>
<h3>Related Businesses</h3>
<ul>
<?php foreach ($record['business_directory_num:labels'] as $label): ?>
<li><a href="/business/directory/index.php#biz-<?php echo $value; ?>"><?php echo $label; ?></a></li>
<?php endforeach; ?>
</ul>
<?php endforeach ?>
---------------------------------
Can anyone offer a suggestion?
Deborah
By Christine - 7 hours ago
Hi Deborah,
It looks like in your code, you're only iterating over the ":labels" of the related businesses, and that the $value variable you're using isn't being defined anywhere.
CMSBuilder actually creates two parallel arrays for list fields:
$record['business_directory_num:values'] for the "num" values.
$record['business_directory_num:labels'] for the "business_name" values.
To get both, you can iterate over each of the ":values" instead, and then use their $index to grab the corresponding label and display it too:
<?php foreach ($record['business_directory_num:values'] as $index => $value): ?>
<?php $label = $record['business_directory_num:labels'][$index]; ?>
<li><a href="/business/directory/index.php#biz-<?php echo $value; ?>"><?php echo $label; ?></a></li>
<?php endforeach; ?>
So that'll loop through each of the "num" values, then grab the associated "business_name" from the labels array using the $index, and display them both together.
interactivetools.com
By Deborah - 5 hours ago
Hello, Christine.
I was unaware of the "two parallel arrays possibility". That works perfectly!
Thanks so much for your help.
Deborah