Jump to content

Recommended Posts

I have a loop that I need to pull two rows. I'm really confused

Basically i have a table with a ton of players and i have a loop to match them up, i need to be able to have a loop match two rows then insert and delete. Heres what I got now that just has a normal loop.

	if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            mysql_query("UPDATE leagues SET status='1' WHERE id='$id'");
			$update = mysql_query("SELECT * FROM leagueplayers WHERE league='$id' ORDER BY ");
   			 while ($row = mysql_fetch_array($update)) {
	        //Insert
		//Delete
		}
        }
    }

Thank you

Link to comment
https://forums.phpfreaks.com/topic/207783-loop-pulling-two-rows/
Share on other sites

I'm confused too because I'm not understanding what you are trying to accomplish. Are you just wanting to match up all the players in pairs randomly? I would assume the insert is used to save a record for the association of each pair, but I'm not understanding what the delete would be for.

 

It should be possible to create all the pairs records with a single query - i.e. no PHP processing. But, I'm hesitant to invest any time into this since I'm not sure if what i think is what you really want.

Well, I cant insert them as pairs as it's a list of people joining at different times, but i want to pull two players at a time from the tables run an insert using details from both, then delete both once the match is created since its basically a waiting list. I'm just not sure how to deal with two rows at a time in a loop.

i'm not entirely sure what you're after either,

however you can update/delete more than one row at a time?

i.e.

mysql_query("UPDATE leagues SET status='1' WHERE id='$id1' OR id='$id2'");

 

but i suspect you want to select two affiliated rows and then do something with them.

I still don't understand what you really want to do and I suspect there is a VERY easy solution with the proper query. But, I'll give you a solution based on what you already have.

 

while ($player1 = mysql_fetch_array($update) && $player2 = mysql_fetch_array($update))
{
    //Each iteration of the loop will pull two records from the
    //results and assign to $player1 and $player2.
    //Note: if uneven number of records are returned
    //the last record won't be processed

    //Insert
    //Delete
}

I still don't understand what you really want to do and I suspect there is a VERY easy solution with the proper query. But, I'll give you a solution based on what you already have.

 

while ($player1 = mysql_fetch_array($update) && $player2 = mysql_fetch_array($update))
{
    //Each iteration of the loop will pull two records from the
    //results and assign to $player1 and $player2.
    //Note: if uneven number of records are returned
    //the last record won't be processed

    //Insert
    //Delete
}

I believe this will work great thank you so much.

hmm so I have this:

if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            mysql_query("UPDATE leagues SET status='0' WHERE id='$id'");
			$update = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
			while ($player1 = mysql_fetch_array($update) && $player2 = mysql_fetch_array($update))
			{
			echo $player1[id];
			echo"-";
			echo $player2[id];
		}
        }

And i got -5, the first ones not pulling anything...hmm

if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            $update1 = mysql_query("UPDATE leagues SET status='0' WHERE id='$id'");
			$update2 = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
			while ($player1 = mysql_fetch_array($update1) && $player2 = mysql_fetch_array($update2))
			{
			echo $player1[id];
			echo"-";
			echo $player2[id];
		}
        }

try this

So player1 doesnt pull any info here:

	if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            mysql_query("UPDATE leagues SET status='0' WHERE id='$id'");
			$update = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
			while ($player1 = mysql_fetch_array($update) && $player2 = mysql_fetch_array($update))
			{
			echo $player1[id];
			echo"-";
			echo $player2[id];
		}
        }

you've got a update query not assigned to any variable.

 

   if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            mysql_query("UPDATE leagues SET status='0' WHERE id='$id'"); //assign this to a variable?
//where is $id coming from?
          $update = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");

            while ($player1 = mysql_fetch_array($update) && $player2 = mysql_fetch_array($update)) //these are running the same query, so both variables in the loop will return the same result. 
            {
            echo $player1[id];
            echo"-";
            echo $player2[id];
         }
        }

 

you need something like

   if($timenow1 == $timenow5 && $timenow2 >= $timenow6){
            $update = mysql_query("UPDATE leagues SET status='0' WHERE id='$id'");
          $select1 = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
$select2 = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");


            while ($player1 = mysql_fetch_array($select1) && $player2 = mysql_fetch_array($select2)) //these can't run the same query.
            {
            echo $player1[id];
            echo"-";
            echo $player2[id];
         }
        }

 

i'm still a bit baffled as to what you're trying to achieve. you want to run a loop and update player IDs within the loop? and somewhere in there you want to have it look at 2 players simultaneously?

