Change text based on rounded total from database

On the website, I have a few small PHP scripts to automate changes to the text of the site, depending on a figure calculated from a MySQL database. The site is for the fundraising group and the text referred to on the home page gives the total.

The amount raised is pulled from the database and rounded to the nearest thousand. This is the PHP I use to round a shape and find the last three digits of the total:

$ query4 = mysql_query ("SELECT SUM (amountreised) AS full_total FROM fundraisingtotal;");
            $ result4 = mysql_fetch_array ($ query4);
            $ fulltotal = $ result4 ["full_total"];
            $ num = $ fulltotal + 30,000;
            $ 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 was raised by the previous fundraising team since the project started, so I added 30,000 to the $ total cost for the $ num variable)

Currently, the text reads:
  the bookstall and other fundraising events have raised more than &pound;<? echo number_format($ftotalr); ?>

I just realized that since PHP is rounded to the nearest thousand if the total is, for example, £ 39,200 and rounded to £ 40,000 if it is said more than £ 40,000 is incorrect, in which case I 'he you have to say "almost £ 40,000" or something like that. I obviously need to replace "more" with a variable.

Obviously, I need to check if the last three digits are close to 0 or 1000, so if the amount was, for example, £ 39,000, the text would read "slightly higher" if it was between £ 39,250 and £ 39,400 sterling, something like "more", between 39,400 and 39,700 pounds sterling, something like "much higher", and between 39,700 and 39,399 pounds "almost".

I managed to get the last three digits of the total as a variable, and I think I need some kind of if / else / elseif code block (not sure if this would be the correct approach or use case / break), and obviously I will need to check if the shape meets each of the criteria, but I cannot figure out how to do this. Can anyone suggest what would be the best way to do this please?

+2


source to share


4 answers


Instead of nested blocks if else

, you can have a function that takes a dollar value, and inside the function, outputs a variable in an expression switch case

and, in a specific case, returns the correct text echo

'd.

Switch casings provide multi-operator readability if else

. If you need coIde examples, let me know.

Update:



Comparing only integers switch case

is the best choice. When comparing conditions that result in a boolean value, usage if else

is what you want.

I took your code and switched case

to if else

, and this is what it looks like with the 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!"

      

+1


source


The simplest and most graceful solution imho would be, as Lizard noted, to set the value and always say "over". You can even make an exception for the first 1000 problem and put something below that on 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 &pound;" . getFlooredText(455) . "\n"; // 400
echo "We've raised over &pound;" . getFlooredText(1455) . "\n"; // 1000
echo "We've raised over &pound;" . getFlooredText(38800) . "\n"; // 38000
?>

      

If you absolutely don't want to always be able to do four or five layers of if-sandwich, this would be the simplest approach to the idea you described, but perhaps not the prettiest. Something like this, if I understood where you are going with it ...



<?
function getRoundedText($amount) {
    $n = substr($amount, -3);
    if($n < 250) {
        return "just over &pound;" . round($amount, -3);
    } elseif($n < 500) {
        return "well over &pound;" . round($amount, -3);
    } elseif($n < 750) {
        return "close to &pound;" . round($amount, -3);
    } elseif($n < 1000) {
        return "almost &pound;" . 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 it helps.

+1


source


You may not need to change the text ... take a look at the function floor

http://php.net/manual/en/function.floor.php

It will always be rounded, so you can still say "more".

0


source


I would use "approximately" to cover all cases!

0


source







All Articles