Need help with categories to show featured items by default
5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 12, 2011 (RSS)
By kevbarker - August 10, 2011
Using some of the tutorials for category menus, I managed to get it working so that when you click a category, it displays a list of the products for that category. Within the products section, there is a checkbox which determines if a product is featured or not.
Currently when I go to the page ALL of the products are displayed by default. I don't want to display ALL products by default (unless the All Products) link is clicked. What I need is for the default display to show only the products with the featured box checked, so that when a visitor first enters the site they see the featured items.
Any suggestions on how that might be achieved? I have posted the code from the page below and the link is: http://ncblades.com/test/
Any assistance will be appreciated.
Thanks,
Kevin
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php
// load viewer library
$libraryPath = 'admin/lib/viewer_functions.php';
$dirsToCheck = array('/home/ncblades/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
list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'categoryFormat' => 'onelevel', // showall, onelevel, twolevel
));
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
));
?>
<h2>Categories</h2>
<ul>
<li><a href="?">(All Products)</a></li>
<?php foreach ($categoriesRecords as $categoryRecord): ?>
<?php echo $categoryRecord['_listItemStart'] ?>
<?php if ($categoryRecord['_isSelected']): ?><b><?php endif ?>
<a href="?category=<?php echo $categoryRecord['num'] ?>"><?php echo $categoryRecord['name'] ?></a>
<?php if ($categoryRecord['_isSelected']): ?></b><?php endif ?>
<?php echo $categoryRecord['_listItemEnd'] ?>
<?php endforeach ?>
</ul>
<h1>Articles - List Page Viewer</h1>
<?php foreach ($productsRecords as $record): ?>
Product Title: <?php echo $record['title'] ?><br/>
Product Category: <?php echo $record['category'] ?><br/>
Product Description: <?php echo $record['product_description'] ?><br/>
<a href="<?php echo $record['_link'] ?>"><?php echo $record['title'] ?></a>
<br />
<br />
<?php foreach ($record['images'] as $upload): ?>
<?php if ($upload['hasThumbnail']): ?>
<img src="<?php echo $upload['thumbUrlPath'] ?>" width="<?php echo $upload['thumbWidth'] ?>" height="<?php echo $upload['thumbHeight'] ?>" alt="" /><br/>
<?php elseif ($upload['isImage']): ?>
<img src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt="" /><br/>
<?php else: ?>
<a href="<?php echo $upload['urlPath'] ?>">Download <?php echo $upload['filename'] ?></a><br/>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
<?php if (!$productsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>
Re: [kevbarker] Need help with categories to show featured items by default
By Jason - August 11, 2011
If the name of your check box field is called "featured" you can exploit CMS Builder's autosearching functions like this:
// load records
list($categoriesRecords, $selectedCategory) = getCategories(array(
'tableName' => 'categories',
'selectedCategoryNum' => '', // defaults to getNumberFromEndOfUrl()
'categoryFormat' => 'onelevel', // showall, onelevel, twolevel
));
if (!@$_REQUEST['category']) {
$_REQUEST['featured'] = 1;
}
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
));
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Need help with categories to show featured items by default
By kevbarker - August 11, 2011
Thanks. I tried the code and it works fine for displaying the featured items when you initially go to the page. However, clicking on the All Products link also shows only the featured items and not ALL of the products. Any ideas? Did I miss something?
Kevin
Re: [kevbarker] Need help with categories to show featured items by default
By Jason - August 12, 2011
What you will need is a way to differentiate between when a person first goes to the page and when someone clicks on All Products.
For example, you can set your all products link to have a variable like this:
<li><a href="?category=all">(All Products)</a></li>
Then we can change your if statement like this:
if (!@$_REQUEST['category']) {
$_REQUEST['featured'] = 1;
}
elseif($_REQUEST['category'] == "all") {
$_REQUEST['category'] = "";
}
Hope this helps
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Re: [Jason] Need help with categories to show featured items by default
By kevbarker - August 12, 2011
Kevin