array variable assignment problems

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bianster
Forum Newbie
Posts: 13
Joined: Sun Feb 08, 2004 3:47 am

array variable assignment problems

Post by bianster »

I have problems with the section of code below, data stored by $salesPerson is an array containing other arrays as its elements. Now the main problem is that the for loop performs an indefinite loop. I still can't see what's the problem after spending several hours on this. Assigning values like this works fine -> $salesPerson[0]['totalComm'] = 0;

Code: Select all

<?php
		$salesPeople = new salesPerson();
		$salesPerson = $salesPeople->getSalesPersonnel();
		if($salesPerson['success'] == 0){
			print($salesPerson['errMsg']);
			return;
		}
		//remove the first array element containing 'None'
		array_shift($salesPerson);
		//initialise array elements
		for($i=0;$i<count($salesPerson);$i++){
			$salesPerson[$i]['totalComm'] = 0;
			$salesPerson[$i]['commPercent'] = 0;
			$salesPerson[$i]['exhibition'] = 0;
			$salesPerson[$i]['exhibitionSales'] = 0;
			$salesPerson[$i]['showroom'] = 0;
			$salesPerson[$i]['showroomSales'] = 0;
			$salesPerson[$i]['referral'] = 0;
			$salesPerson[$i]['referralSales'] = 0;
			$salesPerson[$i]['designer'] = 0;
			$salesPerson[$i]['designerSales'] = 0;
			$salesPerson[$i]['contractor'] = 0;
			$salesPerson[$i]['contractorSales'] = 0;
		}

?>
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

2 things to try

Code: Select all

// this line
$i<count($salesPerson);
have you tried doing it when initialising a count value before the loop
for eg

Code: Select all

$count = count($array);
// then
for ($i = 0; $i < count; $i++)
what you are saying should in theory work, its just i never do it that way

also

try just using

Code: Select all

$salesPerson[]['totalComm'] = 0;
i have had similar problems in the past when the $i is incorrect
bianster
Forum Newbie
Posts: 13
Joined: Sun Feb 08, 2004 3:47 am

Post by bianster »

well I'll be damned.....

assigning the count value to a variable first did the trick! Man, it's things like this that make stuff tougher than it is.

Thanks a lot, man
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

no problem :)
Post Reply