Jump to content

[SOLVED] convert numbers to stars?


delphi123

Recommended Posts

Hi there,

 

I've got a database with values in the tables from 0-10.

 

Can anyone give me any idea how I could make a php system that'd convert these values to star images?

 

I've got three images, a full star, a half filled star and an empty star - so with these I can have ten values in a five star rating (including .0 increments).

 

Problem a) is how can I get the php to pick the correct value - eg 9.7845 needs to round to 10 whereas 9.65434 rounds down to 9.5.

 

Problem b) is how on earth I can convert this to star values - ie so the 10 becomes five star images in a row and 9.5 become four full stars and a half image...

 

Any ideas?

 

Some people suggested javascript, but I'm even worse at that! (if that's possible!)

Link to comment
Share on other sites

If your not a javascript wizard and that easiest way it to makes images

 

0 star filled

half star filled

1 star filled

and so forth images

 

then us a switch to get the value from the database round that off and then use a switch like this

 

 

switch ($database_value) {

 

case 1:

 

echo "<img src=star1.gif>";

 

break;

 

}

 

 

thats a very short and crude example

Link to comment
Share on other sites

Something like:

 

<?php

function to_stars($num){
	$half_star = false;
	$stars_echoed = 0;
$rating = round($num*2,0)/2; //achieves rounding to a half
$full_stars = floor($rating/2); //the number of full stars
if($rating % 2 != 0){//if we have a half star
	$half_star = true;
}
while($full_stars > 0){//output full stars
	echo '<img src="path/to/your/full/star" />';
	$full_stars-= 1;
	$stars_echoed++;
}
if($half_star === true){//output half star if needed
	echo '<img src="path/to/your/half/star" />';
	$stars_echoed++;
}
while($stars_echoed < 5){//if we've output less than 5 stars, output the remaining as empty stars
	echo '<img src="path/to/your/empty/star" />';
	$stars_echoed++;
}
}
to_stars(0.98);//half a star
to_stars(9.62);//4 and a half stars;
?>

Link to comment
Share on other sites

well that's the strange thing, if I view the source I can see the stars are physically outside the table?

 

<img src="images/tinystarfull.gif" /><img src="images/tinystarfull.gif" /><img src="images/tinystarfull.gif" /><img src="images/tinystarfull.gif" /><img src="/images/tinystarempty.gif" /><div class='ratingbg'>   
						<table width='650px' height='121px' border='0' cellspacing='0' class='ratingtable'> ....etc

 

In the php the code calling the function is inside the table in one of the cells:

 

<td class='ratingtable_stars'>"                					.to_stars($table['rating_modelling'])."<br /></td>"

Link to comment
Share on other sites

A little briefer

<?php
function toStars ($n)
{
    $star = floor($n/2);
    $half = $n - ($star*2) >= 0.5 ? 1 : 0;
    $blank = 5 - ($star + $half);
    
    return str_repeat("<img src='star.gif'>", $star) 
            . str_repeat("<img src='halfstar.gif'>", $half)  
            . str_repeat("<img src='blank.gif'>", $blank);
}

echo toStars(0.98);    // 1 half, 4 blank
echo toStars(9.62);     // 4 stars, 1 half

?>

Link to comment
Share on other sites

Barand: Your code wouldn't display the right thing for something like 9.8 which should be rounded to 10 first. I think you do need to round to a half first:

 

<?php
function toStars ($n)
{
	$n = round($n*2,0)/2;
    $star = floor($n/2);
    $half = $n - ($star*2) >= 0.5 ? 1 : 0;
    $blank = 5 - ($star + $half);
    
    return str_repeat("<img src='star.gif'>", $star) 
            . str_repeat("<img src='halfstar.gif'>", $half)  
            . str_repeat("<img src='blank.gif'>", $blank);
}

echo toStars(0.98).'<br />';    // 1 half, 4 blank
echo toStars(9.62).'<br />';     // 4 stars, 1 half
echo toStars(9.;// 5 stars

?>

 

Your solution was much neater however - there's a surprise. Not!

 

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.