简体   繁体   中英

Subtract a mysql array of rows ORDER BY price, date from a number entered by a form using php

I am new to php and have been searching for weeks for this answer with no luck. This is only the second forum I've ever posted on and it's the first time on this one. Please please please... this is driving me crazy.

The site is like a stock broker site. I have a form page where a user types in a value of price and share.

  <form action='sellconfirmorder.php' method='post'>
  Number of Shares to Sell: 
  <input type='text' name='ask_shares_new'><br /><br />

  Lowest Price Per Share You Will Sell: &nbsp
  <input type='text' name='ask_new'>

  <input type='hidden' name='postcardname' value='$postcardname'>
  <input type='submit' name='submit' value='Sell Shares or Place Ask Order'>
  </form>

Then the confirmation page will (this is where I'm stuck) do some math and upon submit update my database.

I need

$query = mysql_query("
        SELECT * 
        FROM ask, search 
        WHERE search.id = ask.card_search_id
        AND search.card_name = '$postcardname' 
        ORDER BY ask_price ASC
  ");

some sort of array or loop to start from the first row of the DB and subtract from the number the user entered.

so an example of what I need without a loop would look something like this only this would obviously never work:

$example_var - first row of array = answer


($example_var - first row of array) - second row of array = answer

($example_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 anser to each subtraction in my UPDATE code for mysql and I will need to use remainder as well. So I need sort of loops or arrays or combo of the two to do the math and then use those numbers to update my DB. Any help will be great. Thanks again.

$answers = array();

// Loop though your query result
while( $row = mysql_fetch_row( $query ) )
{
    // if $example_var get's below 0, abort
    if ( $example_var - $row[0] < 0 )
         break;

    // subtract the current row's value
    $example_var -= $row[0];

    // save the answer
    $answers[] = $example_var;
}

$answer will then contain the intermediate result of each subtraction, $example_var will contain the remainder.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM