I am using the jquery ui widget star rating. After my query echos then all of the stars are shown together (and also the wrong amount if there are more than 2 reviews in the query but everything is correct if there is only 1 review in the query).
Here is the code:
this function assigns the number 1 at the start to an array to know which stars are filled.
function get_stars ($rating) {
if( $rating == 0 ) $rating = 1;
$ratings_array = array_fill (1, 5, 0);
for ($index=(int)$rating; $index>0; $index--) {
$ratings_array[$index] = 1;
}
return $ratings_array;
}
the query I echoed and tried directly in phpmyadmin to make sure it works:
select r.reviews_id, rd.reviews_text, r.reviews_rating, r.date_added, r.customers_name from reviews r join reviews_description rd on rd.reviews_id = r.reviews_id where r.products_id = '40' and rd.languages_id = '1' and reviews_status = '1' order by r.reviews_id DESC
which outputs 2 rows. 5 columns just like it is supposed to: reviews_id reviews_text reviews_rating date_added customers_name
Here is the full while loop with the query, and the table that is echoed:
<table border=0 cellspacing=0 cellpadding=0><tbody>
<?php
if (SPECIFICATIONS_MAX_REVIEWS > '0') {
$reviews_count_query_raw = "
select
count(*) as count
from
" . TABLE_REVIEWS . "
where
products_id = '" . (int) $_GET['products_id'] . "'
and reviews_status = '1'
";
// print 'Count Query: ' . $reviews_count_query_raw . '<br>';
$reviews_count_query = tep_db_query ($reviews_count_query_raw);
$reviews = tep_db_fetch_array ($reviews_count_query);
$reviews_query_average_raw = "
select
(avg(reviews_rating)) as average_rating
from
" . TABLE_REVIEWS . "
where
products_id = '" . (int) $_GET['products_id'] . "'
and reviews_status = '1'
";
// print 'Average Query: ' . $reviews_query_average_raw . '<br>';
$reviews_query_average = tep_db_query ($reviews_query_average_raw);
$reviews_average = tep_db_fetch_array ($reviews_query_average);
$reviews_stars = $reviews_average['average_rating'];
$reviews_query_raw = "
select
r.reviews_id,
rd.reviews_text,
r.reviews_rating,
r.date_added,
r.customers_name
from
" . TABLE_REVIEWS . " r
join " . TABLE_REVIEWS_DESCRIPTION . " rd
on rd.reviews_id = r.reviews_id
where r.products_id = '" . (int) $_GET['products_id'] . "'
and rd.languages_id = '" . $languages_id . "'
and reviews_status = '1'
order by r.reviews_id DESC
";
// print $reviews_query_raw . '<br>';
$reviews_query = tep_db_query( $reviews_query_raw );
$num_rows = tep_db_num_rows( $reviews_query );
echo $reviews_query_raw;
if ($num_rows > 0) {
$row = 0;
while ($reviews = tep_db_fetch_array ($reviews_query)) {
if ($row < SPECIFICATIONS_MAX_REVIEWS) {
$row++;
$date_added = tep_date_short ($reviews['date_added']);
// Show product reviews
?>
<tr>
<td class="main">
<?php
$rating = get_stars($reviews['reviews_rating']);
echo tep_draw_radio_field ('rating', '1', $rating[1], 'class="star" disabled="disabled"') .
' ' . tep_draw_radio_field ('rating', '2', $rating[2], 'class="star" disabled="disabled"') .
' ' . tep_draw_radio_field ('rating', '3', $rating[3], 'class="star" disabled="disabled"') .
' ' . tep_draw_radio_field ('rating', '4', $rating[4], 'class="star" disabled="disabled"') .
' ' . tep_draw_radio_field ('rating', '5', $rating[5], 'class="star" disabled="disabled"') .
sprintf (TEXT_REVIEW_BY, tep_output_string_protected ($reviews['customers_name']) ) . ', ' .
tep_date_long ($reviews['date_added']);
?>
</td>
</tr>
<tr>
<td valign="top" class="main">
<?php
echo tep_break_string (tep_output_string_protected ($reviews['reviews_text']), 60, '<br>') . ( (strlen ($reviews['reviews_text']) >= 100) ? '<br>' : '') .
'... ' .
'<a href="' . tep_href_link (FILENAME_PRODUCT_REVIEWS_INFO, 'products_id=' . $product_info['products_id'] . '&reviews_id=' . $reviews['reviews_id']) . '">' .
'Read Full Review</a>';
?>
</td>
</tr>
<?php
} // if ($row
} // while ($reviews
?>
<tr>
<td><?php echo tep_draw_separator ('pixel_trans.gif', '100%', '10'); ?></td>
</tr>
<?php
} // if ($num_rows
} // if (SPECIFICATIONS_MAX_REVIEWS
?>
<tr>
<td><?php echo tep_draw_separator ('pixel_trans.gif', '100%', '10'); ?></td>
</tr>
</tbody></table>
<?php
the end result looks like this:
http://i.imgur.com/LQyGtHG.png
note that one review is 5 stars and the other is 2 stars. So it is not even the correct amount of starts, but also merging them together.
However if I only have 1 review active in this case the 2 star it works perfectly:
http://i.imgur.com/zipoGqR.png
This is the widget: http://www.fyneworks.com/jquery/star-rating/