Solved

loop

Posted on 1997-11-27
8
234 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
perl script 4 97
Strange perl issue 6 126
Need Helping Editing Perl Script for PowerChute Network Shutdown 3 77
Put query string from URL request -  PERL HTTP Daemon 2 73
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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

867 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

20 Experts available now in Live!

Get 1:1 Help Now