Forums / General Questions / Display products in same category at bottom of product_info

Display products in same category at bottom of product_info

Locked

Started by borillion_star on 25 Dec 2010, 10:52 PM · Views: 767

Results 1 to 7 of 7
This thread is locked. New replies are disabled.
25 Dec 2010, 10:52 PM
#1
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Display products in same category at bottom of product_info

I was wondering if anyone knows a way to select 4 items from a product_page. While viewing product A for example, I would like to
display in the center of the page Product B - F randomly selected as tiles. I was looking at the random products sidebox display but that doesn't limit it just the category being viewed.

Category A contains:
Product A
Product B
Product C
Product D
Product F

28 Dec 2010, 6:30 AM
#2
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Re: Display products in same category at bottom of product_info

So far I have this but Im not entirely sure this is right:

$category_in_now = zen_get_products_category_id($_GET['products_id']);

$similar_products = "SELECT p.products_type, p.products_id, pd.products_name, p.products_image, p.products_price, p.products_tax_class_id,
                                    p.products_date_added, m.manufacturers_name, p.products_model, p.products_quantity, p.products_weight, p.product_is_call,
                                    p.product_is_always_free_shipping, p.products_qty_box_status,
                                    p.master_categories_id
                             FROM " . TABLE_PRODUCTS . " p
                             LEFT JOIN " . TABLE_MANUFACTURERS . " m ON (p.manufacturers_id = m.manufacturers_id), " . TABLE_PRODUCTS_DESCRIPTION . " pd
                             WHERE p.products_status = 1
                             AND p.products_id = pd.products_id
                   			 AND p.master_categories_id  == " . $category_in_now;
28 Dec 2010, 6:50 AM
#3
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Re: Display products in same category at bottom of product_info

I believe the last line

AND p.master_categories_id  == " . $category_in_now;

is not correct, and am not sure how to do it correctly.

28 Dec 2010, 1:32 PM
#4
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,139
Plugin Contributions:
56

Re: Display products in same category at bottom of product_info

borillion_star:

I believe the last line

AND p.master_categories_id == " . $category_in_now;

> 
> is not correct, and am not sure how to do it correctly.

I'm not totally sure, but I'm guessing that changing the == to an = will do the trick ...
29 Dec 2010, 7:21 AM
#5
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Re: Display products in same category at bottom of product_info

YAY now Im getting somewhere, The code works and gets the correct info, this however only gets one product.... I would like to get four of them randomly. How would I loop it?

  $category_in_now = zen_get_products_category_id($_GET['products_id']);

  $products_query_raw = "SELECT p.products_id, p.products_type, pd.products_name, p.products_image, p.products_price,
                             p.master_categories_id
                             FROM " . TABLE_PRODUCTS . " p
                             LEFT JOIN " . TABLE_MANUFACTURERS . " m
                             ON (p.manufacturers_id = m.manufacturers_id), " . TABLE_PRODUCTS_DESCRIPTION . " pd
                             WHERE p.products_status = 1
                             AND p.products_id = pd.products_id
			     AND p.master_categories_id = :catInNow";

$products_query_raw = $db->bindVars($products_query_raw, ':catInNow', $category_in_now, 'integer');
$result = $db->Execute($products_query_raw);

if ($result->RecordCount() > 0) {
  echo 'Model number = ' . $result->fields['products_image'];
} else {
  echo 'Sorry, no record found for product';
}
29 Dec 2010, 10:35 AM
#6
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Re: Display products in same category at bottom of product_info

Somehow you are supposed to add in ORDER BY RAND() limit 4:frusty:

30 Dec 2010, 1:26 AM
#7
borillion_star avatar

borillion_star

New Zenner

Join Date:
Jan 2008
Posts:
73
Plugin Contributions:
0

Re: Display products in same category at bottom of product_info

Here is the final working code if it helps anyone, I still have some confusion about how the LIMIT part works and why I have to loop the query as I was under the impression it would get 4 and return them in a array, if anyone has any corrections to take that into account I would appreciate it.

<?php 
echo '<div>';
$category_in_now = zen_get_products_category_id($_GET['products_id']);

for($i=0;$i<4;$i++) {
		$products_query_raw = "SELECT p.products_id, p.products_type, pd.products_name, p.products_image, p.products_price,
		                       p.master_categories_id
		                       FROM " . TABLE_PRODUCTS . " p
		                       LEFT JOIN " . TABLE_MANUFACTURERS . " m
		                       ON (p.manufacturers_id = m.manufacturers_id), " . TABLE_PRODUCTS_DESCRIPTION . " pd
		                       WHERE p.products_status = 1 
		                       AND p.products_id = pd.products_id
		                       AND p.master_categories_id = :catInNow  ORDER BY rand() LIMIT 0,4";
		
		$products_query_raw = $db->bindVars($products_query_raw, ':catInNow', $category_in_now, 'integer');
		$result = $db->Execute($products_query_raw);
		
		echo '<li>';
			echo '<div id="item">';
		if ($result->RecordCount() > 0) {
			echo '<a href="' . zen_href_link('product_info', 'cPath=' . $cPath .  '&products_id=' . $result->fields['products_id']) . '">';
			echo zen_image(DIR_WS_IMAGES . $result->fields['products_image'], $result->fields['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT);
			echo '</a>';
			echo '</div>';
		echo '</li>';
		}
}
echo '</div>';
?>