delphi123 Posted November 25, 2007 Share Posted November 25, 2007 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!) Quote Link to comment Share on other sites More sharing options...
Distant_storm Posted November 25, 2007 Share Posted November 25, 2007 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 Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 25, 2007 Share Posted November 25, 2007 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; ?> Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 25, 2007 Share Posted November 25, 2007 this is a very helpful script! I'm also trying it as we speak - can you tell me why my star images appear at the side of the page? I've put it in exactly the same place as my numbers, but the images appear about 300px to the left? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 25, 2007 Share Posted November 25, 2007 No idea. It'll be an html/css issue. Quote Link to comment Share on other sites More sharing options...
katie77 Posted November 25, 2007 Share Posted November 25, 2007 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>" Quote Link to comment Share on other sites More sharing options...
Barand Posted November 25, 2007 Share Posted November 25, 2007 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 ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted November 26, 2007 Share Posted November 26, 2007 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! Quote Link to comment Share on other sites More sharing options...
Barand Posted November 26, 2007 Share Posted November 26, 2007 Depends where you draw the line between 9.8 being rounded up and 9.62 rouded down to 4.5 stars Quote Link to comment Share on other sites More sharing options...
delphi123 Posted November 26, 2007 Author Share Posted November 26, 2007 amazing guys - thanks for all your help - works better than I could have hoped! ;D ;D ;D Quote Link to comment 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.