Jump to content

how to subtract an array of a column of rows from mysql DB from a form number


j1m2f3

Recommended Posts

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: &nbsp<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

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

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.