Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A more simple way of doing this?

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
Code:
	if ($tut_rating == 5.0)	$gfx = "5_0.gif";
	if (($tut_rating >= 4.5) && ($tut_rating <= 4.99))	$gfx = "4_5.gif";
	if (($tut_rating >= 4.0) && ($tut_rating <= 4.49))	$gfx = "4_0.gif";
	if (($tut_rating >= 3.5) && ($tut_rating <= 3.99))	$gfx = "3_5.gif";
	if (($tut_rating >= 3.0) && ($tut_rating <= 3.49))	$gfx = "3_0.gif";
	if (($tut_rating >= 2.5) && ($tut_rating <= 2.99))	$gfx = "2_5.gif";
	if (($tut_rating >= 2.0) && ($tut_rating <= 2.49))	$gfx = "2_0.gif";
	if (($tut_rating >= 1.5) && ($tut_rating <= 1.99))	$gfx = "1_5.gif";
	if (($tut_rating >= 1.0) && ($tut_rating <= 1.49))	$gfx = "1_0.gif";
	if (($tut_rating >= 0.5) && ($tut_rating <= 0.99))	$gfx = "0_5.gif";
	if (($tut_rating >= 0  ) && ($tut_rating <= 0.49))	$gfx = "0_0.gif";

Is there a more simple way of doing the above? Even if it requires changing the file name.

Karl Blessing
PHP/MySQL Developer
 
4.995?..
(int)($tut_rating*2) is exactly an index in an array with "0_0.gif", "0_5.gif" etc. Check on bounds >= 0 & <= ... and go on...
May be it helps?...
 
I think this works:

Code:
<?php
if (isset($tut_rating)) {
$rat_rounded = round($tut_rating, 1);
$gfx = str_replace(".", "_", $rat_rounded) . ".gif";
}
?>

<img src="<?=$gfx?>" alt="Rated: <?=$rat_rounded?> of 5" height="20" width="20" border="0" />
ps. I did not test it!!

Olav Alexander Mjelde
Admin & Webmaster
 
sorry, this will work I think:
Code:
<?php
$rat_rounded = ((round(($tut_rating * 2), 0)) / 2);
$gfx = str_replace(".", "_", $rat_rounded) . ".gif";
}
?>

what it does?

it starts in the inner (), where it takes $tut_rating and multiplies with 2.
It then rounds that to the nearest INTEGER (not float).
After that is rounded, it divides by 2.

Example:
3.4 * 2 = 6.8
ROUND(6.8) -> 7
7 / 2 = 3.5

0.4 * 2 = 0.8
ROUND(0.8) -> 1
1 / 2 = 0.5

then, it replaces . with _, in result.

eg. 0.5 -> 0_5
it also appends with .gif:
0_5 -> 0_5.gif


I theory it should work now! :) good luck!
I dont have ftp or ssh access here, so I cant test..

Olav Alexander Mjelde
Admin & Webmaster
 
If you want to always round down to the nearest .5, replace
Code:
round(($tut_rating * 2), 0)
with
Code:
floor($tut_rating * 2)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top