NYCPHP Meetup

NYPHP.org

[nycphp-talk] Help with while loop

Rob Marscher rmarscher at beaffinitive.com
Mon Nov 27 15:25:00 EST 2006


The way it looks now... your doing a while loop that will keep looping 
until unique is true.  If unique never gets set to true, then it's going 
to loop forever.  I don't see a need for that while loop.  You can just do

$unique=checkDuplicate($newTeam);

also... your function could be simplified a little bit:

function checkDuplicate($value){
    global $teams;
    //do query here, return an array
    for($i=0; $i<count($teams); $i++){
        if($value==$teams[$i]){
            return true;
            }
        }
    return false;
    }

Be sure to escape the contents of $newTeam before doing anything like 
inserting it into a database or echoing it on the page.

Good luck!
-Rob



Randal Rust wrote:
> I have always had trouble with trying to write these from scratch. The
> code below is essentially what I have been working on, but it never
> stops looping. What am I missing here?
>
> <?php
>
> $teams=array(
> 'Reds',
> 'Browns',
> 'Bengals',
> 'Cavaliers',
> 'Blue Jackets',
> 'Indians'
> );
>
> $unique=false;
> $newTeam=$_POST['newTeam'];
>
> function checkDuplicate($value){
>     global $teams;
>     $count=0;
>     //do query here, return an array
>     for($i=0; $i<count($teams); $i++){
>         if($value==$teams[$i]){
>             $count++;
>             }
>         }
>     if($count == 0){
>         //no matches
>         return true;
>         }
>     else {
>         //there are matches
>         return false;
>         }
>     }
>     
> while(!$unique){
>     $check=checkDuplicate($newTeam);
>     if($check){ $unique=true; } else { $unique=false; }
>     }
>
> ?>
>




More information about the talk mailing list