Changing text depending on rounded total from database
On a website I have a number of small PHP scripts to automate changes to the text of the site, depending on a figure that's calculated from a MySQL database. The site is for a fundraising group, and the text in question on the home page gives the total amount raised.
The amount raised is pulled from the database and rounded to the nearest thousand. This is the PHP I use to round the figure and find the last three digits of the total:
$query4 = mysql_query("SELECT SUM(amountraised) AS full_total FROM fundraisingtotal;");
$result4 = mysql_fetch_array($query4); $fulltotal = $result4["full_total"]; $num = $fulltotal + 30000; $ftotalr = round($num,-3); $roundnum = round($num); $string = $roundnum; $length = strlen($string); $characters = 3; $start = $length - $characters; $string = substr($string , $start ,$characters); $figure = $string;
(£30,000 is the amount that had been raised by the previous fundraising team from when the project first started, which is why I've added 30000 to $fulltotal for the $num variable)
Currently the text reads:
the bookstall and other fundraising events have raised more than £<? echo number_format($ftotalr); ?>
I've just realised though that because the PHP is rounding to the nearest thousand, if the total's for example £39,200 and it's rounded to £40,000, to say it's more than £40,000 is incorrect, and in that case I'd need it to say 'almost £40,000' or something similar. I obviously need to replace the 'more than' with a variable.
Obviously I need to test whether the last three digits of the total are nearer to 0 or 1000, so that if the total was for example £39,2000, the text would read 'just over', if it was between £39,250 and £39,400 something like 'over', between £39,400 and £39,700 something like 'well over', and between £39,700 and £39,999, 'almost.'
I've managed to get the last three dig开发者_运维知识库its of the total as a variable, and I think I need some sort of an if/else/elseif code block (not sure if that would be the right approach, or whether to use case/break), and obviously I'm going to have to check whether the figure meets each of the criteria, but I can't figure out how to do that. Could anyone suggest what would be the best way to do this please?
Instead of nested if else
blocks, you could have a function that takes in the dollar amount and inside the function throw the variable in a switch case
statement and at a particular case return the correct echo
'd text.
Switch cases provide readability over several if else
statements. If you need coIde examples, let me know.
Update:
Performing comparisons on integers only, switch case
is the best bet. When comparing conditions that result in a boolean value, using if else
is what you need.
I had took your code and switched the case
to be if else
and here is what it looks like with an example output:
function qualifier($figure) {
if ($figure < 249) return "just over";
elseif ($figure < 399) return "over";
elseif ($figure < 699) return "well over";
elseif ($figure < 999) return "almost";
else return "No number entered";
}
echo "We are " . qualifier(250) . " our goal!";
// outputs: "We are over our goal!"
The simplest and most elegant solution imho would be, like Lizard pointed out, to floor the value and always say "over". You could even make exception for the first-1000 problem and floor anything below that to the next 100 or so, something like...
<?
function getFlooredText($amount) {
if($amount < 1000) {
return floor($amount/100)*100;
} else {
return floor($amount/1000)*1000;
}
}
echo "We've raised over £" . getFlooredText(455) . "\n"; // 400
echo "We've raised over £" . getFlooredText(1455) . "\n"; // 1000
echo "We've raised over £" . getFlooredText(38800) . "\n"; // 38000
?>
If you absolutely don't want that you could always do a four or five layer if-sandwich, it would be the easiest approach towards the idea you described but maybe not the prettiest. Something like this, if I understood where you were going with it...
<?
function getRoundedText($amount) {
$n = substr($amount, -3);
if($n < 250) {
return "just over £" . round($amount, -3);
} elseif($n < 500) {
return "well over £" . round($amount, -3);
} elseif($n < 750) {
return "close to £" . round($amount, -3);
} elseif($n < 1000) {
return "almost £" . round($amount, -3);
}
}
echo "We've raised " . getRoundedText(38200) . "\n";
echo "We've raised " . getRoundedText(38400) . "\n";
echo "We've raised " . getRoundedText(38700) . "\n";
echo "We've raised " . getRoundedText(38900) . "\n";
?>
Hope that helps.
You might not need to change the text... take a look at the floor
function http://php.net/manual/en/function.floor.php
It will always round down, so you can still say 'more than'.
I would use "approximately" to cover all cases!
精彩评论