Jump to content

Is there a more efficient way to handle this?


thebusiness

Recommended Posts

This is my code to general rating stars based on a numeric rating.  It seems there should be a better way to handle this.

 

	if ($row['rating'] >= 0.1 && $row['rating'] < .5) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/miniratin05.png" height="12" width="61">';
}
elseif($row['rating'] >= .5 && $row['rating'] < 1) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating10.png" height="12" width="61">';
}
elseif($row['rating'] >= 1 && $row['rating'] < 1.5) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating15.png" height="12" width="61">';
}
elseif($row['rating'] >= 1.5 && $row['rating'] < 2) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating20.png" height="12" width="61">';
}
elseif($row['rating'] >= 2 && $row['rating'] < 2.5) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating25.png" height="12" width="61">';
}
elseif($row['rating'] >= 2.5 && $row['rating'] < 3) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating30.png" height="12" width="61">';
}
elseif($row['rating'] >= 3 && $row['rating'] < 3.5) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating35.png" height="12" width="61">';
}
elseif($row['rating'] >= 3.5 && $row['rating'] < 4) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating40.png" height="12" width="61">';
}
elseif($row['rating'] >= 4 && $row['rating'] < 4.5) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating45.png" height="12" width="61">';
}
elseif($row['rating'] >= 4.5 && $row['rating'] < 5.1) {
	$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating50.png" height="12" width="61">';
}
else {
$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating00.png" height="12" width="61">';
}	

 

       if ($row['rating'] >= 0.1 && $row['rating'] < .5) {
	$rate = '05';
} // .. the rest of the ifstatements, setting the rate to be the 2 digit number

        $random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating' . $rate . '.png" height="12" width="61">';

 

You can probably stream line it bit better if you wanted, I am just too distracted to really to want to try right now :)

       if ($row['rating'] >= 0.1 && $row['rating'] < .5) {
	$rate = '05';
} // .. the rest of the ifstatements, setting the rate to be the 2 digit number

        $random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating' . $rate . '.png" height="12" width="61">';

 

You can probably stream line it bit better if you wanted, I am just too distracted to really to want to try right now :)

 

I get the idea, I like the way that looks better. That way if I have to change anything I only have to do it once.

This may work for you:

<?php

//function from http://www.php.net/manual/en/function.round.php  user given in notes.
function roundUpTo($number, $increments) {
    $increments = 1 / $increments;
    return (ceil($number * $increments) / $increments);
} 


$rounding = 5;
if($row['rating'] >= .1 && $row['rating'] < 5.1) {
$parts = explode('.',$row['rating']); //next two lines extract the decimal.
$number = $parts[0].$parts[1]; //puts numbers back together.
$rate = roundUpTo($number, $rounding);
}
else {
$rate = '00';
}

$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating' . $rate . '.png" height="12" width="61">';
?>

Here's thebusiness' example without the need for an extra variable

 

$random_game_mini['rate'] = '<img src="'.$setting['site_url'].'/'.$setting['template_url'].'/images/minirating';

if ($row['rating'] >= 0.1 && $row['rating'] < .5) $random_game_mini['rate'] .= '05';
elseif($row['rating'] >= .5 && $row['rating'] < 1) $random_game_mini['rate'] .= '10';
elseif($row['rating'] >= 1 && $row['rating'] < 1.5) $random_game_mini['rate'] .= '15';
elseif($row['rating'] >= 1.5 && $row['rating'] < 2) $random_game_mini['rate'] .= '20';
elseif($row['rating'] >= 2 && $row['rating'] < 2.5) $random_game_mini['rate'] .= '25';
elseif($row['rating'] >= 2.5 && $row['rating'] < 3) $random_game_mini['rate'] .= '30';
elseif($row['rating'] >= 3 && $row['rating'] < 3.5) $random_game_mini['rate'] .= '35';
elseif($row['rating'] >= 3.5 && $row['rating'] < 4) $random_game_mini['rate'] .= '40';
elseif($row['rating'] >= 4 && $row['rating'] < 4.5) $random_game_mini['rate'] .= '45';
elseif($row['rating'] >= 4.5 && $row['rating'] < 5.1) $random_game_mini['rate'] .= '50';
else $random_game_mini['rate'] .= '00';

$random_game_mini['rate'] .= '.png" height="12" width="61">';

 

jcbones has the best solution 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.