PHP - Holy Crap This Is Annoying Me.
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"; Similar TutorialsThis topic has been moved to PHP Applications. http://www.phpfreaks.com/forums/index.php?topic=311671.0 function Clean_String($string) { return strtolower(trim(filter_var($string, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW))); } Ive tracked my issue back to filter_var here. For some reason it is adding stuff to the beginning and end of the string im looking for. It did this on 1 string that I noticed and doesnt do it on any others, which was strange because that string was typical like all others. Just a few sentences and numbers and line breaks. I cant find a reason why this is happening This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=349365.0 This topic has been moved to Other. http://www.phpfreaks.com/forums/index.php?topic=347753.0 I'm working on a login script for my site. My login script, for some reason, does not recognise valid and registered user details and kicks the user back to the login screen. I've been looking at my script for some time now and can't find the issue that is causing this - so fresh eyes would be a massive help. The registration script works fine and records user details correctly so that isn't the problem. EDIT: Fixed my PHP quote. Code: [Select] [m]<?php include 'connect.php'; $myusername = $_POST['myusername']; $mypassword = $_POST['mypassword']; $tbl_name = 'usr_test'; $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $myusername = trim($myusername); $mypassword = trim($mypassword); $hash = md5($mypassword); $sql = "SELECT * FROM $tbl_name WHERE usr = '$myusername' AND pass = '$hash'"; $result = mysql_query($sql); if(isset($myusername) && ($hash)) { if($result) { } else{ header("Location:homelogin.php"); } $num_rows = mysql_num_rows($result); if($num_rows > 0){ session_start(); $_SESSION['login'] = "1"; header("Location:homelogin.php"); } else{ header("Location:loginerr.php"); } } else header("Location:loginerr.php"); ?>[/m] Ok so i had my site working well, i had problems with cookies before. But i got them resolved. Then i wanted to add a log out option to drop the cookie. I have two files reacting differently to the setcookie("Cookiename","",time()-3600); command. there is two directorys. root directory root/includes/functions.php functions.php is the file giving me the trouble i think. Ok so in root directoy i have a file called stuff.php with a link to drop the id, it then calls setcookie and deletes it. Then redirects to index.php. ok. So index.php includes functions.php and calls set_id() set_id() is in functions.php - it tests if the cookie exists, if not it creates one. Now when i log out, it sees that a cookie doesnt exist and creates one and stories it in the database. All seems to be working fine. However, after that if i were to look at a thread. it would call a function in functions.php in that function, it does _is_admin($_COOKIE['name']); if it returns true then an additional option pops up. so i have just reset my cookie and i can see in the database that set_id() in functions.php has added created a new cookie. However when the _is_admin() and all other functions in functions.php are called, it still detects the old cookie Which i had before i logged out. Now why would one function in the file act differently from the rest. Sorry if i explained this bad, but its so hard to explain and its annoying. The below functions is in functions.php and recognises there is no cookie and creates a new one. function set_id(){ if(isset($_COOKIE['supercookie'])){ $v =$_COOKIE['supercookie']; //update cookie setcookie("supercookie",$v,time() + 60 * 60 * 24 * 365); } else{ $charlist = "abcdefghijklmnopqrstuvwxyz0123456789"; $c = ""; for($i = 0; $i < 25; $i++ ){ $r = rand() % strlen($charlist); $c .= $charlist[$r]; } setcookie("supercookie",$c,time() + 60 * 60 * 24 * 365); setcookie("FirstArrived","dfffff",time()+60); addaction(9,"Set New id."); mysql_query("INSERT INTO pword (level,lv,scookie) VALUES ('Norm','0','$c')")or die(mysql_error()); } } however any other function in functions.php that calls $_cookie['supercookie'] always seems to be returned the admin cookie which i had before i "logged out" I am experimenting around with PDO, I have a DB handle class and instancing it in a function. The problem is when I want to bind elements in a query, for example I bind a parameter but I can only do it using a varible outside of the object. Code: [Select] <?php $database = new Database("localhost", "fry", "root", ""); $database->set_table("usertest"); $database->set_query("SELECT * FROM usertest WHERE user_name = :user_name"); $value = "matt"; $database->prepare_query(); $database->bind("parameter", ":user_name", $value ,PDO::PARAM_STR, 5); $database->execute(); while($result = $database->fetch()) { echo $result['user_name'].'<br />'; } ?> Is this bad OOP practice? I wanted to put $value = "matt"; into the Database class somehow here is the Database class Code: [Select] <?php class Database{ public $hostname; public $database; public $username; public $password; public $connection; public $prepare; public $query; public $table; public $fetch; public $bind_var; public $bind_val; function __construct($hostname, $database, $username, $password) { $this->hostname = $hostname; $this->database = $database; $this->username = $username; $this->password = $password; $this->Database_connection(); } public function Database_connection() { try { $this->connection = new PDO('mysql:host='.$this->hostname.';dbname='.$this->database, $this->username, $this->password); } catch (PDOException $e) { echo 'Connection failed: ' . $e->getMessage(); } } public function set_query($query) { $this->query = $query; } public function get_query() { return $this->query; } public function set_table($table) { $this->table = $table; } public function get_table() { return $this->table; } public function set_bind_var($var) { $bind_var = $var; } public function set_bind_val($val) { $bind_val = $val; } public function get_bind_var() { return $this->bind_var; } public function get_bind_val() { return $this->bind_val; } public function bind($bindtype, $val, $var, $pdo, $num) { if ($bindtype == "parameter") { $result = $this->prepare->bindParam($val,$var,$pdo); } return $result; } public function prepare_query() { $this->prepare = $this->connection->prepare($this->get_query()); } public function execute() { $this->prepare->execute(); } public function fetch() { return $this->prepare->fetch(); } } ?> Yet agian sorry for the sloppy code, this is mainly an experiment, would be very appriciated if someone could help me out with this problem. |