i am querying the database,inserting the resultant values in an array then manipulating them and thereafter i want to update each of values in the affected rows using mysql_update from the array and that's where i am having trouble.this is my code.kindly assist.

name    sellerid  quantity
john        12           10
joel         23           20
brian       40           10

let's take that as the quey result and someone orders 25 items now the program is to take the items and assign them to one who ordered then deduct from the sellers.

  $cursor="SELECT itemquantity,sellerid FROM mytable WHERE  price='$price'";                     
//it is a table containing data about people selling their commodities $foundItems = array(); // likely to be a parameter of a function... $totalUnitsOrdered = 25; // maps user to amount assigned from him $assignedQuantityPerUser = array(); while ( $row = mysql_fetch_assoc( $cursor ) ) { // Still order Quantity left? if ( 0 < $totalUnitsOrdered ) { if ( $row[ "itemquantity" ] <= $totalUnitsOrdered ) { if (!isset($assignedQuantityPerUser[$row["sellerid"]])) { $assignedQuantityPerUser[$row["sellerid"]] = 0; } // assign all of $row[ "itemquantity" ] $totalUnitsOrdered -= 0 + $row[ "itemquantity" ]; $assignedQuantityPerUser[ $row[ "sellerid" ] ] += 0 + $row[ "itemquantity" ]; } else { // assign all the rest: $totalUnitsOrdered $totalUnitsOrdered = 0; $assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered; } } $newItem[] = $row[ "sellerid" ]; $newItem[] = $row[ "itemquantity" ]; // Append $newItem to the end of $foundItems $foundItems[] = $newItem; }

😕

    Few things before we get to your actual question... this:

              if (!isset($assignedQuantityPerUser[$row["sellerid"]])) { 
                $assignedQuantityPerUser[$row["sellerid"]] = 0; 
              } 
    
          // assign all of $row[ "itemquantity" ] 
          $totalUnitsOrdered  -= 0 + $row[ "itemquantity" ]; 
          $assignedQuantityPerUser[ $row[ "sellerid" ] ] += 0 + $row[ "itemquantity" ]; 

    could be more simply written like this:

              // assign all of $row[ "itemquantity" ] 
              $totalUnitsOrdered -= $row[ "itemquantity" ]; 
              $assignedQuantityPerUser[ $row[ "sellerid" ] ] = 0;

    assuming that the 'sellerid' column has a UNIQUE or PRIMARY index (in other words, the 'sellerid' value uniquely identifies each row in the table).

    Next, note that this:

              // assign all the rest: $totalUnitsOrdered 
              $totalUnitsOrdered   = 0; 
              $assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered; 

    is incorrect; that last statement is always going to be adding 0 since that's the value you just assigned to $totalUnitsOrdered in the preceding statement. For that section to make sense, I believe you'll want to reverse the order of those two statements.

    Next, you've got these two statements:

          $newItem[] = $row[ "sellerid" ]; 
          $newItem[] = $row[ "itemquantity" ]; 

    which don't make sense. Where is $newItem ever defined and/or initialized? What is the purpose of the information you're storing in that array, and why do you need to duplicate that information when it's already being stored in $assignedQuantityPerUser?

    And for the last statement:

          // Append $newItem to the end of $foundItems 
          $foundItems[] = $newItem; 

    the same set of questions from above apply here as well.

    Finally, since those last two arrays appear to be superfluous, and since it would appear to make sense to terminate the while() loop once you've achieved your desired quantity, I would suggest combining this if() statement:

          // Still order Quantity left? 
          if ( 0 < $totalUnitsOrdered ) { 

    with the conditional inside the while() loop. In other words, the loop will only continue if a) there are more rows in the result set to process, AND b) our quantity variable is still positive (indicating we still need more items to fill the current order).

    Assuming you've been following along with all of the proposed changes so far, what you'll want to do next outside of the while() loop is to check if $totalUnitsOrdered is 0; if not, then we've exhausted all rows in the DB result set and still didn't come up with enough quantity to satisfy the order - you'll likely want to stop processing the order and output an error message at this point.

    Otherwise, what you've got now is an array $assignedQuantityPerUser in which the keys are the IDs of sellers whose quantities need to be updated and the values are the number of items which you need to subtract from the sellers' respective quantities. To do that, you'll simply want to use a [man]foreach/man loop over that array (extracting both the key and value) and executing SQL queries in the form of:

    UPDATE myTable
    SET itemquantity = itemquantity - $value
    WHERE sellerid = $key

    (where $key and $value are, of course, the key and value from the foreach() loop).

    Also note that all of the above should probably be done inside of a transaction so that you don't accidentally leave the DB in an inconsistent state (e.g. your UPDATE query failed, meaning the the quantities were never decreased; or the update query succeeds but your script crashes and the order is never actually processed). See the following MySQL manual pages for more info on transactions: 1.8.5.3. Transactions and Atomic Operations and 12.3.1. START TRANSACTION, COMMIT, and ROLLBACK Syntax.

      Write a Reply...