dark_hatchling Posted February 26, 2009 Share Posted February 26, 2009 ok i need some help with this star rating system the guy wants me to add to his website, basically he wants people to be able to leave feedback along with a star rating 1-5, basic idea. now ive got the PHP all set up so far so that it inserts the "feedback" and "rating" values into a database..right now the rating is just set up as an integer(1-5)...my question is, how can i make the integer value relate to images of stars and post it for each feedback..like a rating of 1 will show 1 star image for that feedback, etc. thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/ Share on other sites More sharing options...
MadTechie Posted February 26, 2009 Share Posted February 26, 2009 a simple loop would work! //$row = array from DB containing rate $row['rate'] = 3; /ie for($n=0;$n<$row['rate'];$n++) { echo "<img src='star.png'>"; } Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/#findComment-771936 Share on other sites More sharing options...
dark_hatchling Posted February 26, 2009 Author Share Posted February 26, 2009 ahhh! dude..you're awesome...just what im needing right now. yeah im new to php/mysql, thanks ALOT man. Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/#findComment-771957 Share on other sites More sharing options...
allworknoplay Posted February 26, 2009 Share Posted February 26, 2009 What about showing half stars? I think on Amazon, they will go further and even show a half star so say you have 4 1/2 stars...it would be 4 stars and a half filled star.... Of course you'd need to provide a "half star" image, but how do you think the loop would be structured? Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/#findComment-771981 Share on other sites More sharing options...
scarhand Posted February 26, 2009 Share Posted February 26, 2009 What about showing half stars? I think on Amazon, they will go further and even show a half star so say you have 4 1/2 stars...it would be 4 stars and a half filled star.... Of course you'd need to provide a "half star" image, but how do you think the loop would be structured? I would do it by making rating images called "0.5.gif" "1.0.gif" etc..then use the following code to echo it: <?php $rating = mysql_result(mysql_query("select rating from table where id='$id'"), 0); echo "<img src=\"ratingimages/$rating.gif\">"; ?> You would just have to make sure that the ratings in the table would be in the format of 0.0 and could only be divided by 0.5, or however specific you want. Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/#findComment-771996 Share on other sites More sharing options...
MadTechie Posted February 26, 2009 Share Posted February 26, 2009 Personally, 1 to 5 with half stars is 1 to 10.. so rate goes upto 10! to do the halfstar image.. just need to know what star to put on screen <?php $left = "leftsidestar.png" $right = "rightsidestar.png" //$row = array from DB containing rate $row['rate'] = 6; //ie (from 1 to 10) (or 5 x 2) $full = true; for($n=0;$n<$row['rate'];$n++) { $img = ($full)?$left:$right; echo "<img src='$img'>"; $full = !$full; } ?> @scarhand While your solution is sound, it will require more images and thus more downloads and more updating if the design was changed.. these are all pretty minor so not a big deal, however i would use 1 to 10 like star1.gif, star2.gif, star3.gif, etc because the user only see's the stars not the image names (and i think its easier not having 0.5.jpg) also the DB as you can use a tinyint. instead of a float Another (fancy) option is to create an image which is 5 stars long (max rating) Put it inside a div and set the class overflow to hidden now you could just workout the length.. so 5 star image is 100px thus each star is 20px or 10px per half star now just set the div length to rate * 10 ie 3*10 = 30 so div length=30px which makes 1 and a half stars visible! the good thing about this is you can have the stars different colour or whatever! and the code doesn't change. But thats going on a bit.. 101 ways to do the same thing Quote Link to comment https://forums.phpfreaks.com/topic/147034-help-with-simple-star-rating-system/#findComment-772007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.