Heres what I have so far

    //Start any leagues
$leagues=mysql_query("SELECT id,ladder,datestart,hourstart FROM leagues");
while(list($id,$ladderid,$datestart,$hourstart)=mysql_fetch_row($leagues)){

    $timenow = date('N', time());
    $timenow2 = date('H',time());	
if($timenow == $datestart && $timenow2 == $hourstart){
			$updatel = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
		$update2 = mysql_query("SELECT * FROM leagueplayers WHERE league='$id'");
while ($player1 = mysql_fetch_array($updatel) && $player2 = mysql_fetch_array($update2))
			{
			$totalcountleag=mysql_query("SELECT COUNT(*) FROM leagueplayers WHERE league='$id'");
			$totalcountleag=mysql_fetch_array($totalcountleag);
			$totalcountleag="$totalcountleag[0]";

		if($totalcountleag >= 2){

			$playerd=mysql_query("SELECT * FROM teams WHERE id='$player1[team]'");
			$playerd=mysql_fetch_array($playerd);

			$player2d=mysql_query("SELECT * FROM teams WHERE id='$player2[team]'");
			$player2d=mysql_fetch_array($player2d);


			mysql_query("INSERT INTO challenges (ladderid, challgrid, challgdid, challgrname, challgdname, matchfinder) VALUES (
			'$ladderid','{$player1['team']}','{$player2['team']}','{$playerd['name']}','{$player2d['name']}','1')");

		//	$challenge_id=mysql_query("SELECT challid FROM challenges WHERE challgrname='$playerd[name]' AND challgdname='$player2d[name]'");
		//	$challenge_id=mysql_fetch_array($challenge_id);

		//	mysql_query("INSERT INTO chall_chat (chall_id,challenger,challenged,type)
	 	//	VALUES('$challenge_id[challid]','$player1[team]','$player2[team]','1')");

           // mysql_query("DELETE FROM leagueplayers WHERE id='$player1[id]'");
            //mysql_query("DELETE FROM leagueplayers WHERE id='$player2[id]'");
			}
		}
        }
    }

But the problem is it creates two rows in challenges when it should only create one. The zero on the first one should be the 405 and the first blank should be Gravy Boat and the second row shouldnt exist....

12107 54 0 407   (Blank) bling bling 2010-07-15 04:14:25 1

12108 54 0 405   (Blank) Gravy Boat 2010-07-15 04:14:25 1

you can join the same table to itself, although this is only needed in certain situations and I'm not entirely sure if you need it. You need to describe your mysql table layout/schema and exactly what you're trying to achieve. i.e. user inserts row then row updates xxxx table to have xy field = valueX.

i.e.

$sql = @mysql_query("SELECT t1.*, t2.* FROM table1 t1 JOIN table1 t2 ON t1.columnLink = t2.columnLink");

 

 

IN REPLY TO:

Pull two rows from same table that have ID and Name.

Create a row in different table with both ids and names from first two.

Delete the original two rows

Make sense?

 

<?php
$query1 = "SELECT * FROM example1 LIMIT 0, 2"; 
$result1 = mysql_query($query1) or die(mysql_error());

while($row1 = mysql_fetch_array($result1)){
$query2 = "INSERT INTO example2 (id, name) VALUES('".$row1["id"]."', '".$row1["name"]."' ) "; 
$result2 = mysql_query($query2) or die(mysql_error());
$query3 = "DELETE FROM example1 WHERE id='".$row1["id"]."'"; 
$result3 = mysql_query($query3) or die(mysql_error());
}
?>

 

Is this what you wanted?

Thank you professor badass, were almost there but it should look similar to this, i know it doesnt work but maybe youll get the idea.

<?php
$query1 = "SELECT * FROM example1 LIMIT 0, 2"; 
$result1 = mysql_query($query1) or die(mysql_error());

while($row1 = mysql_fetch_array($result1)){
$query2 = "INSERT INTO example2 (id, name, id2, name2) VALUES('".$row1["id"]."', '".$row1["name"].",".$row2["id"].","$row2["name"]"' ) "; 
$result2 = mysql_query($query2) or die(mysql_error());
$query3 = "DELETE FROM example1 WHERE id='".$row1["id"]."'"; 
$query3 = "DELETE FROM example1 WHERE id='".$row2["id"]."'"; 

$result3 = mysql_query($query3) or die(mysql_error());
}
?>

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.