TEENFRONT Posted December 27, 2008 Share Posted December 27, 2008 Hey Im after creating a function for this rating system... im kinda lost where to start. I give users a rating out of 5.. for each rating i want to show 1 star image. so if a user has 1 rating, hes 1 star. 2 rating, hes 2 star and so on up to 5 rating and 5 star. right now i have this... if($ratingstar == "5") { $ratingstar="<img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'> <font size='1'>($rating)</font>";} if($ratingstar == "4") { $ratingstar="<img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'> <font size='1'>($rating)</font>";} if($ratingstar == "3") { $ratingstar="<img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'> <font size='1'>($rating)</font>";} if($ratingstar == "2") { $ratingstar="<img src='http://www.8baller.co.uk/star.png'><img src='http://www.8baller.co.uk/star.png'> <font size='1'>($rating)</font>";} if($ratingstar == "1") { $ratingstar="<img src='http://www.8baller.co.uk/star.png'> <font size='1'>($rating)</font>";} if($ratingstar == "0") { $ratingstar="<font size='1'>No Rating Yet <a href='#' title='Teams need at least 3 members to gain a rating'><img src='http://8baller.co.uk/forum/images/toplinks/help.gif' border='0'></a></font>";} So thats basically just adding a star image for each value. Im sure theres an easier/nicer way to do this usiong functions..? But im unsure where to start. Is there a way i can easily say, for each rating number, show a star image. so then i just need to do.. $show_stars = stars(5); and that would show 5 star images.? Cheers! Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/ Share on other sites More sharing options...
premiso Posted December 27, 2008 Share Posted December 27, 2008 <?php function stars($num, $rating) { for ($i=0; $i<$num;$i++) { $ratingstar.="<img src='http://www.8baller.co.uk/star.png'>"; } $ratingstar .= "<font size='1'>($rating)</font>"; if ($num == 0) { $ratingstar="<font size='1'>No Rating Yet <a href='#' title='Teams need at least 3 members to gain a rating'><img src='http://8baller.co.uk/forum/images/toplinks/help.gif' border='0'></a></font>"; } return $ratingstar; } ?> Should do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/#findComment-724620 Share on other sites More sharing options...
Mark Baker Posted December 27, 2008 Share Posted December 27, 2008 str_repeat() Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/#findComment-724621 Share on other sites More sharing options...
TEENFRONT Posted December 27, 2008 Author Share Posted December 27, 2008 <?php function stars($num, $rating) { for ($i=0; $i<$num;$i++) { $ratingstar.="<img src='http://www.8baller.co.uk/star.png'>"; } $ratingstar .= "<font size='1'>($rating)</font>"; if ($num == 0) { $ratingstar="<font size='1'>No Rating Yet <a href='#' title='Teams need at least 3 members to gain a rating'><img src='http://8baller.co.uk/forum/images/toplinks/help.gif' border='0'></a></font>"; } return $ratingstar; } ?> Should do what you want. Hey, how do i use this.. what goes where $num is and $rating? i just have $rating and not $num, il try it with just rating, see what happens lol. much appreciate though! Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/#findComment-724624 Share on other sites More sharing options...
DeanWhitehouse Posted December 27, 2008 Share Posted December 27, 2008 Num is how many stars you want. Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/#findComment-724626 Share on other sites More sharing options...
TEENFRONT Posted December 27, 2008 Author Share Posted December 27, 2008 Sorry yes i was been thick. This is very much appreciated! Works perfectly! I love little snippets like this, helps me alot when building other things on the site. Thank you Merry Late Xmas lol. Quote Link to comment https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/#findComment-724634 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.