Displaying value & label for related table list items on Category Menu page
6 posts by 3 authors in: Forums > CMS Builder
Last Post: Yesterday at 6:30pm (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
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
Hello, Christine.
I was unaware of the "two parallel arrays possibility". That works perfectly!
Thanks so much for your help.
Deborah
By Dave - Yesterday at 9:43am
And I'll just add that you can always use our convenience showme() function to see what fields are available:
<?php showme($record); ?> interactivetools.com
Hi Dave — thanks for the reminder about showme().
It’s a really handy tool, and I did use it here, but I wasn’t sure how to display both the values and the labels in this related use case.
Christine helped point me in the right direction, and I’ve now saved a working snippet in my CMSB library for future reference.
Appreciate the great support!
~ Deborah
By Dave - Yesterday at 6:30pm
Hi Deborah,
Oh, of course. Sorry, I scanned that post a bit too quickly.
Glad it's worked now! :-)
interactivetools.com