Jump to content

[SOLVED] basic mysql php math help


Jiraiya

Recommended Posts

iv looked up and read stuff on php and mysql math functions but i dont understand how to do the code for it. i know how to do the math kinda but im trying to pull the numbers from two different tables in the same database. im trying to pull the four numbers numbers A #B, #C, #D. im trying to take number A and and take 10% of that number and subtract it from number D,and have 10% of #C do the same to #A, and have that keep going until either number A or number C reaches zero. and im also trying to then add a set number to #A or #C that didnt reach zero, is there any way someone could show me a simple script or show me a tutorial for it thanks

Link to comment
Share on other sites

Just break it down step by step. First you need to gather the appropiate information from each table.

 

Use the appropriate queries and pull your info from the db. Then assign a variable to each of the appropriate pieces of information.

 

Lets say you use $a $b $c and $d

 

Then formulate your calculations on the variables in question. So for your first one you want to get 10% of $a and subtract it from $d.

 

$new_a = ($d - ($a * .1));

$new_c = ($a - ($c * .1));

 

As for the rest I am unsure of what you are trying to accomplish. Are you trying to do this: '$new_a' does not equal '0' therefor we need to take 10% of $new_a and subtract it from $d  ?

Link to comment
Share on other sites

Then you'll need to run a while statement to have it run continuously until one of them reaches 0.

 

Run the calculation the first time.

$new_a = ($d - ($a * .1));
$new_c = ($a - ($c * .1));

 

Set the while loop to continuously run until one or the other is at or below 0.

while ((($new_a) && ($new_c)) > '0')
{
$new_a = ($d - ($new_a * .1));
$new_c = ($d - ($new_c * .1));
}

 

when while loop is finished, use an if statement to determine if $new_a is greater than 0

 

if ($new_a > '0')
{
$new_a = ($new_a + 'Set Number to add');
}

 

if $new_a = 0 then increment by set number for $new_c

if ($new_c > '0')
{
$new_c = ($new_c + 'Set Number to add');
}

 

Am i correct in assuming that you want to add the set number to the new variable that didn't quite reach 0?

 

Haven't done coding in awhile. So this is probably far from perfect. There may even be a better way to do it. But this logically makes sense to me. Not sure if my coding is perfect but at least the thought process is laid out.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.