Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Can't get rating to display properly


aelalfy1989

Recommended Posts

Hi Everyone,

 

I was wondering, how can I get the rating to display properly. Because in my infoboxes (ex "new to the store", "top sellers", "others also bought", etc) I got the image of the rating to how but it doesn't change according to the rating of the product. Meaning that in my script. my $rating=0; and there for the image stars_'.$rating" will always be stars_0 therefore only displaying the no star image no matter what the actual rating is. Here is my script:

 

<?php
/*
 $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
	$ratings = 0;
	$rating_query = tep_db_query("select count(*) as count, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");

	$rating_result = tep_db_fetch_array($rating_query);
	$totalcount = $rating_result['count'];
	if($rating_result['count'] > 0) {
		$ratings = ceil($rating_result['total'] / $rating_result['count']);
	}

?>

<?php
 $info_box_contents = array();
 $info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));

//  new contentBoxHeading($info_box_contents);

 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;

 $info_box_contents = array();
 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, products_id from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$new_products['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
 $product = tep_db_fetch_array($product_query);
 $p_id = $product['products_id'];	

 $p_pic =  tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], TINYL_IMAGE_WIDTH, TINY_IMAGE_HEIGHT);



 $p_name =  $new_products['products_name'];

 $p_price = '<span class="productSpecialPrice">'.$currencies->display_price($new_products['products_price'], tep_get_tax_rate($new_products['products_tax_class_id'])).'</span>';

 $p_rating = tep_image(DIR_WS_IMAGES . 'stars_' .
$ratings . '.gif' ,
sprintf(BOX_REVIEWS_TEXT_OF_5_STARS,
$ratings));



   $info_box_contents[$row][$col] = array('align' => 'center', 
                                          'text' =>  '<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'], TINY_IMAGE_WIDTH, TINY_IMAGE_HEIGHT) . '<br>' . $new_products['products_name'] . '<br>'.$p_rating.'<br>'.$p_price.'</a>');




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

 new contentBox($info_box_contents);
?>
<!-- new_products_eof //-->

 

If someone can let me know what I'm dong wrong that would be greatly appreciated. I took this part

  <?php

$ratings = 0;

$rating_query = tep_db_query("select count(*) as count, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");

 

$rating_result = tep_db_fetch_array($rating_query);

$totalcount = $rating_result['count'];

if($rating_result['count'] > 0) {

$ratings = ceil($rating_result['total'] / $rating_result['count']);

}

 

?>[/code

 

from my product info page (which my web designer created). There it seems to work but on my infobox it doesn't.

 

Thanks in advance, please feel free to [[email protected]]Email me[/email] at anytime with any suggestions that you may have

 

AE

[email protected]

www.bestmacdiscounts.com

Thank you in advance,

AE

Link to comment
Share on other sites

Hi Everyone,

 

I was wondering, how can I get the rating to display properly. Because in my infoboxes (ex "new to the store", "top sellers", "others also bought", etc) I got the image of the rating to how but it doesn't change according to the rating of the product. Meaning that in my script. my $rating=0; and there for the image stars_'.$rating" will always be stars_0 therefore only displaying the no star image no matter what the actual rating is.

That is because what you doesn't make any sense. There is no products_id in the get variables so you would need to use the $new_products['products_id'] like here to do the query inside the while looop:

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

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

This would generate a lot of queries. The below code would save quite a few of them.

<?php
/*
 $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
 $info_box_contents = array();
 $info_box_contents[] = array('text' => sprintf(TABLE_HEADING_NEW_PRODUCTS, strftime('%B')));
 global $languages_id;

 new contentBoxHeading($info_box_contents);

 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, '0' as ratings 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, '0' as ratings 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;
 $info_box_contents = array();
 while ($_new_products = tep_db_fetch_array($new_products_query)) {
   $new_products[] = $_new_products;
   $products_id_array[] = $_new_products['products_id'];
 }
 $products_id_array = array_unique($products_id_array);
 if ($new_products_with_name = tep_add_product_names($products_id_array, $new_products)) {
   $new_products = $new_products_with_name;
   unset($new_products_with_name);
 }
 // get ratings
 $rating_query = tep_db_query("select count(*) as count, products_id, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id in (" . implode(',', $products_id_array) . ") group by products_id");
 while ($rating_results = tep_db_fetch_array($rating_query)) {
   $rating_result[] = array ('products_id' => $rating_results['products_id'], 'ratings' => ceil($rating_results['total'] / $rating_results['count']));
 }
 $no_of_new_products = count($new_products);
 for ($x = 0; $x < $no_of_new_products; $x++) { 
   if(!empty($rating_result)) {
  for ($i = 0; $i < count($rating_result); $i++) {
	if( $new_products[$x]['products_id'] == $rating_result[$i]['products_id'] ) {
	  $new_products[$x]['ratings'] = $rating_result[$i]['ratings'];
	}
  }
} // end if(!empty($rating_result))
 } // end for ($x = 0; $x < $no_of_new_products; $x++)

 for ($x = 0; $x < $no_of_new_products; $x++) {
// seems not to be used further on
//  $p_id = $product['products_id'];      

//  $p_pic =  tep_image(DIR_WS_IMAGES . $new_products['products_image'], $new_products['products_name'], TINYL_IMAGE_WIDTH, TINY_IMAGE_HEIGHT);

//  $p_name =  $new_products['products_name'];

 $p_price = '<span class="productSpecialPrice">'.$currencies->display_price($new_products[$x]['products_price'], tep_get_tax_rate($new_products[$x]['products_tax_class_id'])).'</span>';

 $p_rating = tep_image(DIR_WS_IMAGES . 'stars_' .
$new_products[$x]['ratings'] . '.gif' ,
sprintf(BOX_REVIEWS_TEXT_OF_5_STARS,
$new_products[$x]['ratings']));



   $info_box_contents[$row][$col] = array('align' => 'center', 
                                          'text' =>  '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $new_products[$x]["products_id"]) . '">' . tep_image(DIR_WS_IMAGES . $new_products[$x]['products_image'], $new_products[$x]['products_name'], TINY_IMAGE_WIDTH, TINY_IMAGE_HEIGHT) . '<br>' . $new_products[$x]['products_name'] . '<br>'.$p_rating.'<br>'.$p_price.'</a>');




  $col ++;
   if ($col >= 1) {
     $col = 0;
     $row ++;
   }
 } // end for ($x = 0; $x < $no_of_new_products; $x++)

 new contentBox($info_box_contents);
?>
<!-- new_products_eof //-->
<?php
function tep_add_product_names ($products_id_array, $query_result_array) {
 global $languages_id;

 if (!is_array($products_id_array) || !is_array($query_result_array)) {
  return false;
 }

 $product_names_query = tep_db_query("select products_id, products_name from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id in (" . implode(',', $products_id_array) . ") and language_id = '" . (int)$languages_id . "'");
 while ($_product_names = tep_db_fetch_array($product_names_query)) { 
  $product_names[] = array('products_id' => $_product_names['products_id'], 'products_name' => $_product_names['products_name']);  
 }

 $no_of_products = count($query_result_array);
 $no_of_product_names = count($product_names);
 for ($x = 0; $x < $no_of_products; $x++) {
   if (!empty($product_names)) {
     for ($i = 0; $i < $no_of_product_names; $i++) {
       if ($query_result_array[$x]['products_id'] == $product_names[$i]['products_id']) {
         $query_result_array[$x]['products_name'] = $product_names[$i]['products_name'];
       }
     }    
   }
 } // end for ($x = 0; $x < $no_of_new_products; $x++)
 return $query_result_array;
}
?>

Link to comment
Share on other sites

Hey Amazing work, works perfectly thank you, Now I gotta learn and understand what you did. I'm not a programmer I had alot of help with my website from a designer, I'm only good at rearranging tables and some small stuff. I'm not good with php stuff. I'm trying to apply the same thing to my specials box in the store front. I'm also having trouble with that because it wont draw two horizontal lines like it should. I'm not even sure where these lines between products come from so I can correct the issue. If you can help me with that, that would be really great.

 

<?php
/*
 $Id: default_specials.php,v 2.0 2003/06/13

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

 Copyright (c) 2003 osCommerce

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

 <tr>
     </tr>
     <tr>
       <td><table border="0" width="100%" cellspacing="0" cellpadding="2" class="smalltext2">
         <tr>
<?php
$info_box_contents = array();
 $info_box_contents[] = array('align' => 'left', 'text' => sprintf('<span style="color:#fff; font-family: Lucida Grande, Lucida Sans Unicode, Arial, Verdana, sans-seri; font-size:12px; font-weight:bold;">Specials</span>', strftime('%B')));
new infoBoxHeading($info_box_contents, false, false, tep_href_link(FILENAME_SPECIALS));

$new = tep_db_query("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 s.products_id = p.products_id and p.products_id = pd.products_id and pd.language_id = '" . $languages_id . "' and s.status = '1' order by rand() DESC limit " . MAX_DISPLAY_SPECIAL_PRODUCTS);



?>
<? tep_draw_heading_top_3();?>	
<?php   
$info_box_contents = array();
 $row = 0;
 $col = 0;

 while ($default_specials = tep_db_fetch_array($new)) {
$product_query = tep_db_query("select products_description, products_id from " . TABLE_PRODUCTS_DESCRIPTION . " where products_id = '" . (int)$specials['products_id'] . "' and language_id = '" . (int)$languages_id . "'");
$product = tep_db_fetch_array($product_query);

       $p_id = $product['products_id'];	

$ratings = 0;
	$rating_query = tep_db_query("select count(*) as count, sum(reviews_rating) as total from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");

	$rating_result = tep_db_fetch_array($rating_query);
	$totalcount = $rating_result['count'];
	if($rating_result['count'] > 0) {
		$ratings = ceil($rating_result['total'] / $rating_result['count']);
	}

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


   $info_box_contents[$row][$col] = array('align' => 'center',
                                          'params' => ' width="33%" valign="top" class="smalltext3"',
                                          'text' => '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'products_id=' . $default_specials["products_id"]) . '">' . tep_image(DIR_WS_IMAGES . $default_specials['products_image'], $default_specials['products_name'], HEADING_IMAGE_WIDTH, HEADING_IMAGE_HEIGHT) . '<br>' . $default_specials['products_name'] . '<br>' . tep_image(DIR_WS_IMAGES . 'stars_' .
$ratings . '.gif' ,
sprintf(BOX_REVIEWS_TEXT_OF_5_STARS,
$ratings)) . '<br>
<s>' . $currencies->display_price($default_specials['products_price'], tep_get_tax_rate($default_specials['products_tax_class_id'])) . '</s><br><font  size="2" color="red">' . $currencies->display_price($default_specials['specials_new_products_price'], tep_get_tax_rate($default_specials['products_tax_class_id'])) . '</font>');
   $col ++;
   if ($col > 3) {
     $col = 0;
     $row ++;
   }
 }
 new contentBox($info_box_contents);
?>
<? tep_draw_heading_bottom_3();?>	

<!-- default_specials_eof //-->

 

 

Thanks

AE

Thank you in advance,

AE

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...