recommended products based on recently viewed
<p>I have a php script on a product page that shows the users 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 scategories that are similar in name to the main product. </p>

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

<p>Here is the last viewed product code <a href="http://pastebin.com/3LrLKHFT" rel="nofollow">http://pastebin.com/3LrLKHFT</a> and here is the random products code <a href="http://pastebin.com/16zy411R" rel="nofollow">http://pastebin.com/16zy411R</a></p>

<p>Can anyone help me achieve this?</p>

<p>Recently Viewed:</p>

<pre><code>    &lt;?php
define('MAX_COLS_FOOTER', 1);
define('MAX_ROWS_FOOTER', 4);

  if (strlen($recently_viewed_footer&gt;1)) {
    $info_box_contents = array();
    $info_box_contents[] = array('align' =&gt; 'left',
                                 'text'  =&gt; '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) &gt;0) &amp;&amp; ($counter &lt; (MAX_ROWS_FOOTER*MAX_COLS_FOOTER)) &amp;&amp; ($rows&lt;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) &lt; 2) {
        $counter = '0' . $counter;
      }
      $info_box_contents[$rows][$cols] = array('align' =&gt; 'left',
                            'params' =&gt;'',                                
                                                        'text'  =&gt; '             
   &lt;!-- Begin Wrapper --&gt;
   &lt;div id="wrapperrightinnner"&gt;         
                 &lt;!-- Begin Left Column --&gt;
                 &lt;div id="leftcolumnrightinnner"&gt;                
                      &lt;a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $recent, 'NONSSL') . '"&gt;' . tep_image(DIR_WS_IMAGES . $recent_info_values['products_image'], tep_get_products_name($recent), '60px', '55px') . '
                 &lt;/div&gt;
                 &lt;!-- End Left Column --&gt;        
                 &lt;!-- Begin Right Column --&gt;
                 &lt;div id="rightcolumnrightinnner"&gt;             
             ' . tep_get_products_name($recent) . '&lt;/div&gt;&lt;/a&gt;

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

<p>Random products:</p>

<pre><code>    &lt;?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&gt;0)
{
?&gt;
&lt;?php
while($result=mysql_fetch_array($qry))
{
 if($i==0 ||  $i%3==0)
 {
echo '&lt;tr valign="top"&gt;';
}
echo '&lt;td align="center" width="300px;" style="padding-left:20px; padding-top:10px; padding-bottom:10px;"&gt;';
?&gt;

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

&lt;?php

if (($i+1)%3==0)
{
echo '&lt;/td&gt;&lt;/tr&gt;';
}
$i++;

}

}

echo '&lt;/table&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;';
?&gt;&lt;/div&gt;
&lt;div&gt;

   &lt;/div&gt;
   &lt;!-- End Wrapper --&gt;
&lt;?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&gt;0)
 {
 ?&gt;
</code></pre>
