Solved

loop

Posted on 1997-11-27
8
233 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
Comment Utility
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
Comment Utility
Edited text of question
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
 
LVL 1

Accepted Solution

by:
malec earned 10 total points
Comment Utility
Here's your problem:

for (@FORM) {

try this instead:
foreach $anyvar (@parts)
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
Don't take it literally. Use your vars instead.
0
 
LVL 84

Expert Comment

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

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now