Skip navigation
Currently Being Moderated

IF elseif else usage

Jan 27, 2012 9:28 PM

Tags: #php #mysql #if #else #elseif

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!

 
Replies
  • Currently Being Moderated
    Jan 28, 2012 12:17 AM   in reply to Drymetal

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2012 12:26 AM   in reply to Drymetal

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 28, 2012 12:49 AM   in reply to Drymetal

    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!

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points