Jump to content
  • Checkout
  • Login
  • Get in touch

osCommerce

The e-commerce.

Need help with rounding differences


hubcat

Recommended Posts

I have 2 very small mods that I added to my code. Both are from community contributions. The purpose of both is to add the average review rating, as a picture of a number of stars, somewhere on the website. Both work fine. They were coded by 2 different people and unfortunately, they round differently so that on the category listing page it might show 3 stars and then on the product_info page it might show 4 stars for the same product. I have to fix one or the other so they match - but since I actually have no idea what the php part means, I can't seem to get it to work. Here's one:

 

<?php
// ========Next section changed for review_approve and star ratings
$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");
$reviews = tep_db_fetch_array($reviews_query);
  $reviews_query_average = tep_db_query("select (avg(reviews_rating)) as average_rating from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "' and approved = 1");
$reviews_average = tep_db_fetch_array($reviews_query_average);
  $reveiws_stars = $reviews_average['average_rating'];
  $reveiws_rating = number_format($reveiws_stars,0);
if ($reviews['count'] > 0) {
?>
  <tr>
	<td class="main"><?php echo TEXT_REVIEW_AVERAGE . ' ' . '<a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS, tep_get_all_get_params()) . '">' . tep_image(DIR_WS_IMAGES . 'stars_' . $reveiws_rating . '.gif') . ' ' . ' (' . $reviews['count'] . ' ' . TEXT_CURRENT_REVIEWS . ')'. '</a>'; ?>
  </td>
  </tr>

 

Here's the other:

 

// BO Review Rating in Product Listing 1.0
 $reviews_query_raw = "select r.reviews_id, left(rd.reviews_id,100) as reviews_id, r.reviews_rating from " . TABLE_REVIEWS . " r, " . TABLE_REVIEWS_DESCRIPTION . " rd, " . TABLE_PRODUCTS . " p, " . TABLE_PRODUCTS_DESCRIPTION . " pd where p.products_status = '1' and p.products_id = r.products_id and r.reviews_id = rd.reviews_id and p.products_id = pd.products_id and pd.language_id = '" . (int)$languages_id . "' and rd.languages_id = '" . (int)$languages_id . "' and p.products_id = ". (int)$listing['products_id'] ." order by r.reviews_id DESC";
 $reviews_split = new splitPageResults($reviews_query_raw, MAX_DISPLAY_NEW_REVIEWS);
$compteurReview = 0;
$noteReview = 0;
$reviews_query = tep_db_query($reviews_split->sql_query);
while ($reviews = tep_db_fetch_array($reviews_query)) {
$compteurReview +=1;
$noteReview += (int)$reviews['reviews_rating'];
}
// EO Review Rating in Product Listing 1.0
		$lc_align = '';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
// BO Review Rating in Product Listing 1.0
if ($compteurReview >0) {
$noteReview = $noteReview/$compteurReview;
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$noteReview . '.gif', $noteReview .'/5'), $compteurReview) . '</a></i>';
}

 

Can anyone point me to how I can make them round the same so the stars match?

 

Thanks!

Adrienne

Link to comment
Share on other sites

The first is actually useing a sql query, not php, and is the correct approach

 

