【发布时间】:2012-03-22 20:57:40
【问题描述】:
我正在查询数据库,将结果值插入到数组中,然后对其进行操作,然后我想使用数组中的mysql_update 更新受影响行中的 每个 值,这就是我我遇到了麻烦。
这是我的代码 - 请协助:
name sellerid quantity
-------------------------
john 12 10
joel 23 20
brian 40 10
让我们把它作为查询结果,有人订购了 25 件商品,现在程序是拿走这些商品并将它们分配给订购的人,然后从卖家那里扣除。
$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;
}
【问题讨论】:
-
这个问题最近已经发布:stackoverflow.com/questions/9531487/…。我认为不够清楚,因此缺乏回应
-
@lng 但显然这是一个不同的问题,与之前提出的问题不同......我尽可能地简化它。
标签: php mysql arrays sorting multidimensional-array