Where based on list + detailed page

2 posts by 2 authors in: Forums > CMS Builder
Last Post: August 1, 2013   (RSS)

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

Greg Thomas







PHP Programmer - interactivetools.com