Jump to content

MySQL/PHP Math Problem - Greater than or less than $variable problem.


romeshomey

Recommended Posts

New member here, hello to everyone who was here before me.

I am completely stumped here and hope someone can lead me in the right direction.  My problem has to do with getting a $variable as a result of another $variable being greater than or less than a number.

Say I have defined a variable through my MySQL query for instance:

[code=php:0]
sum($a+$b+$c) as $total
[/code]

How can I do something like this for example with my $total variable.

[code=php:0]
if $total < 15 = 10
if $total > 15 but < 20 = 5
if $total > 21 = -5
as $newtotal
[/code]



Thanks in advance for any assistance.
I must agree with fenway, i am not sure what it is your trying to do ???

Perhaps a CASE would work.

[code=php:0]sum($a+$b+$c) as $total [/code]

then..

[code=php:0]
switch($total) {
case  $total<15 :
    $newtotal= 10;
break;
....etc
}
[/code]

I guess its the same thing that fenway said?
Perhaps this will help in some way.
Sorry, my question is PHP related, not MySQL.  I think I figured it out though.  I'll know in a bit lol.

[code=php:0]
if($total < 16) $total = 10;
if(($total > 15)||($total < 21) $total = 5;
if($total > 20) $total = $total - 5;
[/code]
So i'm assuming the 'newtotal' goes gets lower as the 'total' gets higher
and from you're first example.....10 is the highest...newtotal will ever get

so make $newtotal start at 10
and decrement it gradually


[code=php:0]
$newtotal = 10;
if ($total > 15)
    $newtotal -= 5;
if($total > 21)
    $newtotal -= 5;

echo $newtotal;
[/code]

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.