Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with multidimensional arrays

Status
Not open for further replies.

MDLU

IS-IT--Management
Nov 5, 2003
102
US
my array is almost working as designed. the problem is the counter variables: $cat['piece_count'] and $cat['total_price']. They aren't incrementing appropriately, they're just sticking with their inital values... Any thoughts?

Code:
$category=array( array(
						"id"=>-1,
						"name"=>"",
						"piece_count"=>1,
						"total_price"=>1
		) );
		
		$totalinventorysales=0;
		$totalquantity=0;
		while ($line=mysql_fetch_array($piece_result)) {
			$totalinventorysales=$totalinventorysales+$line['price'];
			$totalquantity=$totalquantity+$line['quantity'];
			$newcategory=0;
			$pointer=0;
			foreach ($category as $cat) {
				if ($cat['id']==$line['category_id']) { 
THIS LINE->>					$cat['piece_count']++;
AND THIS LINE->>				$cat['total_price']=$cat['total_price']+$line['price'];
					$newcategory=1;
				}
			}
			if ($newcategory==0) {
				$name=show_category($line['category_id'],$conn);
				array_push($category,array(
							"id"=>$line['category_id'],
							"name"=>$name,
							"piece_count"=>1,
							"total_price"=>$line['price']
				));
			}
		}
		//remove first, blank category
		$name=array_shift($category);
		unset($name);

 
never mind. my foreach statement was confusing me. it turns out that I just used a for() loop and referenced the real variable $category, instead of its alias $cat.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top