Jump to content

Recommended Posts

Hi need help with this. I am trying to create a schedule for 20 teams, howerver if the number of teams is changed to 20 the script errors out

 

schedule_class.php

<?php
class schedule{
var $history=array();
var $num=8;
var $a=array();
function make_teams(){
	for($i=1;$i<=$this->num;$i++){
		$this->a[]="team ".$i;
	}
}

function remove_history($team1,$team2){
	if(sizeof($this->history)>0){
		foreach($this->history[$team1] as $key => $val){							
			if($val==$team2&&$val){
				$this->history[$team1][$key]=0;
				break;
			}
		}
	}
}

function make_history(){
	for($i=0;$i<$this->num;$i++){
		for($j=0;$j<$this->num;$j++){				
			if($i==$j) $this->history[$this->a[$i]][]=0;
			else $this->history[$this->a[$i]][]=$this->a[$j];
		}
	}
}

function print_teams(){
	print_r($this->teams);
}
function check_temp_history($temphistory,$team){
	if(sizeof($temphistory)>0){
		foreach($temphistory as $val){
			if($val==$team){
				return false;
			}
		}
	}
	return true;
}
function move(){
	$temphistory=array();
	for($i=0;$i<$this->num;$i++){	
		if(sizeof($this->history)>0){		
			foreach($this->history[$this->a[$i]] as $key => $val){
				if($val&&$this->check_temp_history($temphistory,$val)&&$this->check_temp_history($temphistory,$this->a[$i])){	
					$temphistory[]=$this->a[$i];
					$temphistory[]=$val;
					break;
				}
			}
		}
	}
	$this->a=$temphistory;
}

function run(){
	for($j=1;$j<$this->num;$j++){
		for($i=0;$i<$this->num;$i+=2){
			srand();
			$r=rand(0,1);
			if($r==1){
				echo $this->a[$i]."-".$this->a[$i+1];
			}
			else{
				echo $this->a[$i+1]."-".$this->a[$i];
			}
			echo "<br />";
			$this->remove_history($this->a[$i],$this->a[$i+1]);
			$this->remove_history($this->a[$i+1],$this->a[$i]);
		}
		echo "<hr>";
		$this->move();
	}
}
}
?>

 

schedule.php

 

<?php
include("schedule_class.php");

$s=new schedule();
//number of teams
$s->num=8;
//makes the teams
$s->make_teams();
//makes the history-all the possibilities of the teams(team1 can play against all and so on)
$s->make_history();

//prints the schedule
$s->run();

?>

 

Any ideas peeps... thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/144337-help-needed-with-schedule/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.