j1m2f3 Posted April 2, 2012 Share Posted April 2, 2012 Please help me. I'm fairly new to php and have been looking for answers on Goog for about two weeks or so with not an answer. This is my first post on a forum of any type. I have a form on a php page that when submitted sends a value to a confirm php page <form action='buyconfirmorder.php' method='post'> Purchase: <input type='text' name='ask_shares'><br /><br /> You Will Pay:  <input type='text' name='ask'><br /><br /> <input type='hidden' name='postcardname' value='$postcardname'> <input type='submit' name='submit' value='Order'> </form> I need to take the users input from the form and subtract each row from a mysql DB one at a time until the users input is down to a remainder and then I need the remainder (I'll be working with the remainder as well). $query = mysql_query(" SELECT * FROM ask, search WHERE search.id = ask.card_search_id AND search.card_name = '$postcardname' ORDER BY ask_price ASC"); The array will be numbers only. So an example would be: User input from the form is 100.00 while($rows = mysql_fetch_array($query)) { $ask_price = $rows['ask_price']; } Let's say the array is 50.50, 40.50, 35.00 I need some code to do: 100.00 - 50.50 = 49.50 49.50 - 40.50 = 9.00 9.00 - 35.00 = <0 does not do anything so remainder = 9.00 would it be something like $new_ask_shares = $_POST['ask_shares']; //some sort of a loop or array or combo of both ($new_ask_shares - $ask_price); the numbers are just an example as the actual numbers will be different each time I query my DB. Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/260220-how-to-subtract-an-array-of-a-column-of-rows-from-mysql-db-from-a-form-number/ Share on other sites More sharing options...
marcus Posted April 2, 2012 Share Posted April 2, 2012 $input = 100; $array = array(50.5, 40.5, 35); $remainder = 0; foreach($array AS $num){ $remainder = ($remainder == 0) ? $input : $remainder; $out = $remainder - $num; if($out < 0){ break; } $remainder = $out; } echo $remainder; Just a quick example of how I interpreted your code. I get the result of "9" when I use input=100 and the array=[50.5, 40.5, 35] Give a try and implement my code into yours. Quote Link to comment https://forums.phpfreaks.com/topic/260220-how-to-subtract-an-array-of-a-column-of-rows-from-mysql-db-from-a-form-number/#findComment-1333769 Share on other sites More sharing options...
j1m2f3 Posted April 3, 2012 Author Share Posted April 3, 2012 I thank you for the help and I will need this for the remainder but my main problem is still a puzzle to me. I need not to subtract the actual numbers that I gave for an example. I need to take the actual input from the user form and subtract mysql rows as some sort of array from that. something like: $var- first row of array = answer ($var - first row of array) - second row of array = answer ($var - first row of array - second row of array) - thrid row of array = answer and so on until there is just a remainder left. I will need to use the remainder code you gave me and I thank you but I still need the above solved before I can even use the remainder. Any more help will be great. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/260220-how-to-subtract-an-array-of-a-column-of-rows-from-mysql-db-from-a-form-number/#findComment-1333824 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.