Suggested Related Product

19 posts by 5 authors in: Forums > CMS Builder
Last Post: November 12, 2010   (RSS)

By Jason - November 4, 2010

Hi Jerry,

Here is one approach you could try. First have a Category section where you can have your hierarchical structure however you would like to set it up. Then have a multi record section for products. All products get stored here and each product gets a category from the category menu. This way everything is getting stored in the same place.

Then for "suggested items", in an individual product record you could have a multi select drop down showing other product categories, so you can select 1 or more categories that you would want to suggest products from. This way you're suggesting categories and you won't run into issues if products are renamed or removed.

Finally on the viewer page, you can select a given number of suggested items that belong to one of the categories that were suggested. What you're actually doing is selecting say 5 products that have a category that was listed in the "suggested items" list. You can order this however you want. You can also use a random ordering so it's different each time.

Hope this helps get you started.
---------------------------------------------------
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: [chris] Suggested Related Product

By gkornbluth - November 11, 2010

Hi Chris,

I know it’s been a while, but I’m having a problem implementing the code you suggested above.

Dreamweaver indicates that there’s a syntax error and I could use your help.

Thanks,

Jerry Kornbluth

Here’s the code I’m using.
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/hsphere/local/home/gkornblu/thecmsbcookbook.com/','','../','../../','../../../');
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($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$productsRecord = @$productsRecords[0]; // get first record

// show error message if no matching record is found
if (!$productsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

$categories = explode("\t", trim($productRecord['categories']));

$where = "";

//error on next line
foreach ($category in $categories) {
if ($where) { $where .= " OR "; }
$where .= "categories LIKE '%\t" . mysql_escape($category) . "\t%'"
}
//error on preceding line

if ($where) {
list($relatedProducts,) = getRecords(array(
'tableName' => 'products',
'where' => $where,
'limit' => '10',


));
}
else {
$relatedProducts = array();
}

?>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Chris - November 11, 2010

Hi Jerry,

What's the exact error message you're getting?
All the best,
Chris

Re: [chris] Suggested Related Product

By gkornbluth - November 11, 2010

Thanks for getting back to me Chris,

There's a highlight before the 2 lines mentioned and the Dreamweaver CS5 message that there's a syntax error on line 31 and the code may not work until you fix the error.

Screen capture attached

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php
Attachments:

error_002.jpg 388K

By Chris - November 11, 2010

Oops. Someone's been writing too much JavaScript / AS3 / Python!

foreach ($category in $categories) {

... should be:

foreach ($categories as $category) {

I hope this helps! :)
All the best,
Chris

Re: [chris] Suggested Related Product

By gkornbluth - November 11, 2010 - edited: November 11, 2010

Thanks Chris,

Now I'm down to only one error after the brace.

Jerry

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


// load viewer library
$libraryPath = 'cmsAdmin/lib/viewer_functions.php';
$dirsToCheck = array('/hsphere/local/home/gkornblu/thecmsbcookbook.com/','','../','../../','../../../');
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($productsRecords, $productsMetaData) = getRecords(array(
'tableName' => 'products',
'where' => whereRecordNumberInUrl(1),
'limit' => '1',
));
$productsRecord = @$productsRecords[0]; // get first record

// show error message if no matching record is found
if (!$productsRecord) {
header("HTTP/1.0 404 Not Found");
print "Record not found!";
exit;
}

$categories = explode("\t", trim($productRecord['categories']));

$where = "";


foreach ($categories as $category){
if ($where) { $where .= " OR "; }
$where .= "categories LIKE '%\t" . mysql_escape($category) . "\t%'"
}
//error on preceding line

if ($where) {
list($relatedProducts,) = getRecords(array(
'tableName' => 'products',
'where' => $where,
'limit' => '10',


));
}
else {
$relatedProducts = array();
}

?>

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By Jason - November 12, 2010

Hi Jerry,

It looks like you're missing a semicolon at the end of that line. Try this:

foreach ($categories as $category){
if ($where) { $where .= " OR "; }
$where .= "categories LIKE '%\t" . mysql_escape($category) . "\t%'";
}


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] Suggested Related Product

By gkornbluth - November 12, 2010

Right you are.

Thanks Jason
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php