Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Random products on category with no products


Guest

Recommended Posts

Can anyone tell me if there is a way of showing random products when I have no products in a category, just other sub categories. Id like it to look for random products in the sub categories below it to display.

 

My code for new products that displays on my category pages is below:-

 

Any help would be much appreciated..

Thanks in advance..

 

<?php
/*
 edited - 2005/07/01 13:50

 -----------------------------------------

 $Id: new_products.php,v 1.34 2003/06/09 22:49:58 hpdl Exp $

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/
?>
<!-- new_products //-->
<?php

 if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {
// ######################## Added Enable / Disable Categorie ################
//	$new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);
$new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where c.categories_status='1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and products_status = '1' order by rand() desc limit " . MAX_DISPLAY_NEW_PRODUCTS);
// ######################## End Added Enable / Disable Categorie ################
 } else {
// ######################## Added Enable / Disable Categorie ################
//	$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);
$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where c.categories_status='1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' order by rand() desc limit " . MAX_DISPLAY_NEW_PRODUCTS);
// ######################## End Added Enable / Disable Categorie ################
 }


 $row = 0;
 $col = 0;


 echo '


				<table cellspacing=0 cellpadding=0>
				 <tr><td valign=top><img src=images/m332.gif width=490 height=26></td></tr>
				 <tr><td height=3></td></tr>
				 <tr>



   ';


 while ($new_products = tep_db_fetch_array($new_products_query)) {
  $new_products['products_name'] = tep_get_products_name($new_products['products_id']);

  $product_query = tep_db_query("select products_description  from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$new_products['products_id'] . "' and language_id = '" . (int)1 . "'");
  $product = tep_db_fetch_array($product_query);
  $new_products['products_description'] = $product['products_description'];
  $sql = 'SELECT `products_description` FROM `products_description` WHERE products_id ='.$new_products['products_id'].' && language_id='.(int)$languages_id;
$description_query = tep_db_query($sql);
$description = mysql_fetch_array($description_query, MYSQL_ASSOC);		
$description['products_description'] = substr($description['products_description'], 0, 100);
$desc_len = strlen($description['products_description']);
$description['products_description'][$desc_len-1] = '.';
$description['products_description'][$desc_len-2] = '.';
$description['products_description'][$desc_len-3] = '.';
  echo '


				  <td class=br width=490>
					  <table cellspacing=0 cellpadding=0>
					   <tr><td width=245 align=center valign=middle><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], 110, 110, 'vspace=0') . '</a></td>
						   <td width=245 valign=top>
							<table border=0 cellspacing=0 cellpadding=0 width=200>
							 <tr><td height=12></td></tr>
							 <tr><td align=right><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '"><h2>' . $new_products['products_name'] . '</h2></a></td></tr>
							 <tr><td>' . strip_tags($description['products_description']) . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']). '">' . DESCR_READ_MORE . '</a></td></tr>
							 <tr><td height=14></td></tr>
							 <tr><td align=right><span class=ch1>price:</span>   <span class=ch2>'.$currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])).'</span></td></tr>
							 <tr><td height=8></td></tr>
							 <tr><td align=right><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image_button('small_view.gif') . '</a> <a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $new_products['products_id']) . '">' . tep_image_button('button_in_cart.gif') . '</a></td></tr>
							 <tr><td height=10></td></tr>
							</table>
					   </td></tr>
					  </table>
				 </td>



	';

$col ++;
if ($col > 0) {
  $col = 0;
  $row ++;
  echo '
				 </tr>
				 <tr><td height=3></td></tr>
				<tr>
	   ';
} else echo '<td></td>';
 }


 echo '
				</tr>

				</table>
   ';


?>

Link to comment
Share on other sites

I use this (red parts new, green parts modified code)

 

this gives you subcats in any case. If you want to show subcats only if cat is not filled, you need to hack my code more.

 

<?php

/*

edited - 2005/07/01 13:50

 

-----------------------------------------

 

$Id: new_products.php,v 1.34 2003/06/09 22:49:58 hpdl Exp $

 

osCommerce, Open Source E-Commerce Solutions

http://www.oscommerce.com

 

Copyright © 2003 osCommerce

 

Released under the GNU General Public License

*/

?>

<!-- new_products //-->

<?php

 

