Detail of category, list of products...

10 posts by 3 authors in: Forums > CMS Builder
Last Post: December 15, 2009   (RSS)

By Toledoh - December 11, 2009 - edited: December 11, 2009

Hi Guys,

I cant seem to get this right.

http://www.fibreking.com.au/web/products.php is a list of categories. The links (click on images) is a search for products with category equal to "x"... ie.

http://www.fibreking.com.au/web/productsList.php?product_category=Palletisers

productList.php is the list view of products, but also has the detail viewer page code for the categories.

I want to be able to click on Catgeory X from the products page, and display the list of products associated to that category, plus the details about that category.

The list of products works, however the category details always shows record 1 of the categories, not the actual category selected...

Does that make sence?

Below is the code for the productList.php.


<?php

require_once "/home/.merle/fibre_king/fibreking.com/cmsAdmin/lib/viewer_functions.php";

list($product_categoriesRecords, $product_categoriesMetaData) = getRecords(array(
'tableName' => 'product_categories',
'limit' => '1',
));
$product_categoriesRecord = @$product_categoriesRecords[0]; // get first record

// show error message if no matching record is found
if (!$product_categoriesRecord) {
print "Record not found!";
exit;
}

?>
<?php

require_once "/home/.merle/fibre_king/fibreking.com/cmsAdmin/lib/viewer_functions.php";

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'allowSearch' => '1',
));

?>


Can you help?
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

OK - hours messy around, and I've come up with this... but why doesn't it work?

<?php header('Content-type: text/html; charset=utf-8'); ?>

<?php

require_once "/home/.merle/fibre_king/fibreking.com/cmsAdmin/lib/viewer_functions.php";

list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'allowSearch' => '1',
));

?>

<?php

require_once "/home/.merle/fibre_king/fibreking.com/cmsAdmin/lib/viewer_functions.php";

list($product_categoriesRecords, $product_categoriesMetaData) = getRecords(array(
'tableName' => 'product_categories',
'where' => " name = '" .mysql_real_escape_string($productsRecord['product_category']). "' ",
'limit' => '1',
));
$product_categoriesRecord = @$product_categoriesRecords[0]; // get first record

// show error message if no matching record is found
if (!$product_categoriesRecord) {
print "Record not found!";
exit;
}

?>


it can be seen at http://www.fibreking.com.au/web/productsList2.php?product_category=2
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

By Dave - December 13, 2009

Hi Tim,

The example link you posted is returning an error, but here's some cleaned up code with comments:

<?php
header('Content-type: text/html; charset=utf-8');
require_once "/home/.merle/fibre_king/fibreking.com/cmsAdmin/lib/viewer_functions.php";

// load products - expects search such as ?product_category=Palletisers
list($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'allowSearch' => true,
));
$firstProduct = @$productRecords[0];

// load category for first product - we're assuming all products are in the same category
list($categoryRecords, $product_categoriesMetaData) = getRecords(array(
'tableName' => 'product_categories',
'where' => " name = '" .mysql_real_escape_string( $firstProduct['product_category'] ). "' ",
'allowSearch' => false,
'limit' => '1',
));
$category = @$categoryRecords[0]; // get first record

// load ALL category records - in case you want to display those as well
list($allCategoryRecords, $allCategoryMetaData) = getRecords(array(
'tableName' => 'product_categories',
'allowSearch' => false,
));

// show errors
if (!$firstProduct) { die("Couldn't find matching product!"); }
if (!$category) { die("Couldn't load category for product!"); }

?>


Hope that helps! Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Detail of category, list of products...

Hi Dave,

Thanks for working on a Sunday night!



http://www.fibreking.com.au/web/productsList2.php?product_category=1 is returning the "Couldn't find matching product!"

whereas

http://www.fibreking.com.au/web/productsList.php?product_category=1 shows that product does match...
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

Hi Dave,

Does it matter that products can be applied to multiple categories?

I may be getting an error because the product_category is a multi-select?

Cheers,
Tim
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

<h2><?php echo $product_categoriesRecord['name'] ?></h2>
<?php echo $product_categoriesRecord['description'] ?>

<?php foreach ($productsRecords as $record): ?>
<?php if ($record['product_category'] == 'mysql_real_escape_string($product_categoriesRecord['name']') ): ?>
<div id="productItem">
<?php foreach ($record['image'] as $upload): ?>
<a href="<?php echo $record['_link'] ?>"><img align="left" border="0" src="<?php echo $upload['urlPath'] ?>" width="<?php echo $upload['width'] ?>" height="<?php echo $upload['height'] ?>" alt='' /></a>
<?php endforeach ?>
<h3><?php echo $record['title'] ?></h3>
<?php echo $record['summary'] ?>
</div>
<?php endif ?>
<?php endforeach; ?>

<?php if (!$productsRecords): ?>
No records were found!<br/><br/>
<?php endif ?>


Still doesn't work... I need a break!
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

The only other thing I can think of it having a details page, with a php include... but can you have a query on an include... ie.

<?php
include("list.php?product_categories=Palletisers");
?>


Sorry for the bunch of posts... hope you understand my problem!
Cheers,

Tim (toledoh.com.au)

Re: [Toledoh] Detail of category, list of products...

Hi Guys,

Any help on this would surely be appreciated!

Cheers,
Tim
Cheers,

Tim (toledoh.com.au)

Re: [chris] Detail of category, list of products...

Brilliant Chris!

This looks like the goods.

I just added 'allowSearch' => '0' as well, and all works fine!
Cheers,

Tim (toledoh.com.au)