I have class with a few methods... I call one method and use foreach to loop through the data. During the loop I set a variable to call a method from the same class to get quantities. However, I can't unset that variable and as it loops it adds to the initial value of the variable from the first iteration.

        $cart = new search();
	//get qty loc array
	$qq = array();
	$qc = 1;
	$whs = $cart->warehouseData($conn, $schema);

foreach($whs as $aW=>$bW){

	$qTotals = $cart->checkAvlQtyOneLoc($conn, $item, $aW, $schema);
	$qq[$qc]['location'] = $aW;
	$qq[$qc]['qty'] = $qTotals;

	$qc++;

	$qTotals = null;
	unset($qTotals);
	$qTotals = '';
}

echo '<pre>'.print_r($qq,1).'</pre>';

edit:
just to add to this the return I should get on my $qq[$qc]['qty'] should be: 8,1,1,1,1,..... but instead it returns 8,9,10,11,12,...... etc.

I'm not sure really sure how to unset that $qTotals or if I am doing something completely wrong here. I did read something that mentions setting it to NULL and using unset() but that didn't help.

I appreciate any insight. Thanks..

    My guess is that $cart->checkAvlQtyOneLoc() is storing/updating an object property, so just unsetting your local variable has no affect on that. Without insight into that class and method, though, I do not know what the best (if any) solution would be.

      Thanks for the reply...

      Here is that method:

      	public $qtyOH = 0; //onhand qty
      	public $qtyRS = 0; //reserve qty
      	public $qtyBO = 0; //back order qty
      	public $grandQty;
      	public $itemOneLocation;
      
      public function checkAvlQtyOneLoc($conn, $item, $jdsID, $schema){
      	$this->itemOneLocation = $item;
      
      	$sqlOne = "SELECT IQTYOH, IQTYBO, IQTYRS, IITEM, ISELL 
      			FROM $schema.itemmast 
      			WHERE iitem = '".db2_escape_string($this->itemOneLocation)."'
      			AND isell > '0'
      			AND ico = '".db2_escape_string($jdsID)."'";
      	$resOne = db2_exec($conn,$sqlOne) or die(db2_stmt_errormsg());
      
      	while($rowOne = db2_fetch_assoc($resOne)){
      		$this->qtyOH += trim($rowOne['IQTYOH']);
      		$this->qtyBO += trim($rowOne['IQTYBO']);
      		$this->qtyRS += trim($rowOne['IQTYRS']);
      	}
      
      	$this->grandQty = $this->qtyOH - $this->qtyBO - $this->qtyRS;
      
      	return $this->grandQty;
      
      	$resOne = '';
      
      //end method checkAvlQtyOneLoc();
      }
      
      

      I don't have any __destruct()'s in that class and the other method is just doing a db select and returning the data and that's what I am looping through.

        Just trying different things I found that if I don't reference my properties with $this-> it returns the correct values. Is that mistake on my part to reference those properties in that method?

          So this looks to me like where it keeps a running total:

                  $this->grandQty = $this->qtyOH - $this->qtyBO - $this->qtyRS;
          
              return $this->grandQty; 
          

          If each call within that loop is supposed to be essentially independent of the others, then you either need to instantiate that object within the loop instead of before it, or else reset its $grandQty property (if it is publicly accessible, either directly or via a setter method).

            Your $qtyOH, $qtyRS, $qtyBO, and $grandQty are all object scope properties, which means that any method in your object can modify the values of those properties, and the potentially updated or modified values of those properties will persist until either they're specifically reset, or the object is destroyed and a new instance created. So, despite the fact that you're running the loop on what appear to be different data sets, all the loops are using the values from the previous iteration. This is why you'll get the proper values when you don't use '$this->' - without that, you're using a local scope variable that gets reset on each new call to the checkAvlQtyOneLoc() method. Is there a need to keep the values between data sets? If not, don't worry about the object properties - remove them and use the in-loop variables. If you do need the data to persist, I'd recommend doing all your calculations with local scope variables, then assign those values to an object scoped array property so you can recall the value at a later time.

              Thanks for info! That helps clarify some concepts I wasn't grasping. Appreciated. I will mark this resolved.

                Write a Reply...