if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {

// ######################## Added Enable / Disable Categorie ################

// $new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id where products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

$new_products_query = tep_db_query("select p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where c.categories_status='1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and products_status = '1' order by rand() desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

// ######################## End Added Enable / Disable Categorie ################

} else {

// ######################## Added Enable / Disable Categorie ################

// $new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and c.parent_id = '" . (int)$new_products_category_id . "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

 

$subcategories_array = array();

tep_get_subcategories($subcategories_array, (int)$new_products_category_id);

$add_on_subs = ")";

if (sizeof($subcategories_array) > 0) {

$add_on_subs = " or c.categories_id in (" . implode(',', $subcategories_array) . "))";

}

 

$new_products_query = tep_db_query("select distinct p.products_id, p.products_image, p.products_tax_class_id, if(s.status, s.specials_new_products_price, p.products_price) as products_price from " . TABLE_PRODUCTS . " p left join " . TABLE_SPECIALS . " s on p.products_id = s.products_id, " . TABLE_PRODUCTS_TO_CATEGORIES . " p2c, " . TABLE_CATEGORIES . " c where c.categories_status='1' and p.products_id = p2c.products_id and p2c.categories_id = c.categories_id and (c.categories_id = '" . (int)$new_products_category_id . "' or c.parent_id = '" . (int)$new_products_category_id . "'" . $add_on_subs . " and p.products_status = '1' order by rand() desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

// ######################## End Added Enable / Disable Categorie ################

}

 

 

$row = 0;

$col = 0;

 

 

echo '

 

 

<table cellspacing=0 cellpadding=0>

<tr><td valign=top><img src=images/m332.gif width=490 height=26></td></tr>

<tr><td height=3></td></tr>

<tr>

 

 

 

';

 

 

while ($new_products = tep_db_fetch_array($new_products_query)) {

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

 

$product_query = tep_db_query("select products_description from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$new_products['products_id'] . "' and language_id = '" . (int)1 . "'");

$product = tep_db_fetch_array($product_query);

$new_products['products_description'] = $product['products_description'];

$sql = 'SELECT `products_description` FROM `products_description` WHERE products_id ='.$new_products['products_id'].' && language_id='.(int)$languages_id;

$description_query = tep_db_query($sql);

$description = mysql_fetch_array($description_query, MYSQL_ASSOC);

$description['products_description'] = substr($description['products_description'], 0, 100);

$desc_len = strlen($description['products_description']);

$description['products_description'][$desc_len-1] = '.';

$description['products_description'][$desc_len-2] = '.';

$description['products_description'][$desc_len-3] = '.';

echo '

 

 

<td class=br width=490>

<table cellspacing=0 cellpadding=0>

<tr><td width=245 align=center valign=middle><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], 110, 110, 'vspace=0') . '</a></td>

<td width=245 valign=top>

<table border=0 cellspacing=0 cellpadding=0 width=200>

<tr><td height=12></td></tr>

<tr><td align=right><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '"><h2>' . $new_products['products_name'] . '</h2></a></td></tr>

<tr><td>' . strip_tags($description['products_description']) . '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $random_product['products_id']). '">' . DESCR_READ_MORE . '</a></td></tr>

<tr><td height=14></td></tr>

<tr><td align=right><span class=ch1>price:</span>   <span class=ch2>'.$currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])).'</span></td></tr>

<tr><td height=8></td></tr>

<tr><td align=right><a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . tep_image_button('small_view.gif') . '</a> <a href="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now&products_id=' . $new_products['products_id']) . '">' . tep_image_button('button_in_cart.gif') . '</a></td></tr>

<tr><td height=10></td></tr>

</table>

</td></tr>

</table>

</td>

 

 

 

';

 

$col ++;

if ($col > 0) {

$col = 0;

$row ++;

echo '

</tr>

<tr><td height=3></td></tr>

<tr>

';

} else echo '<td></td>';

}

 

 

echo '

</tr>

 

</table>

';

 

 

?>

:-)

Monika

 

addicted to writing code ... can't get enough of databases either, LOL!

 

my toolbox: Textpad - Compare and Merge - phpMyAdmin - WS_FTP - Photoshop - How to search the forum

 

Interactive Media Award July 2007 ~ category E-Commerce

my advice on the forum is for free, PMs where you send me work are considered consultation which I charge for ...

Link to comment
Share on other sites

I use this (red parts new, green parts modified code)

 

this gives you subcats in any case. If you want to show subcats only if cat is not filled, you need to hack my code more.

 

 

Cheers for that monica, works like a charm.

 

And thank you for such a quick response.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...