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.
[php]$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;
}
[/php]