Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Random Product from Category and Descendants (5159) Support Thread


baalwww

Recommended Posts

  • 2 weeks later...
Hi. I'll open this thread for questions and suggestions.

 

If you'd like to see this code at work, take a look at cebu.us.com The thumbnail photos of the main categories in a column on the right-hand side, and the category photos that are displayed when viewing a category, are generated by random selection of a product photo from that category tree.

Link to comment
Share on other sites

  • 1 month later...

Hi,

 

Many thanks for this great contribution, it corresponds 100% at my wishes. Bad thing is i cant make it working. It dont provide me any products. I modified a little, i seen the random number but nothing more :

 

<?php

define('NO_IMAGE', 'No-Image_Available.gif'); //change this if necessary

$status=true;

$quantity=true;

$photo=true;

$catno="52";

 

if ($catno > 0) {

//create array to hold all categories including and below the one passed

$cats_list = array();

//make sure the passed cat exists

$cat_query = tep_db_query("select count(*) as ctr from " . TABLE_CATEGORIES . " where categories_id = '" . $catno . "'");

$cats = tep_db_fetch_array($cat_query);

if ($cat_query > 0) $cats_list[] = $catno;

//Obtient la lsite des sous-catégories

$cats_list = return_category_descendants($catno,$cats_list);

reset($cats_list);

// echo $cats_list[2]; nous montre la une valeur de sous-catégorie

//create array to hold products

$prod_list = array();

//get all products for all categories in $cats_list

foreach($cats_list as $val) {

$prod_list = return_products_in_category($val, $prod_list,$status,$quantity,$photo);

}

//return random product

if (count($prod_list)>0) {

$prod = array_rand($prod_list);

echo $prod;

return $prod_list[$prod];

} else {

return 0;

}

unset($cats_list);

unset($prod_list);

}

 

function return_category_descendants($parent,$descendants) {

 

$child_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . $parent . "'");

while($descendant = tep_db_fetch_array($child_query)) {

$descendants[] = $descendant['categories_id'];

$descendants = return_category_descendants($descendant['categories_id'],$descendants);

}

return $descendants;

}

 

function return_products_in_category($category, $prod_list, $status=true, $quantity=true, $photo=true) {

$constraint = ' where pc.categories_id = "' . $category . '"';

if($status) $constraint .= " and p.products_status=1";

if($quantity) $constraint .= ' and p.products_quantity>0';

if($photo) $constraint .= ' and p.products_image != "' . NO_IMAGE . '"';

$product_query = tep_db_query("select p.products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " pc LEFT JOIN " . TABLE_PRODUCTS . " p ON pc.products_id = p.products_id" . $constraint);

while($prod = tep_db_fetch_array($product_query)) {

$prod_list[] = $prod['products_id'];

}

return $prod_list;

}

?>

 

The echo $prod; return me random number but i prefer the product :)

 

Thanks for help.

 

Regards, JC

Link to comment
Share on other sites

Hi,

 

I already tried that, the number returned by echo $prod; is not a product_id from category i provided => catno="52"; Maybe i dont use the right value for my echo ???

 

Thanks

Link to comment
Share on other sites

Hi,

 

I already tried that, the number returned by echo $prod; is not a product_id from category i provided => catno="52"; Maybe i dont use the right value for my echo ???

 

Thanks

 

yeah, you should be echoing what is returned, which is the next line...

 

echo $prod_list[$prod]

Link to comment
Share on other sites

hi,

 

i tried, it display nothing :(

 

bye

 

$prod = array_rand($prod_list);

return $prod_list[$prod];

echo $prod_list[$prod];

} else {

return 0;

}

unset($cats_list);

unset($prod_list);

}

echo $prod_list[$prod];

Edited by JC_
Link to comment
Share on other sites

hi,

 

i tried, it display nothing :(

 

bye

 

$prod = array_rand($prod_list);

return $prod_list[$prod];

echo $prod_list[$prod];

} else {

return 0;

}

unset($cats_list);

unset($prod_list);

}

echo $prod_list[$prod];

 

Try this:

 

$prod = array_rand($prod_list);

$echo prod_list[$prod];

return $prod_list[$prod];

Link to comment
Share on other sites

it seems not good $echo is like a variable called echo

 

it returns an error...

 

echo $prod; return me a value generated randomly maybe its value of product in array, and i need to get real value product_id

 

Sometimes i see 0 in values generated, no products have this value in my catalog

 

echo $prod_list; it displays : Array

Edited by JC_
Link to comment
Share on other sites

Something new !!!!

 

echo $prod_list[0] return value 271, wich is product_id of category 52 !!!!

echo $prod_list[12]; return value 393, wich is product_id of category 52 !!!

 

Problem is each refresh give me same value

 

Solution is near ...

Link to comment
Share on other sites

And solution is there :

 

$prod = array($prod_list);

$rand_keys = array_rand ($prod_list, 2);

print $prod_list[$rand_keys[0]];

 

it gives random numbers from catno i specified :)

Link to comment
Share on other sites

Now i try to show product :

 

