【发布时间】:2012-03-20 20:23:12
【问题描述】:
我正在查询数据库,如下所示:
$result=mysql_query("SELECT name,items FROM mytable WHERE price='$price'");
现在,我想创建一个数组来插入作为此查询结果的值,例如假设这是结果数据:
name sellerid quantity
john 12 10
joel 23 20
brian 40 10
我已经将此数据插入到数组中并想要对其进行操作。(这是一个交易平台),假设用户想从数组中的数据中购买 25 件商品,因此要实现此脚本必须从 john 中取出 10 个项目,从 joel 中取出 15 个项目(加起来为 25),然后将它们的项目设置为剩余值,即 john's items=0 和 joel's items=5。
这是代码。我在这一行收到关于未定义索引的错误
$assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered;
这是代码的其余部分:
$query="SELECT itemquantity,sellerid FROM mytable WHERE price='$price'";
//it is a table containing data about people selling their commoditities and the program matches buyers and sellers by price
$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 ) {
// assign all of $row[ "items" ]
$totalUnitsOrdered -= 0 + $row[ "itemquantity" ];
$assignedQuantityPerUser[ $row[ "sellerid" ] ] += 0 + $row[ "itemquantity" ];
**//this is where in getting an error:r[ $row[ "sellerid" ] is an undefined index**
} 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;
} // while
请协助。谢谢。
【问题讨论】:
标签: php mysql arrays indexing sorting