Link to home
Start Free TrialLog in
Avatar of kudak
kudak

asked on

loop

i have connected my database to the web and i use perl to connect it. the problem is the web interface is consists
of 5 fields which the users can choose ay fields to fill in.i do not know which fields the users want to fill in. then i have to parse that data and search in my database the matching data. the loop i do is like this but it is not working:
#$i = 0;
$flag = 0;
$flag1 = 0;
$flag2 = 0;
$flag3 = 0;
$flag4 = 0;
$sql_text = 'SELECT * FROM employee WHERE';
for (@FORM) {  
                if ($flag == 1)
                     {
                  $sql_text = $sql_text . " ";
                      $sql_text = $sql_text . 'last';
                      $sql_text = $sql_text . '=';
                      $sql_text = $sql_text . "'$FORM{'last'}'";
                     
                 
                        if ($flag1 == 1)
                             {         $sql_text = $sql_text . " ";

                                 $sql_text = $sql_text . 'AND';

                                  $sql_text = $sql_text . " ";
                                  $sql_text = $sql_text . 'first';
                                  $sql_text = $sql_text . '=';
                                   $sql_text = $sql_text . "'$FORM{'first'}'";

                           
                              if ($flag2 == 1)
                                        {    $sql_text = $sql_text . " ";


                                            $sql_text = $sql_text . 'AND';

                                              $sql_text = $sql_text . " ";
                                               $sql_text = $sql_text . 'job_title';
                                                 $sql_text = $sql_text . '=';
                                                $sql_text = $sql_text . "'$FORM{'job_title'}'";

                             
                                                       if  ($flag3 == 1)

  {    $sql_text = $sql_text . " ";

                                                         $sql_text = $sql_text . 'AND';

                                                          $sql_text = $sql_text . " ";
                                                          $sql_text = $sql_text . 'department';
                                                          $sql_text = $sql_text . '=';
                                                          $sql_text = $sql_text . "'$FORM{'department'}'";


                                                             if ($flag4 == 1)
                       
                                                               {    $sql_text = $sql_text . " ";
                                                                $sql_text = $sql_text . 'AND';

                                                              $sql_text = $sql_text . " ";
                                                                  $sql_text = $sql_text . 'email';
                                                                  $sql_text = $sql_text . '=';
                                                                  $sql_text = $sql_text . "'$FORM{'email'}'";
                                                      
                                                                
                                                      }

                                                       
                                                       $flag4 == 1;
                                                                                                                                                                 
                                                }            
                                              
                                     
                                           $flag3 == 1 ;
                                          }
                                                                              
                                    
                               $flag2 == 1;
                                    }
    $flag1 == 1;
                         
                     }
               
                $flag == 1 ;
                          
       }

flag means the data is either entered by the user or not. if it is entered, then i concatenate it with the next field and so on. the problem is, this program is not working.
thank you.
actually i do not know how to control the loop. what i did is like i set the flag to 1 is the user fill in the field. do i need to do else??
Avatar of ozo
ozo
Flag of United States of America image

You don't say what "not working" means, but you may be incorrectly assuming that @FORM has some relation to %FORM
The code logic is difficult to follow without indentation but it looks like you may have been intending to do something like:

  $sql_text = 'SELECT * FROM employee WHERE ';
  $sql_text .= join ' AND ',map "$_='$FORM{$_}'",keys(%FORM);

Avatar of kudak
kudak

ASKER

Edited text of question
Why are you trying to do a loop at all?
Instead of a loop over
if( $flag3 == 1 ){
 if( $flag4 == 1 ){
   $sql_text .= " AND email='FORM{'email'}'";
 }
 $flag4 = 1;
}
$flag3 = 1;
why not
 if( $FORM{'department'} ){
   $sql_text .= " AND department='FORM{'department'}'";
 }
 if( FORM{'email' }{
   $sql_text .= " AND email='FORM{'email'}'";
 }
or just
$sql_text .= join ' AND ',map "$_='$FORM{$_}'", grep defined($FORM{$_}), qw(last first job_title department email)
Or if you want to do a loop, how about
for( qw(last first job_title department email) ){
        $sql_text .= " AND $_='FORM{$_}'" if( FORM{$_} );
}

As is, you're loopoing over the elements of the @FORM array,
sucessively seting $_ to each element, but nowhere within your loop
do you deal with either the loop variable $_, nor with @FORM or any of it's elements.
(you do deal with the elements of the %FORM hash, which leads me to suspect
that you may have gotten the idea that they were related somehow)


ASKER CERTIFIED SOLUTION
Avatar of malec
malec

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Since neither $anyvar nor @parts seem to be used,
I don't see how that could improve anything.
Don't take it literally. Use your vars instead.
So if your vars are $_ and @FORM, the proposed answer becomes the same as
for( @FORM ){
You're right, ozo. I didn't know "for" also works like that.