从一组团队创建游戏并对其进行排序 [英] Creating games from a group of teams and sorting them

查看:84
本文介绍了从一组团队创建游戏并对其进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个函数,可以为当前锦标赛的数据库中的所有团队创建游戏.团队可以多次见面并进行对战(用户需要提前指定),然后脚本会将团队置于家中或不在.

一场比赛可以有许多小组,其中有小组.团队只能与同一组中的其他团队进行比赛.

I have written a function which creates games for all the teams in a database for the current tournament. Teams can meet and play against each other any number of times (user specifies this earlier) then the script puts the teams as home or away.

A tournament can have many groups with teams in those groups. Teams should only play against other teams in the same group.

<?php
function creategames($tourid)
{
	$teams = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' ORDER BY group_id DESC")or die(mysql_error());
	$i=0;
	while ($data = mysql_fetch_array($teams))
	{
		$groupnmbr[$i] = $data['group_id'];
		$i++;
	}
	$groups = $groupnmbr[0];
	$nmbrteams = $i;
	$teamspergroup = ($nmbrteams/$groups);
	$meettimes = mysql_result(mysql_query("SELECT meetcount FROM tournament WHERE id = '$tourid'"),0)or die(mysql_error());
	$gamespergroup = nCr($teamspergroup, $meettimes);
	$gamesperteam = (($teamspergroup-1)*$meettimes);
	$z=$groups;
	while ($z > 0)
	{
		$groupid = $z;
		$teams2 = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' AND group_id = '$groupid' ORDER BY RAND()")or die(mysql_error());
		while ($data2 = mysql_fetch_array($teams2)) {
			$team1 = $data2['team_id'];
			$games = 0;
			$games1 = mysql_query("SELECT * FROM games WHERE team_home = '$team1' AND tour_id = '$tourid'")or die(mysql_error());
			$games2 = mysql_query("SELECT * FROM games WHERE team_away = '$team1' AND tour_id = '$tourid'")or die(mysql_error());
			$game1 = mysql_num_rows($games1);
			$game2 = mysql_num_rows($games2);
			$games = $game1+$game2;
			if ($games < $gamesperteam) {
				$teams3 = mysql_query("SELECT * FROM sessions WHERE tour_id = '$tourid' AND group_id = '$groupid' AND team_id != '$team1' ORDER BY RAND()")or die(mysql_error());
				while ($insert = mysql_fetch_array($teams3))
				{
					$team2 = $insert['team_id'];
					$gamechecks = mysql_query("SELECT * FROM games WHERE team_home = '$team1' AND team_away = '$team2' AND tour_id = '$tourid'")or die(mysql_error());
					$gamecheck = mysql_num_rows($gamechecks);
					while ($gamecheck < $meettimes) {
						if ($gamecheck % 2) {
							mysql_query("INSERT INTO games (tour_id, team_home, team_away) VALUES ('$tourid', '$team1', '$team2')")or die(mysql_error());
						}
						else
						{
							mysql_query("INSERT INTO games (tour_id, team_away, team_home) VALUES ('$tourid', '$team1', '$team2')")or die(mysql_error());
						}
						$gamecheck++;						
					}
				}
			}
		}
		$z--;
	}
	return 'Games created';
}
?>



效果很好,但我的问题是所有第一场比赛都主要针对同一支球队,我想知道如何创建一个游戏列表,例如,当总共有6支球队时,在其中有team1和team2 1,那么下一场比赛将是其中team1和team2或不参加比赛的游戏,也许是team3和team5.然后下一场比赛将是team4和team6,在这之后,它将重新开始并查找是否有下一局比赛.

关于如何解决这个问题的任何想法?从数据库中选择游戏时使用rand()可以解决问题,但是同一个团队的游戏肯定会一次或一次彼此相邻显示.

已更新:

这是我目前用来选择游戏并显示它们的方式.



This works fine but my problem is that all the first games are mostly for the same team, I was wondering how I could create a list of games where it would for example, when there are 6 teams in total, have team1 and team2 in game 1 then the next game would be a game where team1 and team2 or not playing, maybe team3 and team5. Then the next game would need to be team4 and team6, after this it would start over and find the next games if there are any.

Any ideas on how to solve this? Using rand() when selecting the games from the database sort of solved it, but games with the same team are bound to show next to each other at one time or another.

Updated:

This is what I currently use to select games and display them.

<?php 
$games = mysql_query("SELECT * FROM games WHERE tour_id = '$id' AND is_final = '0'");
$gamecount=1;
while ($game = mysql_fetch_array($games)) {
	$gameid = $game['id'];
	$hometeam = $game['team_home'];
	$awayteam = $game['team_away'];
	$homescore = $game['home_score'];
	$awayscore = $game['away_score'];
	$status = $game['status'];
	$group = mysql_result(mysql_query("SELECT group_id FROM sessions WHERE tour_id = '$id' AND team_id = '$hometeam'"),0);
	$hometeamname = mysql_result(mysql_query("SELECT name FROM teams WHERE id = '$hometeam'"),0);
	$awayteamname = mysql_result(mysql_query("SELECT name FROM teams WHERE id = '$awayteam'"),0);
	if ($status == 0) {
		echo '<br/><br/><table><tr><td width="300px"><form method="post" action="#">';
		echo 'To be played</td><td width="300px">Grupp '.$group.' Game '.$gameid.'</td></tr>';
		echo '<tr><td colspan="2" align="center">'.$hometeamname.' <input type="text" name="y" size="5">         VS         '.$awayteamname.' <input type="text" name="z" size="5"></td></tr>';
		echo '<tr><td></td><td><input type="hidden" name="game" value="'.$gameid.'"><input type="hidden" name="i" value="'.$hometeam.'"><input type="hidden" name="x" value="'.$awayteam.'"><input type="submit" value="Klar"></form></td></tr></table>';
	}
	else if ($status == 1) {
		echo 'Completed  |  '.$hometeamname.'         '.$homescore.' - '.$awayscore.'          '.$awayteamname.'  |  Grupp '.$group.'<br/>';
	}
	$gamecount++;
}
?>



当我想显示游戏时,此方法有效,但是我可以做些什么来更改上面指定的顺序,以使team1到team6都可以在列表中显示下一个游戏之前玩游戏.

谢谢!



This works when I want to display the games, but what can I do to change the order as specified above so that team1 to team6 all get to play a game before the next game is shown in the list.

Thanks!

推荐答案

tourid) {
tourid) {


团队 = mysql_query("
teams = mysql_query("SELECT * FROM sessions WHERE tour_id = '


tourid'ORDER BY group_id DESC" )或die(mysql_error());
tourid' ORDER BY group_id DESC")or die(mysql_error());


这篇关于从一组团队创建游戏并对其进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