...

View Full Version : Need Loop Help



ptmuldoon
02-18-2008, 11:13 PM
I'm trying to loop through an array, and when I have a match on a field name, I'm trying to assign a variable an have it increment by 1. But I'm uncertain of how to add the second loop inside the first.



//Assign the Map Info
for($i=0; $i < $query3; $i++){
$s = 0;
while ($tplyrs[$i]['pname'] == 'name') {
$js_select='me';
$id = $s;
$s++;
}
}

Ultragames
02-19-2008, 01:37 AM
Give this a try:

//Assign the Map Info
$s = 0;
for($i=0; $i < $query3; $i++){
if( $tplyrs[$i]['pname'] == 'name' ){
$js_select='me';
$id = $s;
$s++;
}
}

ptmuldoon
02-19-2008, 08:00 PM
Unfortunately, I had tried the above already, but still can't seem to get to it to work.

The First loop processes correct, and if there is a match, it assigns the correct "me".

In the above example, I have 7 matches being found during the loop, and I get 7 "me" returned in the output. So I'm trying to have it return me me1, me2, me3, etc.

Do I need to first determine how many matches I have with an sql query, and then run for loop based on the # of matches?

oesxyl
02-19-2008, 08:25 PM
//Assign the Map Info
for($i=0; $i < $query3; $i++){
$s = 0;
while ($tplyrs[$i]['pname'] == 'name') {
$js_select='me';
$id = $s;
$s++;
}
}

- you assign consecutive numbers, $s, to a variable, $id, but you don't use $id anywhere inside of that for
- you assign a value, 'me', to a variable, $js_select, but there is no initialization for $js_select outside of while loop, and also you don't use it anyehere.

Along with that I don't understand what do you want to achive? what is the input and what is the output.

I think that if you give more detail maybe we could help.

best regards

ptmuldoon
02-28-2008, 12:55 AM
Sorry for the delay in getting back to everyone. Essentially, I'm looping through an array, and comparing a user's name. If I have a match, it should auto-increment the matches ie; 1,2,3,....

And for the non matches, it should also auto increment them as 1,2,3,.....

I've posted more of the competed code thus far, removing what should be all the non-essential pieces to keep the code posted smaller.

of course, if there's a better way to go about it, without having so many while loops inside each other, I'm all for it, as I'm still learning.



//Get Game Information, Game ID, etc.
$sql1 = "SELECT * FROM game_info WHERE gid = {$_SESSION['gid']} ";
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_assoc($query1)){
$ginfo[] = $row;
$gnumber = $row['gid'];
$mapstyle = $row['style'];
}

//Display a list of ALL the countries
$sql2 = "SELECT gd.gid, gd.pid, gd.pterritory, gd.pname, gd.parmies, cty.mtype, cty.id, cty.name FROM countries cty, game_data gd WHERE gd.gid = {$gnumber} AND cty.mtype = '{$mapstyle}' AND gd.pterritory = cty.id";
$query2 = mysql_query($sql2);
$me = 1;
$them =1;
while($state = mysql_fetch_assoc($query2)){

//for each state, get the name, etc
$sql3 = "SELECT * FROM game_players where gid = {$gnumber} AND pid = {$state['pid']}";
$query3 = mysql_query($sql3);
while($plyrs = mysql_fetch_assoc($query3)){
$pname = $plyrs['pname'];

}

if($pname == $_SESSION['player_name']){
$state['jselect'] = 'me';
$state['selectid'] = $me++;
} else {
$state['jselect'] = 'them';
$state['selectid'] = $them++;
}

$states[] = $state;
$count = count($states);
$rowcount = $count - 1; //Since Starts at 0?
}

Ultragames
02-28-2008, 01:04 AM
I'm trying to figure this out:

while($plyrs = mysql_fetch_assoc($query3)){
$pname = $plyrs['pname'];

}

if($pname == $_SESSION['player_name']){
$state['jselect'] = 'me';
$state['selectid'] = $me++;
} else {
$state['jselect'] = 'them';
$state['selectid'] = $them++;
}

The way I read that, only the last pname in query3's result is going to be set to $pname. If that is what you want, then why don't you combine query2 and query3 with a JOIN and a GROUP BY? If that is not what you want, then you should put the if/else inside the while loop.

Also, i'm not sure what your using the $rowcount variable for, but if you want to get a counter, you can do it this way:


for( $rowcount=0; $state = mysql_fetch_assoc($query2); $rowcount++ )

That combines the normal for loop and your while loop, to give you the same result, but with a counter.

Ultragames
02-28-2008, 01:08 AM
I just noticed that your second while loop is not inside your first one. Shouldn't it be? If not, then you're once again left with only the last value that the while loop set to be used in the second while loop.

