Solved

loop

Posted on 1997-11-27
8
236 Views
Last Modified: 2006-11-17
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??
0
Comment
Question by:kudak
  • 4
  • 3
8 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1208890
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);

0
 

Author Comment

by:kudak
ID: 1208891
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208892
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)


0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 1

Accepted Solution

by:
malec earned 10 total points
ID: 1208893
Here's your problem:

for (@FORM) {

try this instead:
foreach $anyvar (@parts)
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208894
Since neither $anyvar nor @parts seem to be used,
I don't see how that could improve anything.
0
 
LVL 1

Expert Comment

by:malec
ID: 1208895
Don't take it literally. Use your vars instead.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208896
So if your vars are $_ and @FORM, the proposed answer becomes the same as
for( @FORM ){
0
 
LVL 1

Expert Comment

by:malec
ID: 1208897
You're right, ozo. I didn't know "for" also works like that.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question