Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 6 of 6

Thread: [php]loop-de-loop

  1. #1

    [php]loop-de-loop

    Hello,

    The following code snippet, traveses (2) arrays, checking for a match. I understand the first loop through ($usernames), but where is the interation called on ($passwords)?
    Code:
     
    // Now we loop through each entry in our arrays looking for a valid match
        for ($count = 0; $count < count($usernames) && $matchFound == false; $count++) {
    
            // If username and password matches entry...
            if ($username == $usernames[$count] && $password == $passwords[$count]) {
    
                // Get the user's message and indicate we've found a match
                $message = $messages[$count];
                $matchFound = true;
            }
        }

  2. #2
    i think that the $password var is already set, and it checks the number of the password to the number of the username? i think that code should work

    PHP Code:
     // Now we loop through each entry in our arrays looking for a valid match
        
    for ($count 0$count count($usernames) && $matchFound == false$count++) {

            
    // If username and password matches entry...
            
    if ($username == $usernames[$count] && $password == $passwords[$count]) {

                
    // Get the user's message and indicate we've found a match
                
    $message $messages[$count];
                
    $matchFound true;
            }
    else {
    echo 
    "No match at interval {$count}";
    }
        } 
    that will tell u where there will be an error

  3. #3
    Hi,

    Thanks the current code does work, its from Foundation PHP for FLASH. My question was why this loop goes through (2) arrays, and a second one is not needed for $password.

  4. #4
    Quote Originally Posted by cki
    Hi,

    Thanks the current code does work, its from Foundation PHP for FLASH. My question was why this loop goes through (2) arrays, and a second one is not needed for $password.
    because the snippet checks through 2 arrays to see if it can find a message.. thats what it says

  5. #5
    Hi,

    I've been attempting the same thing with AS, and have hit a snag.
    AS
    Code:
    matchfound = false;
    var message = new Array("Fun", "Love", "Laugh");
    var user = new Array("Alice", "Shelia", "Cathy");
    var pass = new Array("Disco", "Tango", "Samba");
    for (var i = 0; i<user.length && matchfound == false; i++) {
    	//IF USERNAME AND PASSWORD MATCHES ENTRY...
    	if (users == user[i] && password == pass[i]) {
    		trace(user[i]);
    		trace(pass[i]);
    		
    		//GET THE USER'S MESSSAGE AND INDICATE WE'VE FOUND A MATCH
    		var messages = message[i];
    		trace(messages[i]);
    		matchfound = true;
    	}
    }

  6. #6


    Code:
    matchfound = false;
    var message = new Array("Fun", "Love", "Laugh");
    var user = new Array("Alice", "Shelia", "Cathy");
    var pass = new Array("Disco", "Tango", "Samba");
    for (i = 0; i<user.length && matchfound == false; i++) {
    //IF USERNAME AND PASSWORD MATCHES ENTRY...
    if (users == user[i] && password == pass[i]) {
    trace(user[i]);
    trace(pass[i]);
    
    //GET THE USER'S MESSSAGE AND INDICATE WE'VE FOUND A MATCH
    var messages = message[i];
    trace(messages[i]);
    matchfound = true;
    }
    }
    u dont need var i =0 just i=0 will do

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012