Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Show Latest Products From Selected Categories


harvinder

Recommended Posts

Hye everyone,

 

I have been using a template from template monster and it show up four latest producte from all the categories but I only want to show results from selected categories not all of them. I shtta possible to do, and if it is please help me do this. This code is displaying the rsults of the latest added catalog i think in the index.php, what chamges would be necessary to make it show results from selective categories!

    <?php include(DIR_WS_MODULES . FILENAME_NEW_PRODUCTS); ?>

 

Please help!

Link to comment
Share on other sites

In my index.php in the root I have the following lines that inludes the new products

 

<?php include(DIR_WS_MODULES . FILENAME_NEW_PRODUCTS); ?> 

 

This produces results from the new_products.php file which is given below:

 

<!-- new_products //-->
<?php

 if ( (!isset($new_products_category_id)) || ($new_products_category_id == '0') ) {
   $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);
 } else {
   $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);
 }

 $row = 0;
 $col = 0;


 echo '

                   <table cellspacing=1 cellpadding=0>
                    <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'];
  // get products category
  $categories_query = tep_db_query("select categories_id from " . TABLE_PRODUCTS_TO_CATEGORIES . " where products_id = '" . (int)$new_products['products_id'] . "'");
  if (tep_db_num_rows($categories_query) > 0) {
    $categories = tep_db_fetch_array($categories_query);
    $categories_query2 = tep_db_query("select categories_name from " . TABLE_CATEGORIES_DESCRIPTION . " where categories_id = '" . (int)$categories['categories_id'] . "'");
    $categories2 = tep_db_fetch_array($categories_query2);
    $category_name = $categories2['categories_name'];
  } else {
    $category_name = '';
  }


  echo '


                     <td width=225 class=br bgcolor="#FFF8F7">
                         <table cellspacing=0 cellpadding=0>
                          <tr><td height=2 colspan=4></td></tr>
                          <tr><td width=16 height=28 bgcolor="#E7EBC7"></td><td width=109 height=28 bgcolor="#E7EBC7"><a class=ml1 href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . $category_name . '</a></td><td width=16 height=28 bgcolor="#E7EBC7"></td><td width=3 height=28 bgcolor="#BDCA4A"></td></tr>
                          <tr><td height=10 colspan=4></td></tr>
                         </table>
                         <table cellspacing=0 cellpadding=0 width=213>
                          <tr><td valign=top><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'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a></td>
                              <td width=103 valign=top>
                               <table cellspacing=0 cellpadding=0>
                                <tr><td align=right valign=bottom  height=25 ><a class=ml2 href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products['products_id']) . '">' . $new_products['products_name'] . '</a></td></tr>
                                <tr><td height=4></td></tr>
                                <tr><td align=right><img src=images/m17.gif width=97 height=1></td></tr>
                                <tr><td height=20></td></tr>
                                <tr><td class=ch align=right>Price Now:</td></tr>
                                <tr><td height=5></td></tr>
                                <tr><td class=ch2 align=right>'.$currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])).'</td></tr>
                                <tr><td height=20></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></td></tr>
                                <tr><td height=3></td></tr>
                                <tr><td align=right><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=15></td></tr>
                               </table>
                          </td></tr>
                         </table>
                        </td>
       ';

   $col ++;
   if ($col > 1) {
     $col = 0;
     $row ++;
     echo '

                  </tr>
                    <tr>

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


 echo '

                      </tr>
                   </table>

      ';


?>

 

Actually I have no idea with database queries and I am stuck at this point not knowing how to change these qeries to only produce the latest products from only selected categories and not all of the categories? At the moment it is showing four recently added products but I only want to show these recent products from selected four categories, is that possible?

 

Please help!

Link to comment
Share on other sites

for an update I managed to get a solution on my own, I think i was being lazy!!!

 

Here are the things I did for future reference:

 

1) created a New filejust to be safe: new_products_main.php

2) copied the content from new_products.php into this new file and then edited it to refelect the changes.

3) Removed the if statement, so that only the else part is remaining [without the brackets!]

4) then I created two new variables at the top between the <?php and if statement:

<?php

 

$id = 5; $run=0;

 

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

 

the variable $id is the initial category ID you need to display for your featured products such as the first category from which the latest product shols be extracted. [Hint! you can find out your categories ID just by moving you mouse pointer on that specific category link from your shop it will look something like www.yoursite.com/index.php?cPath=54 where 54 is the category ID]

 

5) I then changed the querry from the else line :

 

from

$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);

 

to

$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.categories_id = '" . $id. "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

 

6) In this step i created a switch case just after the while statement i.e.:

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

switch($run){

case 0: $id = 6; break;

case 1: $id = 7; break;

case 2: $id = 8; break;

default: $id = 9;

}

 

Again the &id variable is just for the category id from where you want to extract your results.

 

7) This part includes editing the last lines of the while statement i.e.

 

from

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

}

 

to

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

&run++;

}

 

8) The last step at last, here we need to copy querry at the top of the file and paste it just after the $run++; at the end of the while loop i.e.:

 

from

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

&run++;

}

 

to

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

&run++;

$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.categories_id = '" . $id. "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

}

 

That is what I did to show 4 latest products on my homepage and all of them are selected from the categoried I wanted.

Link to comment
Share on other sites

There was just a small mistake in the steps above sorry for that, here is the corrected post:

 

 

for an update I managed to get a solution on my own, I think i was being lazy!!!

 

Here are the things I did for future reference:

 

1) created a New filejust to be safe: new_products_main.php

2) copied the content from new_products.php into this new file and then edited it to refelect the changes.

3) Removed the if statement, so that only the else part is remaining [without the brackets!]

4) then I created two new variables at the top between the <?php and if statement:

<?php

 

$id = 5; $run=0;

 

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

 

the variable $id is the initial category ID you need to display for your featured products such as the first category from which the latest product shols be extracted. [Hint! you can find out your categories ID just by moving you mouse pointer on that specific category link from your shop it will look something like www.yoursite.com/index.php?cPath=54 where 54 is the category ID]

 

5) I then changed the querry from the else line :

 

from

$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);

 

to

$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.categories_id = '" . $id. "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

 

6) This part includes editing the last lines of the while statement i.e.

 

from

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

}

 

to

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

$run++;

}

 

 

7) In this step i created a switch case just after the $run++; statement i.e.:

 

from

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

$run++;

}

 

to

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

&run++;

switch($run){

case 1: $id = 7; break;

case 2: $id = 8; break;

default: $id = 9;

}

}

 

Again the &id variable is just for the category id from where you want to extract your results.

 

 

8) The last step at last, here we need to copy querry at the top of the file and paste it just after the $run++; at the end of the while loop i.e.:

 

from

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

&run++;

switch($run){

case 1: $id = 7; break;

case 2: $id = 8; break;

default: $id = 9;

}

}

 

to

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

&run++;

switch($run){

case 1: $id = 7; break;

case 2: $id = 8; break;

default: $id = 9;

}

$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.categories_id = '" . $id. "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

}

 

That is what I did to show 4 latest products on my homepage and all of them are selected from the categoried I wanted.

Link to comment
Share on other sites

this is very strange I am unable to edit my own posts, this makes it tedious for the users!!! because of my immaturish programming error I made a big mistake which made the while loop to run forever. So, here are the fix for it which stops the loop and comes out of it.

 

I had this code just after the while loop ended and nothing after it:

 

echo '

 

</tr>

</table>

 

';

 

so what I did is inserted a if statement to stop the loop and inserted the above code into that if statement

here is how it looks now:

 

 

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

 

$run++;

 

switch($run){

case 1: $id = 48; break;

case 2: $id = 49; break;

case 3: $id = 52; break;

default: break;

}

 

$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.categories_id = '" . /*(int)$new_products_category_id*/$id . "' and p.products_status = '1' order by p.products_date_added desc limit " . MAX_DISPLAY_NEW_PRODUCTS);

if ($run == 4){

 

echo '

 

</tr>

</table>

 

';

 

return;}

}

 

 

Summary:

 

In order to display number of latest products you can use this example, first follow the steps in my previous post and then do thses steps in order to stop the loop aswell!!!

 

Thanks, for inspiring me to ge my own solutions!

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...