我正在使用 jquery ui 小部件星级。在我的查询回显之后,所有的星星都会一起显示(如果查询中的评论超过 2 条,则数量也错误,但如果查询中只有 1 条评论,则一切都是正确的)。
这是代码:
此函数将开头的数字 1 分配给数组以了解填充了哪些星星。
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;
}
我回显并直接在 phpmyadmin 中尝试的查询以确保它有效:
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
输出2行。5 列就像它应该的那样:reviews_id reviews_text reviews_rating date_ added customers_name
这是带有查询的完整 while 循环以及回显的表:
<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
最终结果如下所示:
http://i.imgur.com/LQyGtHG.png
请注意,一条评论是 5 星,另一条是 2 星。所以它甚至不是正确的启动数量,而是将它们合并在一起。
但是,如果在这种情况下我只有 1 条评论处于活动状态,那么 2 星它可以完美运行: