Jiraiya Posted November 19, 2008 Share Posted November 19, 2008 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 https://forums.phpfreaks.com/topic/133382-solved-basic-mysql-php-math-help/ Share on other sites More sharing options...
Greaser9780 Posted November 20, 2008 Share Posted November 20, 2008 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 https://forums.phpfreaks.com/topic/133382-solved-basic-mysql-php-math-help/#findComment-694320 Share on other sites More sharing options...
Jiraiya Posted November 20, 2008 Author Share Posted November 20, 2008 I want 10% of 'A' to keep subtracting from 'D', and 10% of 'C' to keep subtracting from 'B' until one of 'B' or 'D' reaches Zero and then i need to add a set number to the the variable that is not at zero. Link to comment https://forums.phpfreaks.com/topic/133382-solved-basic-mysql-php-math-help/#findComment-694578 Share on other sites More sharing options...
Greaser9780 Posted November 22, 2008 Share Posted November 22, 2008 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 https://forums.phpfreaks.com/topic/133382-solved-basic-mysql-php-math-help/#findComment-696471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.