Jump to content

Recommended Posts

I have to make a code that takes X amount of teams and and pairs them each up a given amount of time. No match ups can repeat themselves and they have to be matched up Y amount of times. I've been working on this code since 8:00 last not and I still haven't been able to get it completely working. Now, its stuck in an endless loop in which the match up is being endlessly inserted into the database.

 

Why is this resulting in an endless loop?

$started = "false";
$id=$_GET['id'];
$gid = $id;
//Identify game
$sql = "select * from `games` where `id`='$id'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
	$game = $row['title'];
}
//$games = $_POST['games'];
//START Grab Game
//Find Team
$a=0;
$sql5 = "select * from `leagues` where `game`='$game'";
$result5 = mysql_query($sql5) or die(mysql_error());
while ($row5 = mysql_fetch_array($result5)){
	$division = $row5['name'];
	$sql6 = "select * from `conferences` where `game`='$game' and `division`='$division'";
$result6 = mysql_query($sql6) or die(mysql_error());
while ($row6 = mysql_fetch_array($result6)){
	$conference = $row6['title'];

$sql = "select * from `teams` where `game`='$game' and `league`='$division' and `conference`='$conference'";
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)){
	$a += 1;
	$teamId = $row['id'];
	$team = $row['name'];
	$teamsIndex["$a"] = $team;
	$division = $row['league'];
	$conference = $row['conference'];
}
//Each Team is stored in an index
$b = 0;
while ($b<$a){
$b+=1;
$teams["$b"]=$b;
}
$cWeek = 1;
$weeks = $_POST['weeks'];
//So we have 15 teams. Cycle each team and make matchups

while($cWeek<=$weeks){
echo "<br><br><b>Week $cWeek</b><hr>";
$b = 1;
$rows = -1;
echo "<br>Rows redeclared";
while ($b<=$a){
	$bold = $b;
	echo "<br>DEBUG: $b/$a";
	$end = "false";
	$stopper = 0;
	while ($end!="true"){

		echo "<br><i>End: $end</i>";
		$team1 = $b;
		$redo="false";
		$reverse = "false";
		///lets match up a random
		$against = $team1;
		while ($against==$team1){
			$against = rand(1,$a);
		}
		echo "<br>Row: $rows";
		//We have rows set, so lets check each row.
		$rowcheck = 0;
		$cancont = 0;
		echo "<br><i>$team1 vs $against</i>";
		$stop = "false";

		if ($rows==-1){
			$stop = "true";
			echo "<br><u>FIRST ROW</u>";
		}

		while ($rowcheck<=$rows){
			$reverse = "false";
			$redo = "false";
			$cancont = 1;
			echo "<br><i>RC: $rowcheck/$rows</i>";
			$t1 = $games[$rowcheck]["Team1"];
			$t2 = $games[$rowcheck]["Team2"];
			echo "<br><i>t1 = $t1; t2 = $t2</i>";
			if($t2==$against){
				$cancont = 0;
				$redo="true";
				echo "<br> Team 2 already has a match";
			}

			if($team1==$t2){
				if($against==$t1){
				//The match is already set, just in the reverse order
					$stop="true";
					echo "<br> Team 1 already has a match against team 2";
				}
			}
			if ($cancont==0){
				$rowcheck=$rows+10;
				$redo="true";
			}else{
				$rowcheck +=1;
			}
			//Check if already matched up in a previous week. 
			$teamA = $teamsIndex["$b"];
			$teamB = $teamsIndex["$against"];
			//WAIT how many weeks are listed? 
			$prev = "false";
			$sql9 = "select * from `matches` where `game`='$game' and `division`='$division' and `team1`='$teamA' and `team2`='$teamB'";
			$result9  = mysql_query($sql9) or die(mysql_error());
			while ($row9 = mysql_fetch_array($result9)){
				$stop = "false";
				$rowcheck = $rows+1;
				$prev = "true";
				echo "<br>Match Already Made";
			}
			$sql9 = "select * from `matches` where `game`='$game' and `division`='$division' and `team1`='$teamB' and `team2`='$teamA'";
			$result9  = mysql_query($sql9) or die(mysql_error());
			while ($row9 = mysql_fetch_array($result9)){
				$stop = "false";
				$rowcheck = $rows+1;
				$prev = "true";
				echo "<br>Match Already Made";
			}
			if($prev=="false"){
				if ($team1!=$t1){
					if ($against!=$t2){
						if ($team1!=$t2){
							if ($against!=$t1){
								$stop="true";
								echo "<br>AVALIBLIE";
							}else{
								$stop="false";
								echo "<br>Team Two = t1";
								$rowcheck = $rows+1;
							}
						}else{
							echo "<br>Team1 = t2";
							//HOLD ON. If this happens, it should not record, but still print it!
							$stop = "true";
							$reverse = "true";
							$against = $t1;
							$rowcheck = $rows+1;

						}
					}else{
					$stop="false";
						echo "<br>Team2 = t2";
						$rowcheck = $rows+1;
					}
				}else{
				$stop="false";
					echo"<br>Team1 =t1";
					$rowcheck = $rows+1;
				}
			}
		}
		//Can we record the match?
		if ($redo=="true"){
			$end = "false";
		}else{
			//No issues where found
			$end = "true";
		}
			//Record match
			//if it wasn't stopped!
			if($stop=="true"){

				if ($rows==-1){
						$end = "true";
						$games = array( 
										array( 
											"Team1" => "$team1",
											"Team2" => "$against"
										)
								);
						echo "<br><i>First</i>";
						$rows +=1;

						echo "<Center><br><b>$team1 vs $against</b> ($rows)<br><br></center>";
											//Convert Indexes


					$team1 = $teamsIndex["$b"];
					$against = $teamsIndex["$against"];
					$sql = "insert into `matches` (`team1`,`team2`,`game`,`division`,`week`) values ('$team1','$against','$game','$division','$cWeek')";
					mysql_query($sql) or die(mysql_error());
						$sql3 = "select * from `bb_forums`";
				$mcids=1;
				$result3 = mysql_query($sql3) or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){	
					$mcids+=1;
				}
				$sql3 = "insert into `bb_forums` (`cid`,`name`,`islocked`) values ('7','Match Chat #$mcids','false')";
				mysql_query($sql3) or die(mysql_error());
				$sql3 = "select * from `bb_forums` where `name`='Match Chat #$mcids'";
				$result3 = mysql_query($sql3) or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){	
					$id = $row3['id'];
				}
				$url = "<a href='forum.php?page=tlist&id=$id'>Match Forum</a>";
				$url = mysql_real_escape_string($url);
				$sql3 = "insert into `matchcom` (`team`,`url`,`team2`,`week`) values ('$against','$url','$team1','$cWeek')";
				mysql_query($sql3) or die(mysql_error());
				$sql3 = "select * from `matchcom` where `url`='$url'";
				$result3 = mysql_query($sql3) or die(mysql_error());
				while ($row3 = mysql_fetch_array($result3)){
					$mcid = $row3['id'];
				}
				$sql3 = "insert into `matchcom_data` (`mcid`,`game`,`division`) values ('$mcid','$game','$division')";
				mysql_query($sql3) or die(mysql_error());
					$end = "true";
				}else{
					if($reverse!="true"){
					$rows +=1;

					echo "<br>$rows";
					$games[$rows]["Team1"]="$team1";
					$games[$rows]["Team2"]="$against";
					//array_push($games,Team1 => "$team1", Team2 => "$against");
					/*$games .= array(
									array(
										Team1 => "$team1",
										Team2 => "$team2"
									)
							);		*/

					echo "<centeR><br><b>$team1 vs $against</b> ($rows) <br><br></center>";
					//Convert Indexes
					//$team1 = $b;
					echo "<br>$b";
					$team1 = $teamsIndex["$b"];
					$against = $teamsIndex["$against"];
					//Insert into Matches
					$sql = "insert into `matches` (`team1`,`team2`,`game`,`division`,`week`) values ('$team1','$against','$game','$division','$cWeek')";
					mysql_query($sql) or die(mysql_error());
					//Insert into Match Com
				$sql3 = "select * from `bb_forums`";
				$mcids=1;
				$result3 = mysql_query($sql3) or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){	
					$mcids+=1;
				}
				$sql3 = "insert into `bb_forums` (`cid`,`name`,`islocked`) values ('7','Match Chat #$mcids','false')";
				mysql_query($sql3) or die(mysql_error());
				$sql3 = "select * from `bb_forums` where `name`='Match Chat #$mcids'";
				$result3 = mysql_query($sql3) or die(mysql_error());
				while($row3 = mysql_fetch_array($result3)){	
					$id = $row3['id'];
				}
				$url = "<a href='forum.php?page=tlist&id=$id'>Match Forum</a>";
				$url = mysql_real_escape_string($url);
				$sql3 = "insert into `matchcom` (`team`,`url`,`team2`,`week`) values ('$against','$url','$team1','$cWeek')";
				mysql_query($sql3) or die(mysql_error());
				$sql3 = "select * from `matchcom` where `url`='$url'";
				$result3 = mysql_query($sql3) or die(mysql_error());
				while ($row3 = mysql_fetch_array($result3)){
					$mcid = $row3['id'];
				}
				$sql3 = "insert into `matchcom_data` (`mcid`,`game`,`division`) values ('$mcid','$game','$division')";
				mysql_query($sql3) or die(mysql_error());

					}else{
						echo "<centeR><br><b>$team1 vs $against</b> ($rows) <br><br></center>";
					}
					$end = "true";
				}
				$end="true";
			}else{
				echo "<br><b>Failed</b><br><br>";
				$end="false";
			}
		}
		//echo "<br>";
	$b= $bold+1;;
}
	//echo "<br>";
	//Week is done, instert
	$cWeek +=1;
}
			}
		}