The 2nd is messy & using split results so I`m not surprised it gets it wrong

 

so for the 2nd have:

 

<?php
// ========Next section changed for review_approve and star ratings
$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");
$reviews = tep_db_fetch_array($reviews_query);
  $reviews_query_average = tep_db_query("select (avg(reviews_rating)) as average_rating from " . TABLE_REVIEWS . " where products_id = '" . (int)$listing['products_id']  . "' and approved = 1");
$reviews_average = tep_db_fetch_array($reviews_query_average);
  $reveiws_stars = $reviews_average['average_rating'];
  $reveiws_rating = number_format($reveiws_stars,0);

$lc_align = '';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}

 

 

but your snippit looks incomplete so that may not work

Sam

 

Remember, What you think I ment may not be what I thought I ment when I said it.

 

Contributions:

 

Auto Backup your Database, Easy way

 

Multi Images with Fancy Pop-ups, Easy way

 

Products in columns with multi buy etc etc

 

Disable any Category or Product, Easy way

 

Secure & Improve your account pages et al.

Link to comment
Share on other sites

but your snippit looks incomplete so that may not work

 

Well, you were right, but there were only those few changes that started with // BO so I'm not sure what was missing... Here's the whole file with your changes. Now the stars don't show at all. Any suggestions?

 

<?php
/*
 $Id: product_listing.php,v 1.44 2003/06/09 22:49:59 hpdl Exp $

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

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/

 $listing_split = new splitPageResults($listing_sql, MAX_DISPLAY_SEARCH_RESULTS, 'p.products_id');

 if ( ($listing_split->number_of_rows > 0) && ( (PREV_NEXT_BAR_LOCATION == '1') || (PREV_NEXT_BAR_LOCATION == '3') ) ) {
//begin_show_all_mod
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
 <tr>
<td class="smallText"><?php echo $listing_split->display_count($listing_split->current_page_number); ?></td>
<td class="smallText" align="right"><?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?></td>
 </tr>
</table>
<?php
//end_show_all_mod
 }

 $list_box_contents = array();

 if ($listing_split->number_of_rows > 0) {
$listing_query = tep_db_query($listing_split->sql_query);

$row = 0;
$column = 0;
while ($listing = tep_db_fetch_array($listing_query)) {

  $product_contents = array();

  for ($col=0, $n=sizeof($column_list); $col<$n; $col++) {
// BEGIN add for SORT_BY_DROPDOWN
		 if ($column_list[$col] == 'PRODUCT_LIST_30SALES')
			continue;
// END add for SORT_BY_DROPDOWN
	$lc_align = '';

	switch ($column_list[$col]) {
	  case 'PRODUCT_LIST_MODEL':
		$lc_align = '';
		$lc_text = ' ' . $listing['products_model'] . ' ';
		break;
	  case 'PRODUCT_LIST_NAME':
// BO Review Rating in Product Listing 1.0

$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");
$reviews = tep_db_fetch_array($reviews_query);
  $reviews_query_average = tep_db_query("select (avg(reviews_rating)) as average_rating from " . TABLE_REVIEWS . " where products_id = '" . (int)$listing['products_id']  . "' and approved = 1");
$reviews_average = tep_db_fetch_array($reviews_query_average);
  $reveiws_stars = $reviews_average['average_rating'];
  $reveiws_rating = number_format($reveiws_stars,0);

$lc_align = '';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}
// EO Review Rating in Product Listing 1.0
		} else {
		  $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> ';
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}
// EO Review Rating in Product Listing 1.0
		}
		break;
	  case 'PRODUCT_LIST_MANUFACTURER':
		$lc_align = '';
		$lc_text = ' <a href="' . tep_href_link(FILENAME_DEFAULT, 'manufacturers_id=' . $listing['manufacturers_id']) . '">' . $listing['manufacturers_name'] . '</a> ';
		break;
	  case 'PRODUCT_LIST_PRICE':
		$lc_align = 'right';
		if (tep_not_null($listing['specials_new_products_price'])) {
		  $lc_text = ' <s>' .  $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</s>  <span class="productSpecialPrice">' . $currencies->display_price($listing['specials_new_products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . '</span> ';
		} else {
		  $lc_text = ' ' . $currencies->display_price($listing['products_price'], tep_get_tax_rate($listing['products_tax_class_id'])) . ' ';
		}
		break;
	  case 'PRODUCT_LIST_QUANTITY':
		$lc_align = 'right';
		$lc_text = ' ' . $listing['products_quantity'] . ' ';
		break;
	  case 'PRODUCT_LIST_WEIGHT':
		$lc_align = 'right';
		$lc_text = ' ' . $listing['products_weight'] . ' ';
		break;
	  case 'PRODUCT_LIST_IMAGE':
		$lc_align = 'center';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a>';
		} else {
		  $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . tep_image(DIR_WS_IMAGES . $listing['products_image'], $listing['products_name'], SMALL_IMAGE_WIDTH, SMALL_IMAGE_HEIGHT) . '</a> ';
		}
		break;
//========== begin SID Killer (Buy Now Button) ==========
	  case 'PRODUCT_LIST_BUY_NOW': 
		$lc_align = 'center'; 
		$lc_text = '<form name="buy_now_' . $listing['products_id'] . '" method="post" action="' . tep_href_link(basename($PHP_SELF), tep_get_all_get_params(array('action')) . 'action=buy_now', 'NONSSL') . '"><input type="hidden" name="products_id" value="' . $listing['products_id'] . '">' . tep_image_submit('button_buy_now.gif', TEXT_BUY . $listing_values['products_name'] . TEXT_NOW) . '</form><br><br><br> '; 
		break;
//========== end SID Killer (Buy Now Button) ==========
	}
	$product_contents[] = $lc_text;

  }
  $lc_text = implode('<br>', $product_contents);
  $list_box_contents[$row][$column] = array('align' => 'center',
											'params' => 'class="productListing-data"',
											'text'  => $lc_text);
  $column ++;
  if ($column >= 3) {
	$row ++;
	$column = 0;
  }
}

new productListingBox($list_box_contents);
 } else {
$list_box_contents = array();

$list_box_contents[0] = array('params' => 'class="productListing-odd"');
$list_box_contents[0][] = array('params' => 'class="productListing-data"',
							   'text' => TEXT_NO_PRODUCTS);

new productListingBox($list_box_contents);
 }

 if ( ($listing_split->number_of_rows > 0) && ((PREV_NEXT_BAR_LOCATION == '2') || (PREV_NEXT_BAR_LOCATION == '3')) ) {
//begin_show_all_mod
?>
<table border="0" width="100%" cellspacing="0" cellpadding="2">
 <tr>
<td class="smallText"><?php echo $listing_split->display_count($listing_split->current_page_number); ?></td>
<td class="smallText" align="right"><?php echo TEXT_RESULT_PAGE . ' ' . $listing_split->display_links(MAX_DISPLAY_PAGE_LINKS, tep_get_all_get_params(array('page', 'info', 'x', 'y'))); ?></td>
 </tr>
</table>
<?php
//end_show_all_mod
 }
?>

Link to comment
Share on other sites

I see where u were guessing!!!

 

change:

 

case 'PRODUCT_LIST_NAME':
// BO Review Rating in Product Listing 1.0

$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$HTTP_GET_VARS['products_id'] . "'");
$reviews = tep_db_fetch_array($reviews_query);
  $reviews_query_average = tep_db_query("select (avg(reviews_rating)) as average_rating from " . TABLE_REVIEWS . " where products_id = '" . (int)$listing['products_id']  . "' and approved = 1");
$reviews_average = tep_db_fetch_array($reviews_query_average);
  $reveiws_stars = $reviews_average['average_rating'];
  $reveiws_rating = number_format($reveiws_stars,0);

$lc_align = '';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}
// EO Review Rating in Product Listing 1.0
		} else {
		  $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> ';
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}
// EO Review Rating in Product Listing 1.0
		}
		break;

 

to

 

case 'PRODUCT_LIST_NAME':
// BO Review Rating in Product Listing 1.0

$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS . " where products_id = '" . (int)$listing['products_id'] . "'");
$reviews = tep_db_fetch_array($reviews_query);
  $reviews_query_average = tep_db_query("select (avg(reviews_rating)) as average_rating from " . TABLE_REVIEWS . " where products_id = '" . (int)$listing['products_id']  . "' and approved = 1");
$reviews_average = tep_db_fetch_array($reviews_query_average);
  $reveiws_stars = $reviews_average['average_rating'];
  $reveiws_rating = number_format($reveiws_stars,0);

$lc_align = '';
		if (isset($HTTP_GET_VARS['manufacturers_id'])) {
		  $lc_text = '<a href="' . tep_href_link(FILENAME_PRODUCT_INFO, 'manufacturers_id=' . $HTTP_GET_VARS['manufacturers_id'] . '&products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a>';  
		} else {
		  $lc_text = ' <a href="' . tep_href_link(FILENAME_PRODUCT_INFO, ($cPath ? 'cPath=' . $cPath . '&' : '') . 'products_id=' . $listing['products_id']) . '">' . $listing['products_name'] . '</a> '; }
// BO Review Rating in Product Listing 1.0
if ($reviews['count'] > 0) {
$lc_text .= '<br><i><a href="' . tep_href_link(FILENAME_PRODUCT_REVIEWS . '?products_id=' . $listing['products_id']) . '">' . sprintf(TEXT_REVIEW_RATING_ALG, tep_image(DIR_WS_IMAGES . 'stars_' . (int)$reveiws_rating . '.gif', $reveiws_rating .'/5'), $reviews['count']) . '</a></i>';
}
// EO Review Rating in Product Listing 1.0

		break;

Sam

 

Remember, What you think I ment may not be what I thought I ment when I said it.

 

Contributions:

 

Auto Backup your Database, Easy way

 

Multi Images with Fancy Pop-ups, Easy way

 

Products in columns with multi buy etc etc

 

Disable any Category or Product, Easy way

 

Secure & Improve your account pages et al.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...