returning 0 from a list of values

below is the class which has the problem; most of it can be ignored. the function pos is returning null or 0 although values have been set previosly. this is when the function generate is called. please help.

thanks.

[php]
class listcreate {

private $digitarray = array(array());//first parameter in all instances is the week identifier
private $posarray  = array(array());
private $validcheck = array(array());//holds who has played who up to $week [hometeam][awayteam]; boolean
private $tables = array(array()); //holds how many free tables at venue of team
private $teams;//constant in all weeks
private $week;//id of current working week. prevents need for constant defining of week. use setweek function



public $matchdate = array(); // date of this match

function __construct($n){
	$this->listcreate($n);
}

function listcreate($n){
	
	$this->teams = $n;
	for($x = 1; $x <= $this->teams; $x++){
		for($n = 1; $n <= (($teams - 1 + ($teams % 2)) * 2); $n++){
			$this->digitarray[$n][$x] = 1;
			$this->posarray[$n][$x] = $x;
		}
		$this->validcheck[$x][$x] = true;
	}
	$this->week = 1;
}

function generate(){
	
	$this->listcreate($this->teams);
	
	for($x = 1; $x <= $this->teams; $x++){
			echo$this->posarray[$this->week][$x] . $this->digit($x) . $this->digitarray[$this->week][$x] . '<br />';
		}
	
	$placearray = array();
	
	$this->week = 1;
	
	$maxlength = ($this->teams - 1 + ($this->teams % 2)) * 2;
	echo $this->pos(1);
	while($this->week <= $maxlength ){
	
	$placearray = $this->posarray[$this->week];
		
	while(!$this->isValid()){
		$this->posIncr();
		for($x = 1; $x <= $this->teams; $x++){
			echo$this->posarray[$this->week][$x] . $this->digit($x) . $this->digitarray[$this->week][$x] . '<br />';
		}
		
		if($this->posarray[$this->week] == $placearray){
			$this->prevWeek();
			echo'<br />Prev';
			
		}
	}
	$this->nextWeek();
	echo'<br />Next';
	}
}

function setWeek($week){//sets which week the changes are made to.
	$this->week = $week;
}

function isValid(){
	$valid = true;
	for($x = 1; $x < $this->teams; $x = $x + 2){
		echo$this->pos($x). $x . $this->pos($x+1) . '-';
		$this->validcheck[$this->pos($x)][$this->pos($x+1)] = $this->validcheck[$this->pos($x)][$this->pos($x+1)] + 1;
	}
	echo'<br />';
	for($x = 1; $x < $this->teams; $x++){
		for($n = 1; $n <= $this->teams; $n++){
			echo$this->validcheck[$x][$n];
			if($this->validcheck[$x][$n] > 1){$valid = false;}
		}
	}
	echo'<br />';
	for($x = 1; $x < $this->teams; $x = $x + 2){
		$this->validcheck[$this->pos($x)][$this->pos($x+1)] = $this->validcheck[$this->pos($x)][$this->pos($x+1)] - 1;
	}
}

function nextWeek(){
	for($x = 1; $x < $this->teams; $x = $x + 2){
		$this->validcheck[$this->pos($x)][$this->pos($x+1)]++;
	}
	$this->week++;
}

function prevWeek(){
	$this->week--;
	for($x = 1; $x < $this->teams; $x = $x + 2){
		$this->validcheck[$this->pos($x)][$this->pos($x+1)]--;
	}
}

function digit($n){// n = team id
	
		return $this->digitarray[$this->week][(($this->teams - $n) + 1)]; //negates reversed effect (ie largest base first)
	
}

function digitIncr($n = 1){ //incriments array; smallest base first;
	if(($this->digitarray[$this->week][$n] + 1) > $n){
		$this->digitarray[$this->week][$n] = 1;
		if($n <= $this->teams){$this->digitIncr($n + 1);}
	}else{
		$this->digitarray[$this->week][$n] = $this->digitarray[$this->week][$n] + 1;
	}
}

function pos($n){// n = position; returns team id
	if($n <= $this->teams){
		return $this->posarray[$this->week][$n];
	}else{
		return 0;
	}
}

function posIncr(){// puts positions in array using setpos
	$this->digitIncr();
	$this->clearPos();
	for($x = 1; $x <= $this->teams; $x++){
		echo $this->setPos($this->digit($x), 0);
		$this->posarray[$this->week][$x] = $this->setPos($this->digit($x), 0);
		
	}
}


private function setPos($dig, $pos){//finds corresponding position dependant on its digit.		
	
	while($dig){
		$postaken = true;
		while($postaken){
			$pos++;
			$postaken = false;
			for($x = 1; $x <= $this->teams; $x++){
				if($this->posarray[$this->week][$x] == $pos){$postaken = true;}
			}
		}
		$dig--;
	}
	
	return $pos;
}



private function clearPos(){//clears current array for new values
	for($x = 1; $x <= $this->teams; $x++){
		$this->posarray[$this->week][$x] = 0;
	}
}

}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service