ptmuldoon
02-28-2008, 01:26 AM
I just noticed that your second while loop is not inside your first one. Shouldn't it be? If not, then you're once again left with only the last value that the while loop set to be used in the second while loop.

Actually, the second loop outside the first doesn't matter as the first loop is only getting 1 row of data. Thats where I'm still learning, as I keep using a while loop to get the info for a row, even if its just one row of data.

But in the second loop, I'm starting with $me=1 and $them=1. And when it loops, it loops through and assigns the variable based on the order or query. I think perhaps I may need to compare and loop through the array after first assigning the 'me' and 'them'. Not sure, but I'll give it shot, and see what I learn.

Ultragames
02-28-2008, 01:31 AM
Try this:

//Get Game Information, Game ID, etc.
$query1 = mysql_query("SELECT * FROM game_info WHERE gid = {$_SESSION['gid']} ") or die(mysql_error());
$query1_row = mysql_fetch_assoc($query1);

$ginfo[] = $query1_row;
$gnumber = $query1_row['gid'];
$mapstyle = $query1_row['style'];

//Display a list of ALL the countries
$query2 = mysql_query("SELECT gd.gid, gd.pid, gd.pterritory, gd.pname, gd.parmies, cty.mtype, cty.id, cty.name FROM countries cty, game_data gd WHERE gd.gid = {$gnumber} AND cty.mtype = '{$mapstyle}' AND gd.pterritory = cty.id") or die(mysql_error());

$me = 1; $them =1;
while($state = mysql_fetch_assoc($query2)){
//for each state, get the name, etc
$query3 = mysql_query("SELECT * FROM game_players where gid = {$gnumber} AND pid = {$state['pid']}") or die(mysql_error());
$pname = mysql_result($query3, 0, 'pname');

if($pname == $_SESSION['player_name']){
$state['jselect'] = 'me';
$state['selectid'] = $me++;
} else {
$state['jselect'] = 'them';
$state['selectid'] = $them++;
}

$states[] = $state;
$count = count($states);
$rowcount = $count - 1; //Since Starts at 0?
}

ptmuldoon
02-28-2008, 01:47 AM
Hey, that was excellent, thank you very much !!

It works great. But to try and help me learn some, what exactly is happening with this query?

$pname = mysql_result($query3, 0, 'pname')

What exactly does the 0 do in the query?

Ultragames
02-28-2008, 01:52 AM
Check out the manual: mysql_result (http://us3.php.net/mysql_result)

The first variable is the mysql_query result.
The second variable is the row in the result, (in your case you only want the first row.)
The third is the feild in the result.

One thing I should have added though, is that you should check that your query worked before trying to get results from it, so I have added checks to make sure that mysql_query does not return false, and that it returned at least one row. Otherwise if you try to use mysql_result, or any of the mysql_fetch_* functions, they will error.


//Get Game Information, Game ID, etc.
$query1 = mysql_query("SELECT * FROM game_info WHERE gid = {$_SESSION['gid']} ") or die(mysql_error());
if( $query1 && mysql_num_rows($query1)>=1 ){
$query1_row = mysql_fetch_assoc($query1);

$ginfo[] = $query1_row;
$gnumber = $query1_row['gid'];
$mapstyle = $query1_row['style'];

//Display a list of ALL the countries
$query2 = mysql_query("SELECT gd.gid, gd.pid, gd.pterritory, gd.pname, gd.parmies, cty.mtype, cty.id, cty.name FROM countries cty, game_data gd WHERE gd.gid = {$gnumber} AND cty.mtype = '{$mapstyle}' AND gd.pterritory = cty.id") or die(mysql_error());

if( $query2 && mysql_num_rows($query2)>=1 ){
$me = 1; $them =1;
while($state = mysql_fetch_assoc($query2)){
//for each state, get the name, etc
$query3 = mysql_query("SELECT * FROM game_players where gid = {$gnumber} AND pid = {$state['pid']}") or die(mysql_error());
if( $query3 && mysql_num_rows($query3)>=1 ){
$pname = mysql_result($query3, 0, 'pname');

if($pname == $_SESSION['player_name']){
$state['jselect'] = 'me';
$state['selectid'] = $me++;
} else {
$state['jselect'] = 'them';
$state['selectid'] = $them++;
}

$states[] = $state;
$count = count($states);
$rowcount = $count - 1; //Since Starts at 0?
} else {
echo 'ERROR ON QUERY 3';
}
}
} else {
echo 'ERROR ON QUERY 2';
}
} else {
echo 'ERROR ON QUERY 1';
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum