Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11

Thread: Need Loop Help

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Need Loop Help

    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.

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


  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Give this a try:
    PHP Code:
    //Assign the Map Info 
    $s 0
    for(
    $i=0$i $query3$i++){         
        if( 
    $tplyrs[$i]['pname'] == 'name' ){ 
            
    $js_select='me'
            
    $id $s
            
    $s++; 
        } 

    If I'm postin here, I NEED YOUR HELP!!

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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?
    Last edited by ptmuldoon; 02-19-2008 at 08:05 PM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    PHP Code:
    //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

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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.

    PHP Code:
    //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?
        


  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    I'm trying to figure this out:
    PHP Code:
            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:

    PHP Code:
    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.
    If I'm postin here, I NEED YOUR HELP!!

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    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.
    If I'm postin here, I NEED YOUR HELP!!

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Ultragames View Post
    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.

  • #9
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Try this:
    PHP Code:
    //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($query30'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? 

    If I'm postin here, I NEED YOUR HELP!!

  • #10
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    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?

  • #11
    Regular Coder
    Join Date
    Aug 2002
    Location
    Oregon, United States of America
    Posts
    882
    Thanks
    1
    Thanked 9 Times in 9 Posts
    Check out the manual: 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.

    PHP Code:
    //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)>=){
        
    $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)>=){
            
    $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)>=){
                    
    $pname mysql_result($query30'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';

    If I'm postin here, I NEED YOUR HELP!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •