1

I have a PHP script on a product page that shows the user's last 5 viewed products inside a box, and a PHP script on the same product page that shows 10 random products from all of the categories that are similar in name to the main product.

What I would like to do is have the random products based on the recently viewed products instead of the product title name and limited to all the products inside the subcategorys of the parent category of the product they are viewing.

Here is the last viewed product code http://pastebin.com/3LrLKHFT, and here is the random products code http://pastebin.com/16zy411R

Can anyone help me achieve this?

Recently Viewed:

    <?php
define('MAX_COLS_FOOTER', 1);
define('MAX_ROWS_FOOTER', 4);

  if (strlen($recently_viewed_footer>1)) {
    $info_box_contents = array();
    $info_box_contents[] = array('align' => 'left',
                                 'text'  => 'RECENTLY VIEWED'
                                );
    new infoBoxHeading($info_box_contents, false, false);

   $counter = 0;
    $info_box_contents = array();
    $recent_products = split(';',$recently_viewed_footer);

   $rows=0;
   $cols=0;
   foreach ($recent_products as $recent) {  if ((strlen($recent) >0) && ($counter < (MAX_ROWS_FOOTER*MAX_COLS_FOOTER)) && ($rows<MAX_ROWS_FOOTER)) {
      $recent_info = tep_db_query("select  p.products_image from " . TABLE_PRODUCTS . " p where p.products_id = '" . $recent. "'");
      $recent_info_values = tep_db_fetch_array($recent_info);
      $counter++;
      if (strlen($counter) < 2) {
        $counter = '0' . $counter;
      }
      $info_box_contents[$rows][$cols] = array('align' => 'left',
                            'params' =>'',                                
                                                        'text'  => '             
   <!-- Begin Wrapper -->
   <div id="wrapperrightinnner">         
                 <!-- Begin Left Column -->
                 <div id="leftcolumnrightinnner">                
                      <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $recent, 'NONSSL') . '">' . tep_image(DIR_WS_IMAGES . $recent_info_values['products_image'], tep_get_products_name($recent), '60px', '55px') . '
                 </div>
                 <!-- End Left Column -->        
                 <!-- Begin Right Column -->
                 <div id="rightcolumnrightinnner">             
             ' . tep_get_products_name($recent) . '</div></a>

                 <!-- End Right Column -->               
   </div>
   <!-- End Wrapper -->
                         '
                           );
      $cols++;
      if ($cols >= MAX_COLS) {
        $cols = 0;
        $rows++;
      }
     }
   }   new tableBox($info_box_contents,true);
  }?>

Random products:

    <?php
$i=0;
$pieces = explode(" ", $products_name);
$productname=$pieces[1]." ".$pieces[2];
$productname1="%".$productname."%";
$productname2="%".$pieces[1]."%";
$productname3="%".$pieces[0]."%";
$query="select * from products_description where products_name LIKE '$productname3' OR '$productname1' OR '$products_name' limit 0,$popular_at_shop";
//$query="select * from products_description where products_name like '$productname1' or '$productname2' or '$productname3' limit 0,$popular_at_shop";
$qry=mysql_query($query);
$qry1=mysql_num_rows($qry);
if($qry1>0)
{
?>
<?php
while($result=mysql_fetch_array($qry))
{
 if($i==0 ||  $i%3==0)
 {
echo '<tr valign="top">';
}
echo '<td align="center" width="300px;" style="padding-left:20px; padding-top:10px; padding-bottom:10px;">';
?>

<a href="<?php echo tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $result['products_id']); ?>"><div class="popularatshop"><?php echo substr($result['products_name'],0,28); ?>...</div></a>

<?php

if (($i+1)%3==0)
{
echo '</td></tr>';
}
$i++;

}

}

echo '</table></td></tr><tr>';
?></div>
<div>

   </div>
   <!-- End Wrapper -->
<?php
  if (isset($HTTP_GET_VARS['products_id'])) {
  $i=0;
  $id=$HTTP_GET_VARS['products_id'];

/*
   $orders_query = mysql_query("select p.products_id, p.products_image from orders_products opa, orders_products opb, orders o, products  p where opa.products_id = '$id' and opa.orders_id = opb.orders_id and opb.products_id != '$id' and opb.products_id = p.products_id and opb.orders_id = o.orders_id and p.products_status = '1' group by p.products_id order by o.date_purchased desc limit 0,$also_purchase_limit");
*/

  $orders_query = mysql_query("select p.products_id, p.products_image from products p, categories c, products_to_categories ptc where p.products_id = ptc.products_id and c.categories_id = ptc.categories_id and c.categories_id in (select categories_id from products_to_categories where products_id = '$id') order by rand() limit 0, 21");

 $also1=mysql_num_rows($orders_query);
 if($also1>0)
 {
 ?>
4

0 回答 0