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
https://forums.phpfreaks.com/topic/78831-solved-convert-numbers-to-stars/
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

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

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

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

?>

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!

 

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.