Jump to content

Scheduler


chriscloyd

Recommended Posts

Need help the page doesnt do anything, if anyone could guide me in helping me make this work and more efficient

I am making a weekley scheduler

if the team have either played this week dont schedule

or if the two teams have played each other in previous weeks dont schedule them together

here is my scheduler class

<?php
class scheduler {

var $week;
var $max_teams;

function scheduler() {
	global $Settings;
	$get_all = mysql_query("SELECT * FROM teams");
	$this->max_teams = mysql_num_rows($get_all);
	if ($Settings->week == 'Not Started') {
		$this->week = 1;
	} else {
		$this->week = $Settings->week;
	}
}
function CheckTeams($team1,$team2) {
	//check if the two teams have played at all
	//this season
	if ($this->checkPlay($team1,$team2)) {
		return true;
	} else {
		return false;
	}		
}
function checkPlay ($team1,$team2) {
	$week = $this->week;
	$check = mysql_query("SELECT * FROM matches WHERE team1 = '".$team1."' AND team2 = '".$team2."' OR  team1 = '".$team2."' AND team2 = '".$team1."'");
	$num = mysql_num_rows($check);
	if ($num > 0) {
		return false;
	} else {
		$check = mysql_query("SELECT * FROM matches WHERE `team1` = '".$team1."' OR `team2` = '".$team1."' AND `week` = '".$week."' ");
		$nums = mysql_num_rows($check);
		if ($nums > 0) {
			return false;
		} else {
			$check = mysql_query("SELECT * FROM matches WHERE `team1` = '".$team2."' OR `team2` = '".$team2."' AND `week` = '".$week."' ");
			$nums = mysql_num_rows($check);
			if ($nums > 0) {
				return false;
			} else {
				return true;
			}
		}
	}
}
function createMatch ($team1,$team2) {
	$create = mysql_query("INSERT INTO matches (`team1`, `team2`) VALUES ('$team1','$team2')");
	if ($create) {
		return true;
	} else {
		return false;
	}
}
}
?>

 

here is my other script

<?php

//get all teams first and set them in available array
$get_all = mysql_query("SELECT * FROM teams");
$max_teams = mysql_num_rows($get_all);
$i = 1;
$rand_team = rand(1,$max_teams);
while($team = mysql_fetch_assoc($get_all)) {
$available[$i] = 	$team['teamname'];
$i++;
}

// end getting all teams
for ($i = 1; $i < $Scheduler->max_teams; $i++) {
//get the two teams
$get_team1 = rand(1,$Scheduler->max_teams);
$get_team2 = rand(1,$Scheduler->max_teams);
$team1 = $available[$get_team1];
$team2 = $available[$get_team2];
if ($Scheduler->CheckTeams($team1,$team2)) {
	if ($Scheduler->createMatch($team1,$team2)) {
		echo 'Scheduled '.$team1.' to play '.$team2.' in week '.$Scheduler->week.'<br />';
	}
}else {
	$i--;
}


}

?>

Link to comment
https://forums.phpfreaks.com/topic/216035-scheduler/
Share on other sites

ya i think i am so how do i get out haha i update my script and it schedules it but it still comes up with the error

 

class scheduler

<?php
class scheduler {

var $week;
var $max_teams;

function scheduler() {
	global $Settings;
	$get_all = mysql_query("SELECT * FROM teams");
	$this->max_teams = mysql_num_rows($get_all);
	if ($Settings->week == 'Not Started') {
		$this->week = 1;
	} else {
		$this->week = $Settings->week;
	}
}
function CheckTeams($team1,$team2) {
	//check if the two teams have played at all
	//this season
	if ($team1 == $team2) {
		return false;
	}
	if ($this->checkPlay($team1,$team2)) {
		return true;
	} else {
		return false;
	}		
}
function checkTeam ($team) {
	$check = mysql_query("SELECT * FROM matches WHERE team1 = '".$team."' OR team2 = '".$team."' AND week = '".$this->week."'");
	$num = mysql_num_rows($check);
	if ($num > 0) {
		return false;
	} else {
		return true;
	}
}
function checkPlay ($team1,$team2) {
	$week = $this->week;
	$check = mysql_query("SELECT * FROM matches WHERE team1 = '".$team1."' AND team2 = '".$team2."' OR  team1 = '".$team2."' AND team2 = '".$team1."'");
	$num = mysql_num_rows($check);
	if ($num > 0) {
		return false;
	} else {
		return true;
	}
}
function createMatch ($team1,$team2) {
	$week = $this->week;
	$create = mysql_query("INSERT INTO matches (`team1`, `team2`, `week`) VALUES ('$team1','$team2', '$week')");
	if ($create) {
		return true;
	} else {
		return false;
	}
}
}
?>	

 

the index page to schedule and show results

 

<?PHP
session_start();
mysql_connect('localhost',******,******);
mysql_select_db(******);
include("classes/settings.php");
include("classes/scheduler.php");
$Scheduler = new scheduler;

//set arrarys
$available = array();
$playing = array();

//get all teams first and set them in available array
$get_all = mysql_query("SELECT * FROM teams");
$max_teams = mysql_num_rows($get_all);
$i = 1;
$rand_team = rand(1,$max_teams);
while($team = mysql_fetch_assoc($get_all)) {
$available[$i] = 	$team['teamname'];
$i++;
}
// end getting all teams
for ($i = 1; $i < $Scheduler->max_teams; $i++) {
//get the two teams
$get_team1 = rand(1,$Scheduler->max_teams);
$get_team2 = rand(1,$Scheduler->max_teams);
$team1 = $available[$get_team1];
$team2 = $available[$get_team2];
if ($Scheduler->checkTeam($team1)) {
	if ($Scheduler->checkTeam($team2)) {
		if ($Scheduler->CheckTeams($team1,$team2)) {
			if ($Scheduler->createMatch($team1,$team2)) {
				echo 'Scheduled '.$team1.' to play '.$team2.' in week '.$Scheduler->week.'<br />';
				;
			} else {
				$i--;
			}
		} else {
			$i--;
		}
	} else {
		$i--;
	}
} else {
	$i--;
}	
}

?>

Link to comment
https://forums.phpfreaks.com/topic/216035-scheduler/#findComment-1122840
Share on other sites

your queries will likely fetch all rows where team1 = '".$team."' regardless of any of the rest of the query. You need to fix your conditions with parentheses.

 

$check = mysql_query("SELECT * FROM matches WHERE team1 = '".$team1."' AND team2 = '".$team2."' OR  team1 = '".$team2."' AND team2 = '".$team1."'");

 

SHOULD BE:

 

$check = mysql_query("SELECT * FROM matches WHERE (team1 = '".$team1."' AND team2 = '".$team2."') OR  (team1 = '".$team2."' AND team2 = '".$team1."'")) or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/216035-scheduler/#findComment-1122952
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.