Product listings

11 posts by 2 authors in: Forums > CMS Builder
Last Post: April 3, 2013   (RSS)

By s2smedia - March 27, 2013

I need help filtering products by Brand and by Type.

I have a section in my CMS where you can add brands, style, products. The products section requires you to select from a list what brand, style the product is part of.

I need to know how to have a list show the styles available for that brand.. and then list the products that match the size.

Right now each brand is showing ALL styles.. even though some of the brands do not have those styles available.

so for example:

http://rosenthalhotel.com/test/Products/

Each brand should only have styles list that have products available. Same goes for the drop down menu.

Then on this page:

http://rosenthalhotel.com/test/Products/productlist.php

I have a tabbed panels box.. I would like the tabs to show only the available styles for that brand.. then within each panel.. show the products that match that style.

Any help would be appreciated!

Thanks You!

Hi,

Would it be possible to post/attach your current code to a post for me?

Also, I just want to confirm the relationships. 

So a product has both a single brand and category, but there isn't a link between a brand and category?

If so, I think something like this should work:

  
  //Load all of the possible categories
  $categorys = mysql_select('categories');  

  // load records from 'product'
  list($products, $productMetaData) = getRecords(array(
    'tableName'   => 'product',
    'loadUploads' => true,
    'allowSearch' => false,
  ));



 ?>
 //Loop through each of the possible categories
 <?php foreach($categorys as $category): ?>
<!-- category container -->
<div>
  <h3><?php echo $category['title']; ?></h3>
  <ul>
    <?php
      //Create an array to store the categories a product can belong too.
      $sizeArray = array(); 
      //Cycle through the products
      foreach($products as $product){
        //If the current product is in this category
        if($product['category'] == $category['num']){
          //Store its value the size array.
          $sizeArray[$product['size']] = $product['size:label']; 
        }
      }

    ?>
    <!-- cycle through the size array and display its content -->
    <?php foreach($sizeArray as $value => $label): ?>
    <li><?php echo $label; ?></li>
    <?php endforeach; ?>
  </ul>
</div>
<?php endforeach; ?>

This is just example code to give you a basic idea of how it could be done, you'll have to make a lot of changes to get it to work with your example. 

So the code cycles through the brands (in this example they are the categories), then creates a size array that is used to store the possible styles (in this example they are sizes). Then loop through all of the products to see if any are using the current category. If they are then the code stores the size the product has in the size array.  Then a foreach loop is used to cycle through the size array and display the appropriate sizes for that category.

Another option might be to combine styles and brands into one category section. With the brands being the top level items, and below each brand would be its styles. Then when creating the list field for the products, use the num field of the category for the value, and the breadcrumb field for the label. Then when a user was creating a product they would select something like: sambonet : Table Mats from the dropdown. The advantage to this is that creating the style lists with the getCategory function would be simpler than the method above, and less server intensive.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By s2smedia - March 28, 2013

Yes their is a relationship between Brands and Categories.

When creating a category in CMS.. I have list option to select what Brands offer that particular Category.

Here is my code (I stripped down unnecessary code):

Product List Page:

<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'admin/lib/viewer_functions.php';
$dirsToCheck = array('/home/roshotel/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'products'
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'loadUploads' => true,
'allowSearch' => false,
));

?>

<div class="subcontent">
<div class="breadcrumbs"><a href="#">Home</a> > <a href="#">Products</a> > Rosenthal</div>

<div class="subheadline"><img src="../images/Rosenthal.png" width="375" height="140" /></div>
<div class="subcontent2">

<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">

<li class="TabbedPanelsTab" tabindex="0">Flatware</li>
<li class="TabbedPanelsTab" tabindex="0">Glassware</li>
</ul>
<div class="TabbedPanelsContentGroup">

<div class="TabbedPanelsContent"><div class="sectionbox">
<?php foreach ($productsRecords as $record): ?><?php if ($record['category'] != 'Flatware') { continue; } ?><div class="newproduct3"><div class="product_image"></div>
<div class="product_category"><?php echo $record['category'] ?></div>
<div class="product_name2"><?php echo htmlencode($record['item_description']) ?></div>
<div class="product_pattern"><?php echo htmlencode($record['pattern']) ?></div></div><?php endforeach ?>


<div style="clear: both;"></div>

</div></div>
<div class="TabbedPanelsContent"><div class="sectionbox">
<?php foreach ($productsRecords as $record): ?><?php if ($record['category'] != 'Glassware') { continue; } ?><div class="newproduct3"><div class="product_image"></div>
<div class="product_category"><?php echo $record['category'] ?></div>
<div class="product_name2"><?php echo htmlencode($record['item_description']) ?></div>
<div class="product_pattern"><?php echo htmlencode($record['pattern']) ?></div></div><?php endforeach ?>


