Jump to content

Creating a function for this?


TEENFRONT

Recommended Posts

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!

 

Link to comment
https://forums.phpfreaks.com/topic/138585-creating-a-function-for-this/
Share on other sites

<?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.

<?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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.