- This topic is empty.
-
AuthorPosts
-
January 8, 2015 at 3:22 am #192606bruce-1980Participant
Hi
I hope you dont mid me joining the conversation. I’ve been following your post and have found it very useful up to now but I seem to have a problem with the rounding.
Here is my code first of all…
<php $sql = "SELECT AVG(rating) FROM artiste_reviews WHERE artiste_author_id=$author"; $result = mysql_query($sql) or die(mysql_error()); while($reviewrow = mysql_fetch_array($result)) { ` // round down to get number of whole stars needed $wholeStars = floor( $reviewrow['AVG(rating)'] ); // double, round, take modulo. // this will be 1 if you have a half-rating, 0 if not. $halfStar = round( $reviewrow['AVG(rating)'] * 2 ) % 2; // display blank stars. $blankStar = 5 - $wholeStars - $halfStar; // this will hold your html markup $HTML = ""; // write img tags for each whole star for( $i=0; $i<$wholeStars; $i++ ){ $HTML .= "<img src='/Graphics/rating-star-full.png' alt='Whole Star' />"; } // write img tag for half star if needed if( $halfStar ){ $HTML .= "<img src='/Graphics/rating-star-half.png' alt='Half Star' />"; } // write img tag for blank stars if needed for( $i=0; $i<5-$wholeStars-$halfStar; $i++ ){ $HTML .= "<img src='/Graphics/rating-star-empty.png' alt='Blank Star' />"; } // all done print $HTML; ` } >
I’ve added $blankstar too to display the full 5 stars, filled, half filled or empty.
The problem is if for example my average value is output at 3.6 I get the correct 3 and a half stars displayed but if the average is say 3.8, I only get 3 stars displayed.
The FLOOR $wholestars is returing 3 and the ROUND $halfStar is returning 0 which is correct but I cant figure out how to get it display an extra star for the rounded up remainder of 0.8 to get the 4 stars as it should be.
You can see my working page at http://www.our-pub.co.uk/Artists/TomHolden2.php
Hope you can help! :)
January 8, 2015 at 6:18 am #192633Paulie_DMemberI hope you dont mind me joining the conversation.
The conversation ended a year ago.
Necro post.
January 8, 2015 at 6:42 am #192635TomParticipant@Paulie_D I hope you don’t mind me responding to @bruce-1980 to give him a tip even though this is a dead post.
@bruce-1980 The mysql extension in PHP is deprecated so you should consider switching over to either the mysqli extension or PDO to future proof your scripts. I also highly recommend looking into prepared statements to help prevent SQL injection.January 8, 2015 at 10:44 am #192647MBMParticipantI’ll pass on my code within the next day. I resolved this a while back including the rounding but I don’t code anymore unless I can help it so wouldn’t know exactly what I did.
January 16, 2015 at 5:25 am #193416bruce-1980ParticipantThanks for the replies guys, didn’t realise the thread was a year old! Just saw January 6… didn’t notice the 2014 bit!
I’ve implemented the star ratings without the halves for now but I’ll try the code above and see if I can get it going!
And I’ll replace the mysql functions with mysqli
Thanks again! :)January 16, 2015 at 5:45 am #193419MBMParticipantIt’s all there. The code is marked up so you should be able to figure out what each section is doing.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.