$sql="update `games` set `status`='live' where `id`='$gid'";
mysql_query($sql) or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/228944-holy-crap-this-is-annoying-me/
Share on other sites

look this code

<?php
$teams = array('team1',
     'team2',
     'team3',
     'team4',
     'team5',
     'team6',
     'team7',
     'team8',
     'team9',
     'team10',
     'team11',
     'team12',
     'team13',
     'team14',
     'team15');
$c = count($teams);
$h = ceil($c/2);
$d = $c % 2;
$nl = "<br />\n";
for($i=1; $i<count($teams); $i++){
    $teams = array_values($teams);
    echo "Week $i<hr />\n";
    for($j=0; $j<$h-1; $j++) echo $teams[$j],' - ',$teams[$j + $h],$nl;
    if($d == 0) echo $teams[$j],' - ',$teams[$j + $h],$nl;
    else echo  $teams[$j], " free", $nl;
    $t1 = array_shift($teams);
    $t2 = array_shift($teams);
    array_push($teams, $t2);
    array_unshift($teams, $t1);
    echo "<br /><br />\n";
}
?> 

for me it output

Week 1
team1 - team9
team2 - team10
team3 - team11
team4 - team12
team5 - team13
team6 - team14
team7 - team15
team8 free


Week 2
team1 - team10
team3 - team11
team4 - team12
team5 - team13
team6 - team14
team7 - team15
team8 - team2
team9 free


Week 3
team1 - team11
team4 - team12
team5 - team13
team6 - team14
team7 - team15
team8 - team2
team9 - team3
team10 free


Week 4
team1 - team12
team5 - team13
team6 - team14
team7 - team15
team8 - team2
team9 - team3
team10 - team4
team11 free


Week 5
team1 - team13
team6 - team14
team7 - team15
team8 - team2
team9 - team3
team10 - team4
team11 - team5
team12 free


Week 6
team1 - team14
team7 - team15
team8 - team2
team9 - team3
team10 - team4
team11 - team5
team12 - team6
team13 free


Week 7
team1 - team15
team8 - team2
team9 - team3
team10 - team4
team11 - team5
team12 - team6
team13 - team7
team14 free


Week 8
team1 - team2
team9 - team3
team10 - team4
team11 - team5
team12 - team6
team13 - team7
team14 - team8
team15 free


Week 9
team1 - team3
team10 - team4
team11 - team5
team12 - team6
team13 - team7
team14 - team8
team15 - team9
team2 free


Week 10
team1 - team4
team11 - team5
team12 - team6
team13 - team7
team14 - team8
team15 - team9
team2 - team10
team3 free


Week 11
team1 - team5
team12 - team6
team13 - team7
team14 - team8
team15 - team9
team2 - team10
team3 - team11
team4 free


Week 12
team1 - team6
team13 - team7
team14 - team8
team15 - team9
team2 - team10
team3 - team11
team4 - team12
team5 free


Week 13
team1 - team7
team14 - team8
team15 - team9
team2 - team10
team3 - team11
team4 - team12
team5 - team13
team6 free


Week 14
team1 - team8
team15 - team9
team2 - team10
team3 - team11
team4 - team12
team5 - team13
team6 - team14
team7 free

sorry 15 teams break my code

here is fix

<?php
$teams = array('team1',
     'team2',
     'team3',
     'team4',
     'team5',
     'team6',
     'team7',
     'team8',
     'team9',
     'team10',
     'team11',
     'team12',
     'team13',
     'team14',
     'team15');
$c = count($teams);
$h = ceil($c/2);
$d = $c % 2;
if($d) array_push ($teams, 'free');
$c = count($teams);
$nl = "<br />\n";
for($i=1; $i<count($teams); $i++){
    $teams = array_values($teams);
    echo "Week $i<hr />\n";
    for($j=0; $j<$h; $j++) echo $teams[$j],' - ',$teams[$c -$j -1],$nl;
    $t1 = array_shift($teams);
    $t2 = array_shift($teams);
    array_push($teams, $t2);
    array_unshift($teams, $t1);
    echo "<br /><br />\n";
}
?> 

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.