4 Replies - 542 Views - Last Post: 11 February 2012 - 03:05 AM Rate Topic: -----

Topic Sponsor:

#1 smixen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-March 11

PHP/mysql football manager tournament

Posted 06 February 2012 - 03:24 AM

Hi I need help with my football manager tournament.
I am trying to insert all teams that are registered in the game into a table called "timetable" randomly.
The database will have the columns id, team1, team2, date, time.
I know how to do this manually but it will take to much time if it's like 50 teams and to figure out wish team they haven't challenge. And how to make that all teams play one match everyday.

<?php
mysql_query("INSERT INTO timetable (id, team1, team2, date, time) VALUES ('','Blue','Red','2011-02-06','18.00')")
?>



I know my English is not the best but I hope you get it.
Thanks any way,

Is This A Good Question/Topic? 0
  • +

Replies To: PHP/mysql football manager tournament

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: PHP/mysql football manager tournament

Posted 06 February 2012 - 07:31 AM

I don't think you want them inserted randomly. I think you want to go through each team and pair it up with every other team, then choose the dates of their games semi randomly from a list of game date/times. I say semi randomly because you probably don't want the same teams playing twice in a row.
Was This Post Helpful? 0
  • +
  • -

#3 smixen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-March 11

Re: PHP/mysql football manager tournament

Posted 06 February 2012 - 04:47 PM

View PostCTphpnwb, on 06 February 2012 - 07:31 AM, said:

I don't think you want them inserted randomly. I think you want to go through each team and pair it up with every other team, then choose the dates of their games semi randomly from a list of game date/times. I say semi randomly because you probably don't want the same teams playing twice in a row.

You read my mind. It a bit hard for ne to explai things because im just a 14 year old kid from sweden.
But if I know all the things he actually typed then my problem i sloved.
I think the only thing I need to learn is how to insert so only each team challange one team once or twice.
But if twice all the other teams needs to do that to hope this can be sloved, thanks any way.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1942
  • View blog
  • Posts: 7,296
  • Joined: 08-August 08

Re: PHP/mysql football manager tournament

Posted 06 February 2012 - 05:23 PM

The first step is to pair up the teams. You can do that by iterating through the array of teams pairing each team with each of the teams that follow it in the array.
Example:
<?php
$teams = array("Team A", "Team B", "Team C", "Team D", "Team E");
$games = array();

class game {
	public $home_team;
	public $visitors;
	public $date_time;
	public $home_score;
	public $visitor_score;
	public $winner;
	
	function __construct($h,$v) {
		$this->home_team = $h;
		$this->vistors = $v;
	}
	
	function show_pairing() {
		echo $this->home_team." vs ".$this->vistors."<br>";
	}
}

for($i = 0; $i < count($teams)-1; $i++) {
	for($j = $i + 1; $j < count($teams); $j++) {
		$games[] = new game($teams[$i],$teams[$j]);
	}
}

for($i = 0; $i < count($games); $i++) {
	$games[$i]->show_pairing();
}
?>

I've made the $games array an array of objects. That may be a bit confusing at first, but it will make things easier later when the project has more code.
Was This Post Helpful? 1
  • +
  • -

#5 smixen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 19-March 11

Re: PHP/mysql football manager tournament

Posted 11 February 2012 - 03:05 AM

View PostCTphpnwb, on 06 February 2012 - 05:23 PM, said:

The first step is to pair up the teams. You can do that by iterating through the array of teams pairing each team with each of the teams that follow it in the array.
Example:
<?php
$teams = array("Team A", "Team B", "Team C", "Team D", "Team E");
$games = array();

class game {
	public $home_team;
	public $visitors;
	public $date_time;
	public $home_score;
	public $visitor_score;
	public $winner;
	
	function __construct($h,$v) {
		$this->home_team = $h;
		$this->vistors = $v;
	}
	
	function show_pairing() {
		echo $this->home_team." vs ".$this->vistors."<br>";
	}
}

for($i = 0; $i < count($teams)-1; $i++) {
	for($j = $i + 1; $j < count($teams); $j++) {
		$games[] = new game($teams[$i],$teams[$j]);
	}
}

for($i = 0; $i < count($games); $i++) {
	$games[$i]->show_pairing();
}
?>

I've made the $games array an array of objects. That may be a bit confusing at first, but it will make things easier later when the project has more code.


Thank u so much! Now let's learn some more about arrays. :bananaman:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1