8 Replies Latest reply: Jan 28, 2012 12:53 AM by Drymetal RSS

    IF elseif else usage

    Drymetal Community Member

      So, now I've been trying to learn If, elseif and else statements properly.  And I'm doing something wrong. 

       

      A guy told me his website would email specific people based on what zipcode they put in the contact form on his site.  I was wondering how he did it, so I tried to recreate something similar.

       

      Here is what I've done:

       

      1. I created a file called zipcodes.php

      2. In this file it has set every zipcode to a variable.  For instance:

      <?php
      $Nashville=("37010" or "37027" or "37040" or "37055" or "37152");
      ?>
      

       

      3.  I created a form with a field called 'zip'.

      4. I created a php file called phpmail.php to send this form.

      5. Right now, the top of this file looks like this:

       

      <?php
      include("zipcodes.php");
      $zip=$_POST["zip"];
      //NASHVILLE//
      if ($zip==$Nashville)
       $my_email = "email1@mysite.com";
       //Knoxville//
       if ($zip==$Knoxville)
       $my_email = "email2@mysite.com";
       //Huntsville//
       if ($zip==$Huntsville)
       $my_email = "email3@mysite.com";
      ...etc.
      

       

      This doesn't work.  It sends them all to the last option which is texas or email15@mysite.com

       

      Before I had the first if set to if and all the others set to elseif

      But that didn't work.  It sent all the emails to Nashville email I made.

       

      If statements must work though in this way.  Because if I have this:

      if ($zip==$Nashville)
       $my_email = "email1@mysite.com";
           //Anything Else//
        else $my_email = "hippityhoppity@live.com";
      

       

      It works.  Meaning if a zipcode is in Nashville it goes to one email - and if it isn't in Nashville - it goes to hippityhoppity.

       

      Am I not getting something about all of this?  Thanks!

        • 1. Re: IF elseif else usage
          Drymetal Community Member

          Using the brackets doesn't work either.  All of this goes to the first email.  It doesn't make sense.  This is exactly what this php book says needs to be done. 

           

          <?php
          include("zipcodes.php");
          $zip=$_POST["zip"];
          if ($zip==$Nashville){
           $my_email = "nashville@mysite.com";
          } elseif ($zip==$Knoxville){
           $my_email = "knoxville@mysite.com";
          } elseif ($zip==$Huntsville){
           $my_email = "huntsville@mysite.com";
          } elseif ($zip==$Florida){
           $my_email = "florida@mysite.com";
          } elseif ($zip==$Georgia){
           $my_email = "georgia@mysite.com";
          } elseif ($zip==$SouthCarolina){
           $my_email = "scarolina@mysite.com";
          } elseif ($zip==$NorthCarolina){
           $my_email = "ncarolina@mysite.com";
          } elseif ($zip==$Pennsylvania){
           $my_email = "penn@mysite.com";
          } elseif ($zip==$Maryland){
           $my_email = "maryland@mysite.com";
          } elseif ($zip==$Virginia){
           $my_email = "virginia@mysite.com";
          } elseif ($zip==$Texas){
           $my_email = "texas@mysite.com";
          } else { $my_email = "hippityhoppity43204@live.com";
          }
          
          
          • 2. Re: IF elseif else usage
            Burak Ueda Community Member

            Arrays are useful in these type of situations:

             

            $nashville = Array("37010","37027","37040","37055","37152");

            $knoxville = Array("32010","32027","32040","32055","32152");

            $huntsville = Array("47010","47027","47040","47055","47152");

             

            if(in_array($zip, $nashville)) {

                 $my_email = "......";

            } elseif(in_array($zip, $knoxville) {

                 $my_email = "......";

            } else {

                 $my_email = "....";

             

            But if you have a lot of locations and zip codes to compare, I would suggest you to use a database like MySQL.

            • 3. Re: IF elseif else usage
              Drymetal Community Member

              Ok, after trying 3,894 different ideas, I solved the issue.  In case any crazy person like me wants to ever do this, I'll post my solution.

               

              In zipcodes.php, I have arrays for each place.  E.g.:

               

              <?php
              $Nashville = array (37010, 37027);
              ?>
              
              <?php
              $Knoxville=array(37354, 37709, 37717);
              ?>
              
              <?php
              $Huntsville=array(35019, 35179, 35612);
              ?>
              

               

              Then in my phpmail.php file (the thing that processes the email) I have this:

               

              <?php
              include("zipcodes.php");
              
              $zip=$_POST["zip"];
              
                        if (in_array($zip, $Nashville)) {
               $my_email = "nashville@mysite.com";
              
                        }elseif (in_array($zip, $Knoxville)) {
               $my_email = "Knoxville@mysite.com";
              
                        }elseif (in_array($zip, $Huntsville)) {
               $my_email = "Huntsville@mysite.com";
              
              } else { $my_email = "hippityhoppity@mysite.com";
              
              }
              

               

               

              And it works!    And if there zipcode isn't in one of the arrays, it goes to the email used in the else statement.  : ) : ) : )

              • 4. Re: IF elseif else usage
                Drymetal Community Member

                Figures you'd answer the same time I came up with it.  Would've saved me a 12 pack of beer and frustration. 

                Though it makes me excited to see that my code looks like yours.  : ) 

                 

                You say to use a database instead.  Why?  Wouldn't that put more strain on the server or am I wrong?

                How would I do that?  With recordsets?

                • 5. Re: IF elseif else usage
                  Burak Ueda Community Member

                  Yeah! Exactly same time to the minute, what a coincidence!

                  As for using a database, it all depends how many zip codes you have to compare, If you are talking about tens of thousands of zip codes from hundreds of cities all over the country, then yes a database will be faster.

                  • 6. Re: IF elseif else usage
                    Drymetal Community Member

                    Yeah, pretty strange answering to the minute.  lol

                    There are about 3,000 zipcodes.  But there isn't a practical purpose for this.  So maybe I should learn how to get it from a database. 

                    BTW, if I wanted one of these to go to two emails instead of just one, how would I do that? 

                     

                    I tried:

                    if (in_array($zip, $Nashville)) {
                     $my_email = "nashville@mysite.com" . "nashville2@mysite.com;

                     

                    But that didn't send the email to anyone.  Isn't that how you do AND?

                    • 7. Re: IF elseif else usage
                      Burak Ueda Community Member

                      What is your emailing script like?

                      Appending the email addresses like that will result a corrupt address like: nashville@mysite.comnashville2@mysite.com

                       

                      It all depends how is your emailing scripts excepts data.

                       

                       

                      Edit: I hate Adobe for using this stupid forum software. It puts links automatically and won't allow me to write in a plain text!

                      • 8. Re: IF elseif else usage
                        Drymetal Community Member

                        Yeah I know.  I also wish it'd let me use PHP syntax instead of plain.  I think it'd make things easier to read.

                        Here is the entire script.  It is made to work with any form with little regard for what the form has in it.  So long as there is an email field and a zip field.

                         

                        <?php

                        include("zipcodes.php");

                        $zip=$_POST["zip"];

                                  if (in_array($zip, $Nashville)) {

                        $my_email = ".....";

                         

                         

                                  }elseif (in_array($zip, $Knoxville)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $Huntsville)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $Florida)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $Georgia)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $SouthCarolina)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $NorthCarolina)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $Pennsylvania)) {

                        $my_email = ".....";

                         

                                  }elseif (in_array($zip, $Maryland)) {

                        $my_email = ".....";

                         

                                   }elseif (in_array($zip, $Virginia)) {

                        $my_email = ".....";

                         

                                   }elseif (in_array($zip, $Texas)) {

                        $my_email = ".....";

                         

                        } else { $my_email = ".....";

                         

                         

                        }

                         

                         

                         

                         

                        $continue = "/";

                         

                         

                         

                         

                        $errors = array();

                         

                         

                         

                         

                        if(count($_COOKIE)){foreach(array_keys($_COOKIE) as $value){unset($_REQUEST[$value]);}}

                         

                         

                         

                         

                        function recursive_array_check_header($element_value)

                        {

                         

                         

                        global $set;

                         

                         

                        if(!is_array($element_value)){if(preg_match("/(%0A|%0D|\n+|\r+)(content-type:|to:|cc:|bcc: )/i",$element_value)){$set = 1;}}

                        else

                        {

                         

                         

                        foreach($element_value as $value){if($set){break;} recursive_array_check_header($value);}

                         

                         

                        }

                         

                         

                        }

                         

                         

                        recursive_array_check_header($_REQUEST);

                         

                         

                        if($set){$errors[] = "You cannot send an email header";}

                         

                         

                        unset($set);

                         

                         

                         

                         

                        if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))

                        {

                         

                         

                        if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "Email address may not contain a new line or a colon";}

                         

                         

                        $_REQUEST['email'] = trim($_REQUEST['email']);

                         

                         

                        if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "Email address is invalid";}else{$exploded_email = explode("@",$_REQUEST['email']);if(empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "Email address is invalid";}else{if(substr_count($exploded_email[1],".") == 0){$errors[] = "Email address is invalid";}else{$exploded_domain = explode(".",$exploded_email[1]);if(in_array("",$exploded_domain)){$errors[] = "Email address is invalid";}else{foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "Email address is invalid"; break;}}}}}}

                         

                         

                        }

                         

                         

                         

                         

                        if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "You must enable referrer logging to use the form";}

                         

                         

                         

                         

                        function recursive_array_check_blank($element_value)

                        {

                         

                         

                        global $set;

                         

                         

                        if(!is_array($element_value)){if(!empty($element_value)){$set = 1;}}

                        else

                        {

                         

                         

                        foreach($element_value as $value){if($set){break;} recursive_array_check_blank($value);}

                         

                         

                        }

                         

                         

                        }

                         

                         

                        recursive_array_check_blank($_REQUEST);

                         

                         

                        if(!$set){$errors[] = "You cannot send a blank form";}

                         

                         

                        unset($set);

                         

                         

                         

                         

                        if(count($errors)){foreach($errors as $value){print "$value<br>";} exit;}

                         

                         

                        if(!defined("PHP_EOL")){define("PHP_EOL", strtoupper(substr(PHP_OS,0,3) == "WIN") ? "\r\n" : "\n");}

                         

                         

                         

                         

                        function build_message($request_input){if(!isset($message_output)){$message_output ="";}if(!is_array($request_input)){$message_output = $request_input;}else{foreach($request_input as $key => $value){if(!empty($value)){if(!is_numeric($key)){$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}

                         

                         

                        $message = build_message($_REQUEST);

                         

                         

                        $message = $message . PHP_EOL.PHP_EOL."-- ".PHP_EOL."";

                         

                         

                        $message = stripslashes($message);

                         

                         

                        $subject = "FormToEmail Comments";

                         

                         

                        $headers = "From: " . $_REQUEST['email'];

                         

                         

                        mail($my_email,$subject,$message,$headers);

                         

                         

                        ?>

                         

                         

                        <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

                         

                         

                        <html>

                         

                         

                        <head>

                         

                         

                        <title>Dreamweaver Tutorial - Contact Form</title>

                        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

                        </head>

                         

                         

                        <body bgcolor="#ffffff" text="#000000">

                         

                         

                        <div>

                        <center>

                        <b>Thank you <?php print stripslashes($_REQUEST['name']); ?></b>

                        <br>Your message has been sent

                        <p><a href="<?php print $continue; ?>">Click here to continue</a></p>

                        </center>

                        </div>

                         

                         

                        </body>

                        </html>