-1

i am querying the DB as follows:

$result=mysql_query("SELECT name,items FROM mytable WHERE price='$price'");

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 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.

this is the code.i am getting an error at this line about an undefined index

$assignedQuantityPerUser[ $row[ "sellerid" ] ] += $totalUnitsOrdered;

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

kindly assist.thanks.

1 Answer 1

3

Initialize the value before you use it:

if (!isset($assignedQuantityPerUser[$row["sellerid"]])) {
  $assignedQuantityPerUser[$row["sellerid"]] = 0;
}
$assignedQuantityPerUser[$row["sellerid"]] += $totalUnitsOrdered;
Sign up to request clarification or add additional context in comments.

3 Comments

but 'sellerid' is value retrieved from the database(as you can see from the above table) and therefore shouldn't be initialized to a different value..how do i solve this?..i am not sure whether the code is right.
You are using the sellerid as a KEY to address an element in an array. That element needs to be initialized. You are NOT altering the sellerid itself by doing that.
thanks it worked.problem is that now the code just loops through the values without updating them in the database,how can i update each of the affected values in the DB.how can i update each of the values from the array.thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.