Where based on list + detailed page
2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 1, 2013 (RSS)
Hi
I have a section for brands (which contains the details for the brands). I also have a section for products (which details full product info). How would I write a clause which says that it's display brand A, then only show the products associated with brand A? For example, http://pipi7194.staging-cloud.netregistry.net/brands.php?1 - should only show the products for this brand. I'd really like NOT to create a dedicated page for this brand ie, brandA.php,
Is there a way to do this?
Thanks in advance!
By gregThomas - August 1, 2013
Hi,
If the brand field in your products section is using the brands num for the value, the following method would work:
<?php
// load record from 'blog'
list($blogs, $blogMetaData) = getRecords(array(
'tableName' => 'blog',
'loadUploads' => true,
'allowSearch' => false,
));
//Group the blogs by there category
$blogs = array_groupBy($blogs, 'category', true);
// load records from 'blog_categories'
list($categories, $blog_categoriesMetaData) = getRecords(array(
'tableName' => 'blog_categories',
'loadUploads' => true,
'allowSearch' => false,
));
?>
This is just example code, so you'll have to get it working with the system you're trying to create. In your code the blogs would be the products, and the blog categories would be the brands.
So I've grouped the blogs by the category they're in (in your case you'd want to group the products by there brands). This is done using the array_groupBy function.
Then to display items by there category I used the following code:
<!-- cycle through the categories -->
<?php foreach($categories as $category): ?>
<h2><?php echo $category['title']; ?></h2>
<!-- if there are blogs in this category -->
<?php if(@$blogs[$category['num']]): ?>
<!-- cycle through them and display there content -->
<?php foreach($blogs[$category['num']] as $blog): ?>
<p><?php echo $blog['title']; ?></p>
<?php endforeach; ?>
<?php endif; ?>
<?php endforeach; ?>
So in the above code a foreach loop cycles through the categories displaying the title, then I check if there are any blogs in that category, if there is then a foreach loop cycles through the blogs and displays there title.
Let me know if you have any questions.
Thanks!
Greg
PHP Programmer - interactivetools.com