Click to See Complete Forum and Search --> : Random Shop Product from a Specific Shop Category


chris124
February 8th, 2008, 04:20 AM
Unfortunately my php coding abilities are limited. All I need is some php code that will display a random shop product from one of my shops categories, for instance, my shop sells furniture, tables, beds, wardrobes etc. I would like to display a random product from say my wardrobe category.

Currently I have duplicated a similar function from elsewhere in my shop. My shop has the facility to show special offers the code for this file is below. I believe that all I need is a slight modification to this code so that it will show random products from a specific Category.

<!-- new_products //-->

<style type="text/css">
<!--
#apDiv1 {
position:absolute;
width:154px;
height:90px;
z-index:1;
left: 175px;
top: 710px;
}
-->
</style>
<div id="apDiv1">
<?php

if ($random_product = tep_random_select("select p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, p.products_image, s.specials_new_products_price from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_SPECIALS . " s where p.products_status = '1' and p.products_id = s.products_id and pd.products_id = s.products_id and pd.language_id = '" . (int)$languages_id . "' and s.status = '1' order by s.specials_date_added desc limit " . MAX_RANDOM_SELECT_SPECIALS)) {
?>
<?php
$info_box_contents = array();
$info_box_contents[] = array('text' => BOX_HEADING_SPECIALS);

new infoBoxHeading($info_box_contents, false, false, tep_href_link(FILENAME_SPECIALS));

$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'] . '</a><br><s>' . $currencies->display_price($random_product['products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</s><br><span class="productSpecialPrice">' . $currencies->display_price($random_product['specials_new_products_price'], tep_get_tax_rate($random_product['products_tax_class_id'])) . '</span>');

new infoBox($info_box_contents);

?>
<img src="http://www.lawrencesfurniturecentre.co.uk/oscommerce/images/a5BottomCorners.jpg" alt="Click Item for more Information" width="154" height="20" />
<?php
}
?>
</div>
<!-- new_products_eof //-->

PeejAvery
February 8th, 2008, 09:01 AM
Instead of selecting so many different columns from your database, why don't you use SELECT *?

Concerning you problem, it looks like it is attempting to select a random product. What about the function isn't working? I think your best and easiest way would be...

$rows = mysql_num_row(mysql_query("SELECT products_id FROM " . TABLE_PRODUCTS));
$random = mt_rand(0, $rows - 1);
$query = mysql_query("SELECT * FROM " . TABLE_PRODUCTS . " LIMIT $random, 1");

chris124
February 8th, 2008, 01:24 PM
Thanks PeejAvery

I tried you code but it just failed to work, that's not your codes fault it's just need to call the php code that I already have.

Anyway, I have narrowed down what I need to the following code

<?php
if ($random_product = tep_random_select("select p.products_id, pd.products_name, p.products_price, p.products_tax_class_id, p.products_image, s.specials_new_products_price from " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd, " . TABLE_SPECIALS . " s where p.products_status = '1' and p.products_id = s.products_id and pd.products_id = s.products_id and pd.language_id = '" . (int)$languages_id . "' and s.status = '1' order by s.specials_date_added desc limit " . MAX_RANDOM_SELECT_SPECIALS)) {
?>


As you can see it is calling certain information from a table called specials, I need it to call information from a table called categories.

To help you I have listed the fields in the categories table below:
categories_id
categories_image
parent_id
sort_order
date_added
last_modified