Problem with program to generate pairings

Discussion in 'PHP' started by dukeofism, Oct 28, 2009.

  1. #1
    Hello! I am new to PHP. I have been working on a program to take some id numbers from a table and randomly pair them up and put the parings into another table. I think I have gotten rid of all the syntax errors, but now when i try to run the page, it just takes forever to load and never ends up loading... I am currently using XAMPP to run a "sever" on my computer to test this program.

    Early I had the form at the bottom of the page set up to use $_POST and began with an if($_POST["submit"] to enter into the PHP code above the connection to the database part of the code.

    Any help would be greatly appreciated!

    Sorry for the spaghetti code that it has become. I have spent quite a while trying to write and rewrite it
     
    dukeofism, Oct 28, 2009 IP
  2. organicCyborg

    organicCyborg Peon

    Messages:
    330
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Whoa. That's some messy code...

    Could you edit your post to use the code tag instead of the quote tag? Might be easier to understand with proper indents.
     
    organicCyborg, Oct 29, 2009 IP
  3. dukeofism

    dukeofism Peon

    Messages:
    739
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <html>
    <body>
    hi
    <?php
    
    $con = mysql_connect("localhost","root","home");
    if(!$con){
      die('Could not connect: ' . mysql_error());
    }
    echo "set";
    $tableName="w7";
    //$tableName=$_GET["week"];//ln10
    mysql_select_db("assassins", $con);
    $pResult = mysql_query("SELECT * FROM players WHERE dead= 0 AND confirmed =1");
    //checks for people who have confirmed their email address and who are still alive
    $randLimit= mysql_num_rows($pResult)+1;
    //fix code for odd number
    echo "pre fisrt while".$tableName."hey";
    $tries=0;
    while($row = mysql_fetch_array($pResult) )
    {
    	echo "in first while";
    	$wResult= mysql_query("SELECT * FROM ".$tableName." WHERE id1=".$row['id']." OR id2=".$row['id']);
    	if($tries >0&& mysql_num_rows($wResult)!=0)
    	{
    		
    		echo $row['name']." appears ".mysql_num_rows($wResult)." times.";//21
    	}
    	while(mysql_num_rows($tries<1&&$wResult)<1)//if they do not appear in the week table
    	{
    		$rand=rand(1,$randLimit);
    		$p2Result=mysql_query("SELECT * FROM players WHERE id=".$rand);//find the player who matches random
    		//need to now check to see if random player is in week. If not pair the two together. Else move on in loop
    		$targetResult=mysql_query("Select * FROM ".$tableName." WHERE id1=".$rand." OR id2=".$rand);
    		if($tries>0&&mysql_num_rows($targetResult)<1) //if rand isn't in weekly table
    		{
    			echo "true<br>";
    			mysql_query("INSERT INTO ".$tableName." VALUES (".$row['id'].", ".$rand.")");
    		}
    		//reset $wResult for check
    		$wResult= mysql_query("SELECT * FROM ".$tableName." WHERE id1=".$row['id']." OR id2=".$row['id']);
    	}//35
    	$tries++;
    	
    }
    $displayQuery=mysql_query("SELECT * FROM ".$tableName);
    	$i=1;
    	//while($row1=mysql_fetch_array($displayQuery))
    	//{
    		//echo i." player 1: ".$row1['id1']." player 2: ".$row1['id2']."<br>";
    	//}
    	
    ?>
    <form action="insertweek.php" type="get">//insert week is the name of this file
    <input type="text" name="week"/>
    <input type="submit" value="true" name="submit"/>
    </form>
    </body>
    </html>
    
    
    Code (markup):
     
    dukeofism, Oct 29, 2009 IP