Assign Record to Category and Manufacturer
5 posts by 3 authors in: Forums > CMS Builder
Last Post: August 9, 2013 (RSS)
By Perchpole - July 31, 2013
Hello, All -
My client has asked me to set-up a method of grouping products (records) together that I hope someone will be able to help me with.
The idea is that each record is assigned to a category - as normal - but also to a "manufacturer".
The list of manufacturers would be set up in a separate editor and one would be chosen whenever a new product is created.
When the record is displayed, I want to show a list of all the other products assigned to the same manufacturer - regardless of which category they are in.
What would be the simplest way of achieving this?
Thanks,
Perchpole
By rconring - July 31, 2013 - edited: July 31, 2013
I would set up a second product (list) viewer on the detail page and use the manufacturer variable from the first viewer record to limit the second list to the product manufacturer in the first. That would list all other products by that manufacturer on the detail page.
Here is some sample code I copied from one of my sites:
Viewers on detail page:
// load record from 'products'
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$productsRecord = @$productsRecords[0]; // get first record
if (!$productsRecord) { dieWith404("Record not found!"); } // show error message if no record found
// load records of same mfg from 'products'
$thismfg = $productsRecord['manufacturer']; // assign product mfg to a simple variable
list($prodRecords, $prodMetaData) = getRecords(array(
'tableName' => 'products',
'where' => "manufacturer = '$thismfg'", // only records that match detail mfg
));
Display the related products from same mfg and skip detail product.
<!-- loop thru product list of same mfg -->
<?php foreach ($prodRecords as $record): ?>
<!-- if not same record as detail record -->
<?php if($record['num'] != $productsRecord['num']): ?>
<div class="prod-box">
<?php echo $record['name'] ?><br/>
</div>
<?php endif ?>
<?php endforeach ?>
Hope that helps.
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987
By Codee - August 8, 2013
Hi Ron,
With regards to the additional products by this manufacturer listings: if there are no other products by that mfg...is an error returned or a mesg? Thnks!
By rconring - August 9, 2013
Well, not as it is. I didn't need that in my application, but I made some changes in the code to make this happen if you need it.
I changed the viewer for related products to also filter out the detail record from related products. Then checked for existence of any related records and if none display the "no Records" message.
<?php
// load record from 'products'
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'where' => whereRecordNumberInUrl(0),
'loadUploads' => true,
'allowSearch' => false,
'limit' => '1',
));
$productsRecord = @$productsRecords[0]; // get first record
if (!$productsRecord) { dieWith404("Record not found!"); } // show error message if no record found
// load records of same mfg from 'products'
list($prodRecords, $prodMetaData) = getRecords(array(
'tableName' => 'products',
'where' => "manufacturer = $productsRecord[manufacturer] and num != $productsRecord[num]",
// only records that match detail mfg and omit product detail record
));
?>
----------------------------------------------------
<!-- If related products exist in $prodRecords viewer -->
<?php if($prodRecords): ?>
<h2>More Products from <?php echo $productsRecord['manufacturer:label'] ?></h2>
<!-- loop thru product list of same mfg -->
<?php foreach ($prodRecords as $record): ?>
<div class="related">
<?php echo $record['name'].' ' ?><br/>
<?php echo $record['manufacturer:label'] ?>
</div>
<?php endforeach ?>
<?php else: ?>
<?php echo "No additional Products for this manufacturer" ?>
<?php endif ?>
Is this what you had in mind?
Conring Automation Services
----------------------------------------
Software for Business and Industry Since 1987