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

Link to comment
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.

 

 

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.