<div style="clear: both;"></div>

</div></div>

BRAND Page with Categories

<?php
/* STEP 1: LOAD RECORDS - Copy this PHP code block near the TOP of your page */

// load viewer library
$libraryPath = 'admin/lib/viewer_functions.php';
$dirsToCheck = array('/home/roshotel/public_html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }

// load records from 'categories'
list($categoriesRecords, $categoriesMetaData) = getRecords(array(
'tableName' => 'categories',
'loadUploads' => true,
'allowSearch' => false,
));

?>

<div class="sectionbox">
<div class="productbrandlist">
<div class="product_image2" style="background-image:url(<?php foreach ($homepage_slidesRecord['logo'] as $index => $upload): ?><?php echo $upload['thumbUrlPath'] ?><?php endforeach ?>)"></div>
<div class="productbrandcategory">
<?php foreach ($categoriesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
<?php endforeach ?></div>
</div>
<div class="productbrandlist">
<div class="product_image2" style="background-image:url(<?php foreach ($homepage_slidesRecord['logo2'] as $index => $upload): ?><?php echo $upload['thumbUrlPath'] ?><?php endforeach ?>)"></div>
<div class="productbrandcategory">
<?php foreach ($categoriesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
<?php endforeach ?></div>
</div>
<div class="productbrandlist">
<div class="product_image2" style="background-image:url(<?php foreach ($homepage_slidesRecord['logo3'] as $index => $upload): ?><?php echo $upload['thumbUrlPath'] ?><?php endforeach ?>)"></div>
<div class="productbrandcategory">
<?php foreach ($categoriesRecords as $record): ?>
<li><a href="<?php echo $record['_link'] ?>"><?php echo htmlencode($record['title']) ?></a></li>
<?php endforeach ?></div>
</div>

<div style="clear: both;"></div>

</div>

By s2smedia - March 29, 2013

That worked perfectly!!

now I just need to figure out the product listing page..

so on this page:

http://rosenthalhotel.com/test/Products/Sambonet-Products.php

I have the categories showing correctly (per the previous code you helped with)..

BUt now I need the tabbed panel boxes for each category to show only products that match the category.. Here is my code:

<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">All</li>
<?php foreach ($categoriesRecords as $record): ?>
<?php if(in_array('Sambonet', $record['brands:values'])): ?>
<li class="TabbedPanelsTab" tabindex="0"><?php echo htmlencode($record['title']) ?></li>
<?php endif; ?>
<?php endforeach ?>
</ul>
<div class="TabbedPanelsContentGroup">

<?php foreach ($categoriesRecords as $record): ?>
<?php if(in_array('Rosenthal', $record['brands:values'])): ?>
<div class="TabbedPanelsContent"><div class="sectionbox">
<?php foreach ($productsRecords as $record): ?><div class="newproduct3"><div class="product_image"></div>
<div class="product_category"><?php echo $record['category'] ?></div>
<div class="product_name2"><?php echo htmlencode($record['item_description']) ?></div>
<div class="product_pattern"><?php echo htmlencode($record['pattern']) ?></div></div><?php endforeach ?>


<div style="clear: both;"></div>

</div></div>
<?php endif; ?>
<?php endforeach ?>

</div>
</div>

By gregThomas - April 1, 2013

Hi,

I would do something like this:

<div id="TabbedPanels1" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">All</li>
  <?php foreach ($categoriesRecords as $record): ?>
    <?php if(in_array('Sambonet', $record['brands:values'])): ?>
      <li class="TabbedPanelsTab" tabindex="0"><?php echo htmlencode($record['title']) ?></li>
    <?php endif; ?>
  <?php endforeach ?>
  </ul>
  <div class="TabbedPanelsContentGroup">
    <?php foreach ($categoriesRecords as $record): ?>
      <?php if(in_array('Rosenthal', $record['brands:values'])): ?>
      <div class="TabbedPanelsContent">
        <div class="sectionbox">
        <?php foreach ($productsRecords as $product): ?>
          <?php if(in_array($record['title'],$product['category_field_for_product:label'])): ?>
            <div class="newproduct3">
              <div class="product_image"></div>
              <div class="product_category"><?php echo $product['category'] ?></div>
              <div class="product_name2"><?php echo htmlencode($product['item_description']) ?></div>
              <div class="product_pattern"><?php echo htmlencode($product['pattern']) ?></div>
            </div>
          <?php endif; ?>
        <?php endforeach ?>
          <div style="clear: both;"></div>
        </div>
      </div>
      <?php endif; ?>
    <?php endforeach ?>
  </div>
</div>

This is just example code, so you'll have to make a few changes to get it working. You'll need to change category_field_for_product:label to the name of the list field that contains the products category. I've assumed that a product can be in multiple categories, let me know if this isn't the case.

I noticed that you were using the variable name $record in both your nested foreach loops, this would have caused problems as the second foreach loop would overwrite the content of the first. So I changed its name to $product. 

So I've added an if statement (highlighted in orange), this checks if the title of the current category is in the list field that contains the categories that the product belongs to and if it is the product will be displayed.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By s2smedia - April 1, 2013

Thanks!

currently A product will not be in more than 1 category.. but if this is something we can keep just incase down the road they do want to list in multiple categorys that would be great.

I added your code.. however i am getting an error.

http://rosenthalhotel.com/test/Products/ArthurKrupp-Products.php

--------

<div id="TabbedPanels1" class="TabbedPanels">
<ul class="TabbedPanelsTabGroup">
<li class="TabbedPanelsTab" tabindex="0">All</li>
<?php foreach ($categoriesRecords as $record): ?>
<?php if(in_array('Arthur Krupp', $record['brands:values'])): ?>
<li class="TabbedPanelsTab" tabindex="0"><?php echo htmlencode($record['title']) ?></li>
<?php endif; ?>
<?php endforeach ?>
</ul>
<div class="TabbedPanelsContentGroup">

<?php foreach ($categoriesRecords as $record): ?>
<?php if(in_array('Arthur Krupp', $record['brands:values'])): ?>
<div class="TabbedPanelsContent"><div class="sectionbox">
<?php foreach ($productsRecords as $product): ?>
<?php if(in_array($record['title'],$product['category:values'])): ?>
<div class="newproduct3">
<div class="product_image"></div>
<div class="product_category"><?php echo $product['category'] ?></div>
<div class="product_name2"><?php echo htmlencode($product['item_description']) ?></div>
<div class="product_pattern"><?php echo htmlencode($product['pattern']) ?></div>
</div>
<?php endif; ?>
<?php endforeach ?>


<div style="clear: both;"></div>

</div></div>
<?php endif; ?>
<?php endforeach ?>
</div>
</div>

By gregThomas - April 2, 2013

Hi,

The problem is that the code I've given you is designed to work for a records that could have multiple categories, try changing your code to something like this:

<div id="TabbedPanels1" class="TabbedPanels">
  <ul class="TabbedPanelsTabGroup">
    <li class="TabbedPanelsTab" tabindex="0">All</li>
  <?php foreach ($categoriesRecords as $record): ?>
    <?php if(in_array('Sambonet', $record['brands:values'])): ?>
      <li class="TabbedPanelsTab" tabindex="0"><?php echo htmlencode($record['title']) ?></li>
    <?php endif; ?>
  <?php endforeach ?>
  </ul>
  <div class="TabbedPanelsContentGroup">
    <?php foreach ($categoriesRecords as $record): ?>
      <?php if(in_array('Rosenthal', $record['brands:values'])): ?>
      <div class="TabbedPanelsContent">
        <div class="sectionbox">
        <?php foreach ($productsRecords as $product): ?>
          <?php if(($record['title'] == $product['category'])): ?>
            <div class="newproduct3">
              <div class="product_image"></div>
              <div class="product_category"><?php echo $product['category'] ?></div>
              <div class="product_name2"><?php echo htmlencode($product['item_description']) ?></div>
              <div class="product_pattern"><?php echo htmlencode($product['pattern']) ?></div>
            </div>
          <?php endif; ?>
        <?php endforeach ?>
          <div style="clear: both;"></div>
        </div>
      </div>
      <?php endif; ?>
    <?php endforeach ?>
  </div>
</div>

I've highlighted the changes I've made to the if statement in green. So if the product category is only storing one value in the list field, then it will be stored in a string and not an array. So the if statement checks if the current category is equal to the category stored in the if product category list field. If it is, it displays the contents of the product.

Let me know if you have any questions.

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By s2smedia - April 2, 2013

Perfect! thanks..

final thing... some of the rows only list 2 products.. any idea how to be sure it always shows 5 per row (unless of course it is the last row)

By s2smedia - April 2, 2013

in addition to the products listings... I had one additional request.

How do I merge list records from 2 lists into one. Example.. I have 2 Multi Record lists News, Events... but I want to have them ALL list together on this page:

http://rosenthalhotel.com/test/NewsEvents/