SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: IF and ELSEIF

  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    UK
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    IF and ELSEIF

    Hi All

    Cant get this to work... if $country is United Kingdom it works... if $country is any of the european countries in the second elseif it works....

    it is the thrid elseif that i am having the problem with... if $country is anything other than that defined in the first two if's it still shows it as european postage...

    hope this makes sense....

    any ideas..
    thanks
    steve


    PHP Code:

    IF ($country == 'United Kingdom') {
    $postageprice '0';
    } ELSEIF (
    $country == 'Andorra' OR 'Austria' OR 'Belgium' OR 'Bulgaria' OR 'Croatia' OR 'Cyprus' OR 'Czech Republic' OR 'Denmark' OR 'Estonia' OR 'Finland' OR 'France' OR 'Germany' OR 'Greece' OR 'Hungary' OR 'Iceland' OR 'Ireland' OR 'Italy' OR 'Latvia' OR 'Lithuania' OR 'Luxembourg' OR 'Malta' OR 'Monaco' OR 'Netherlands' OR 'Norway' OR 'Poland' OR 'Portugal' OR 'Romania' OR 'Slovakia' OR 'Slovenia' OR 'Spain' OR 'Sweden' OR 'Switzerland' OR 'Turkey' OR 'Ukraine' OR 'Vatican City') {
    $postageprice $oneyeareuropepostage;
    echo 
    "</tr><tr>";
    echo 
    "<td width='250'>European Postage:</td><td>$oneyeareuropepostage</td>";
    } ELSEIF (
    $country != 'United Kingdom' OR 'Andorra' OR 'Austria' OR 'Belgium' OR 'Bulgaria' OR 'Croatia' OR 'Cyprus' OR 'Czech Republic' OR 'Denmark' OR 'Estonia' OR 'Finland' OR 'France' OR 'Germany' OR 'Greece' OR 'Hungary' OR 'Iceland' OR 'Ireland' OR 'Italy' OR 'Latvia' OR 'Lithuania' OR 'Luxembourg' OR 'Malta' OR 'Monaco' OR 'Netherlands' OR 'Norway' OR 'Poland' OR 'Portugal' OR 'Romania' OR 'Slovakia' OR 'Slovenia' OR 'Spain' OR 'Sweden' OR 'Switzerland' OR 'Turkey' OR 'Ukraine' OR 'Vatican City') {
    $postageprice $oneyearworldpostage;
    echo 
    "</tr><tr>";
    echo 
    "<td width='250'>World Postage:</td><td>$oneyearworldpostage</td>";


  2. #2
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would strongly suggest you use switches instead of the long list of or's:
    http://us2.php.net/manual/en/control...res.switch.php

    This will result is both easier to read code as well as better overall performance. While it may be fractions of a second improvement, that ads up when you scale.

    Review the overall logic as it really doesn't look logical.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    UK
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks inclick..

    changed it to this and it works perfectly...

    have never used switch before...cheers...

    PHP Code:
    switch($country)
    {
        case 
    'United Kingdom';
    $postageprice '0';
        break;

          case 
    'Andorra';
          case 
    'Austria';
          case 
    'Belgium';
          case 
    'Bulgaria';
          case 
    'Croatia';
          case 
    'Cyprus';
          case 
    'Czech Republic';
          case 
    'Denmark';
          case 
    'Estonia';
          case 
    'Finland';
          case 
    'France';
          case 
    'Germany';
          case 
    'Greece';
          case 
    'Hungary';
          case 
    'Iceland';
          case 
    'Ireland';
          case 
    'Italy';
          case 
    'Latvia';
          case 
    'Lithuania';
          case 
    'Luxembourg';
          case 
    'Malta';
          case 
    'Monaco';
          case 
    'Netherlands';
          case 
    'Norway';
          case 
    'Poland';
          case 
    'Portugal';
          case 
    'Romania';
          case 
    'Slovakia';
          case 
    'Slovenia';
          case 
    'Spain';
          case 
    'Sweden';
          case 
    'Switzerland';
          case 
    'Turkey';
          case 
    'Ukraine';
          case 
    'Vatican City';
    $postageprice $oneyeareuropepostage;
    echo 
    "</tr><tr>";
    echo 
    "<td width='250'>European Postage:</td><td>$oneyeareuropepostage</td>";
        break;


        default;
    $postageprice $oneyearworldpostage;
    echo 
    "</tr><tr>";
    echo 
    "<td width='250'>World Postage:</td><td>$oneyearworldpostage</td>";
        break;


  4. #4
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Glad to see that worked. Looks a lot easier to understand as well. Happy coding.
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd stick it in a db (you probably wanna edit prices), or use arrays:
    PHP Code:
    <?php
    $europrice 
    1;
    $oceaniaprice 2;
    $postage = array(
        
    'Austria' => $europrice,
        
    'Belgium' => $europrice,
        
    'Australia' => $oceaniaprice,
    );
    ?>
    <td width='250'>World Postage:</td><td><?php echo $postage[$country?></td>
    or even
    PHP Code:
    <?php
    $areas 
    = array(
        
    'europe' => array('Austria''Belgium'),
        
    'oceania' => array('Australia''NZ'),
    );
    $prices = array(
        
    'europe' => 1,
        
    'oceania' => 2,
    );

    $price '';
    foreach(
    $areas as $k => $v) {
        if(
    in_array($country$v)) {
            
    $price prices[$k];
            break;
        }
    }
    ?>
    <td><?php echo $price ?></td>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •