Zen Cart Logo
Forums / Templates, Stylesheets, Page Layout / v139 Random Products Image instead of Category Icon

v139 Random Products Image instead of Category Icon

Views: 1,287

Results 1 to 10 of 10
4 Jun 2012, 8:28 PM
#1
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

v139 Random Products Image instead of Category Icon

Hi, i wish to add an option for using a random Products Image instead of Category Icon Image,

In includes/modules/category_row.php

after:

if (!defined('IS_ADMIN_FLAG')) {
die('Illegal Access');
}

i've added something like:

$rand_category_query = $db->Execute("select p.products_image as img from ". TABLE_PRODUCTS . " p ".
"LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc ON ".
"p.products_id = ptc.products_id ".
"WHERE LENGTH(p.products_image) > 0 ".
"AND ptc.categories_id = " .$categories->fields['categories_id'].
" ORDER BY rand( ) LIMIT 1");

$random_cat = $rand_category_query->fields['img'];

But

$random_cat instead of zen_image(DIR_WS_IMAGES . $categories->fields['categories_image'] is not working, whts wrong? Thanks :blink:

8 Jun 2012, 5:18 PM
#2
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

No one? noboby interested?

14 Jun 2012, 7:29 PM
#3
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

Up, it's really important, what's wrong?

14 Jun 2012, 8:04 PM
#4
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

Revisited:

$random_products_query = "select p.products_id, p.products_image, pd.products_name
from " . TABLE_PRODUCTS . " p left join " . TABLE_PRODUCTS_DESCRIPTION . "
pd on p.products_id = pd.products_id
where p.products_id = pd.products_id
and p.products_status = 1
and pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
and p.products_image > '' ORDER BY rand( ) LIMIT 1";

$categories_img = $db->Execute($random_products_query);

and

/$categories->fields['categories_image']/$categories_img->fields['products_image']

Now partially working, image is random from some products but is the same for all categories!
Any ideas?

15 Jun 2012, 4:20 AM
#5
kamion avatar

kamion

New Zenner

Join Date:
Mar 2011
Posts:
93
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

Webmart:

Now partially working, image is random from some products but is the same for all categories!
Any ideas?

I'm no expert, and I don't really know the answer, but I can see that your query is not linking the products you are selecting back to a particular category. You need to match the products selected to a particular category ID into your mysql query.... somehow. :) Good luck!

15 Jun 2012, 4:47 AM
#6
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: v139 Random Products Image instead of Category Icon

I don't have a fully-functioning answer, but kamion is right, you need to limit the products to the relevant category. The query would have to include reference to TABLE_PRODUCTS_TO_CATEGORIES and the product being associated with the correct category id, as your original code showed. Not sure if there is anything wrong with your syntax there, but that is the query you need to perfect. I'm also not sure why you need to reference the description to get the image, which is in the base "products" table.

15 Jun 2012, 8:24 AM
#7
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

gjh42:

you need to limit the products to the relevant category. The query would have to include reference to TABLE_PRODUCTS_TO_CATEGORIES

Thanks to both but the question is how?

What's wrong on?

$random_products_query = "select p.products_image, p.products_id, ptc.products_id, ptc.categories_id
from ". TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc ON ".
"p.products_id = ptc.products_id
WHERE LENGTH(p.products_image) > ''
AND ptc.categories_id = " .$categories->fields['categories_id'].
" ORDER BY rand( ) LIMIT 1";

$categories_img = $db->Execute($random_products_query);

and

/$categories->fields['categories_image']/$categories_img->fields['products_image']

18 Jun 2012, 10:50 AM
#8
kamion avatar

kamion

New Zenner

Join Date:
Mar 2011
Posts:
93
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

Expanding on your second attempt above that partially works, maybe something like:

$random_products_query = "select p.products_id, p.products_image, pd.products_name, ptc.categories_id
FROM " . TABLE_PRODUCTS . " p,
     " . TABLE_PRODUCTS_DESCRIPTION . " pd,
     " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc 
WHERE p.products_id = pd.products_id
AND p.products_status = 1
AND pd.language_id = '" . (int)$_SESSION['languages_id'] . "'
AND ptc.categories_id = '" .$categories->fields['categories_id']. "'
AND p.products_image > '' ORDER BY rand( ) LIMIT 1";

$categories_img = $db->Execute($random_products_query);

Just taking a stab here. Totally untested. :lol:

22 Jun 2012, 3:34 PM
#9
webmart avatar

webmart

New Zenner

Join Date:
Nov 2011
Posts:
34
Plugin Contributions:
0

Re: v139 Random Products Image instead of Category Icon

nop.... :lookaroun

16 Nov 2012, 10:16 AM
#10
calljj avatar

calljj

Zen Follower

Join Date:
Mar 2007
Posts:
253
Plugin Contributions:
2

Re: v139 Random Products Image instead of Category Icon

Webmart:

nop.... :lookaroun

$patharray = array();
$new_img = "";

 $sql = "SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c WHERE c.parent_id=" .$categories->fields['categories_id']. " and c.categories_status = 1";
    $result = $db->Execute($sql);

if ($result->EOF){
$patharray[] = $categories->fields['categories_id'];
}else{
while (!$result->EOF) {
//check if more categories underneath
$path_lastsub = $result->fields['categories_id']; 
$patharray[] = intval($path_lastsub);      
$result->MoveNext();
                }
}
$i=0;

while($i <= count($patharray)-1) {
$random_products_query = "select p.products_image from ". TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc ON ".
"p.products_id = ptc.products_id
WHERE p.products_image != '' and p.products_status = 1
AND ptc.categories_id = " .$patharray[$i]. " ORDER BY rand( ) LIMIT 1";
$categories_img = $db->Execute($random_products_query); 
$new_img = $categories_img->fields['products_image'];
if ($new_img != ""){
break; 
}
$i++;
}

///if massive drill down catgeories then migt have to hunting a little further for images....keep adding to this pattern if needed

if ($new_img == ""){
$i = 0;
while($i <= count($patharray)-1) {

$sql = "SELECT c.categories_id FROM " . TABLE_CATEGORIES . " c WHERE c.parent_id=" .$patharray[$i]. " and c.categories_status = 1";
    $results = $db->Execute($sql);

if ($results->EOF){
$patharrays[] = $categories->fields['categories_id'];
}else{
while (!$results->EOF) {
//check if more categories underneath
$path_lastsubs = $results->fields['categories_id']; 
$patharrays[] = intval($path_lastsubs);      
$results->MoveNext();
                }
}

$j=0;

while($j <= count($patharrays)-1) {
$random_products_querys = "select p.products_image from ". TABLE_PRODUCTS . " p LEFT JOIN " . TABLE_PRODUCTS_TO_CATEGORIES . " ptc ON ".
"p.products_id = ptc.products_id
WHERE p.products_image != '' and p.products_status = 1
AND ptc.categories_id = " .$patharrays[$i]. " ORDER BY rand( ) LIMIT 1";
$categories_img = $db->Execute($random_products_querys); 
$new_img = $categories_img->fields['products_image'];
if ($new_img != ""){
break; 
}
$j++;
}
$i++;
}

}

$categories->fields['categories_image'] = $new_img;