Solved

loop

Posted on 1997-11-27
8
239 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…

751 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