从PHP arrya不确定的指标差 [英] undefined index error from a php arrya
问题描述
我正在查询数据库如下:
i am querying the DB as follows:
$result=mysql_query("SELECT name,items FROM mytable WHERE price='$price'");
现在,我想创建一个数组来插入是值这个查询的结果E'G让我们说这是得到的数据:
now,i want to create an array to insert the values that are as a result of this query e'g let's say this is the resultant data:
name sellerid quantity
john 12 10
joel 23 20
brian 40 10
i.ve插入这个数据到一个数组,并希望操纵它。(这是一个交易平台),所以我们说,一个用户想买数组中从数据25项,因此实现这一目标的脚本已采取从约翰的10个项目,15名来自乔尔(即增加了25个),然后将自己的物品的剩余价值也就是约翰的项目= 0和乔尔的项目= 5。
i.ve inserted this data into an array and want to manipulate it.(this is a trading platform),so let's say a user wanted to buy 25 items from the data in the array,and so to achieve this the script has to take the 10 items from john and 15 from joel(that adds up to 25) and then set their items to the remaining value i.e john's items=0 and joel's items=5.
这是code.I我得到一个错误在该行约未定义指数
this is the code.i am getting an error at this line about an undefined index
$assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered;
这是code的REST:
THIS IS THE REST OF THE CODE:
$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
敬请assist.thanks。
kindly assist.thanks.
推荐答案
在使用它之前初始化值:
Initialize the value before you use it:
if (!isset($assignedQuantityPerUser[$row["sellerid"]])) {
$assignedQuantityPerUser[$row["sellerid"]] = 0;
}
$assignedQuantityPerUser[$row["sellerid"]] += $totalUnitsOrdered;
这篇关于从PHP arrya不确定的指标差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!