if (count($prod_list)>0) {

$prod = array($prod_list);

$rand_keys = array_rand ($prod_list, 2);

 

$products_query = 'select pd.products_name, p.products_price, p.products_tax_class_id, p.products_image from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " where p.products_status = '1' and p.products_id = '" . $prod_list[$rand_keys[0]] . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'';

 

$random_product = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . tep_image(DIR_WS_IMAGES . $products_query['products_image'], $products_query['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a class="smallText" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . $products_query['products_name'] . '</a><br>' . $trueprice . '</font><br><font color="red">' . $promosprice . '</font>';

 

echo $random_product;

 

echo $products_query['products_image'];

 

i got an error T_String on $products_query

Link to comment
Share on other sites

This query gives variables = s,

 

echo $products_query['products_image']; ====> s

 

Its like s from select :(

 

$products_query = "select " . $select_column_list . " p.products_id, p.manufacturers_id, p.products_price, p.products_tax_class_id, IF(s.status, s.specials_new_products_price, NULL) as specials_new_products_price, IF(s.status, s.specials_new_products_price, p.products_price) as final_price from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_MANUFACTURERS . " m left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where p.products_status = '1' and pd.products_id = p.products_id and pd.language_id = '" . (int)$languages_id . "' and p.manufacturers_id = m.manufacturers_id and m.manufacturers_id = '" . (int)$HTTP_GET_VARS['manufacturers_id'] . "'";

 

$random_product = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . tep_image(DIR_WS_IMAGES . $products_query['products_image'], $products_query['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a class="smallText" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . $products_query['products_name'] . '</a><br>' . $trueprice . '</font><br><font color="red">' . $promosprice . '</font>';

 

echo $random_product;

Link to comment
Share on other sites

Now i try to show product :

 

if (count($prod_list)>0) {

$prod = array($prod_list);

$rand_keys = array_rand ($prod_list, 2);

 

$products_query = 'select pd.products_name, p.products_price, p.products_tax_class_id, p.products_image from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " where p.products_status = '1' and p.products_id = '" . $prod_list[$rand_keys[0]] . "' and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "'';

 

$random_product = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . tep_image(DIR_WS_IMAGES . $products_query['products_image'], $products_query['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a class="smallText" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . $products_query['products_name'] . '</a><br>' . $trueprice . '</font><br><font color="red">' . $promosprice . '</font>';

 

echo $random_product;

 

echo $products_query['products_image'];

 

i got an error T_String on $products_query

 

Yes, the $echo was a typo :)

 

You get a T_String error because your select begins with a single quote, but you use a double quote after the from

Link to comment
Share on other sites

$products_query = "select p.products_image, p.products_price, p.products_tax_class_id, pd.products_name from " . TABLE_PRODUCTS . " p left join " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_id = pd.products_id and p.products_id = '168' and pd.language_id = '" . (int)$languages_id . "'";

 

$random_product = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]]) . '">' . tep_image(DIR_WS_IMAGES . $products_query['products_image'], $products_query['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a class="smallText" href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $prod_list[$rand_keys[0]] . '">' . $products_query['products_name'] . '</a><br>' . $trueprice . '</font><br><font color="red">' . $promosprice . '</font>';

 

echo $random_product;

 

It returns an unexpected ; on $random_product

 

and echo $products_query['products_image']; returns value ====> s

Edited by JC_
Link to comment
Share on other sites

Hi, this is first working

 

TO DO :

Use the tax class id

Display special price if product is in specials

 

Working code :

 

<?php

 

define('NO_IMAGE', 'No-Image_Available.gif'); //change this if necessary

$status=true;

$quantity=true;

$photo=true;

$catno="52";

 

function return_category_descendants($parent,$descendants) {

 

$child_query = tep_db_query("select categories_id from " . TABLE_CATEGORIES . " where parent_id = '" . $parent . "'");

while($descendant = tep_db_fetch_array($child_query)) {

$descendants[] = $descendant['categories_id'];

$descendants = return_category_descendants($descendant['categories_id'],$descendants);

}

return $descendants;

}

 

function return_products_in_category($category, $prod_list, $status=true, $quantity=true, $photo=true) {

$constraint = ' where pc.categories_id = "' . $category . '"';

if($status) $constraint .= " and p.products_status=1";

if($quantity) $constraint .= ' and p.products_quantity>0';

if($photo) $constraint .= ' and p.products_image != "' . NO_IMAGE . '"';

$product_query = tep_db_query("select p.products_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " pc LEFT JOIN " . TABLE_PRODUCTS . " p ON pc.products_id = p.products_id" . $constraint);

while($prod = tep_db_fetch_array($product_query)) {

$prod_list[] = $prod['products_id'];

}

return $prod_list;

}

 

if ($catno > 0) {

$cats_list = array();

$cat_query = tep_db_query("select count(*) as ctr from " . TABLE_CATEGORIES . " where categories_id = '" . $catno . "'");

$cats = tep_db_fetch_array($cat_query);

if ($cat_query > 0) $cats_list[] = $catno;

$cats_list = return_category_descendants($catno,$cats_list);

reset($cats_list);

$prod_list = array();

foreach($cats_list as $val) {

$prod_list = return_products_in_category($val, $prod_list,$status,$quantity,$photo);

}

if (count($prod_list)>0) {

$prod = array($prod_list);

$rand_keys = array_rand ($prod_list, 2);

if ($random_product = tep_random_select("select products_id, products_image, products_tax_class_id, products_price from " . TABLE_PRODUCTS . " where products_status = '1' and products_id = '".$prod_list[$rand_keys[0]]."'")) {

 

} else {

return 0;

}

unset($cats_list);

unset($prod_list);

}

 

$random_product['products_name'] = tep_get_products_name($random_product['products_id']);

$random_product['specials_new_products_price'] = tep_get_products_special_price($random_product['products_id']);

 

$info_box_contents = array();

if (tep_not_null($random_product['specials_new_products_price'])) {

$whats_new_price = '<s>' . $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</s><br>';

$whats_new_price .= '<span class="productSpecialPrice">' . $currencies->display_price($random_product['specials_new_products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</span>';

} else {

$whats_new_price = $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id']));

}

 

$info_box_contents = array();

$info_box_contents[] = array('align' => 'center',

'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $random_product['products_image'], $random_product['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a><br><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']) . '">' . $random_product['products_name'] . '<br>' . $random_product['products_price'] . '</a><br>');

 

new infoBox($info_box_contents);

}

 

?>

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...