Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts

    Exclamation if, elseif help please

    Hi all,

    i have what looks to be complicated if statments which im sure can be condensed much smaller

    here is the first if statment which works
    PHP Code:
    if($itemno <= 9){
        
    $itemtype "DVD";
        }
        elseif (
    $itemno >=10 AND $itemno <=18){
        
    $itemtype "CD";
        }
        elseif (
    $itemno >=19 AND $itemno <=27){
        
    $itemtype "Video Games";
        }
        elseif (
    $itemno >=28 AND $itemno <=36){
        
    $itemtype "Blu-Ray";
        }
        elseif (
    $itemno >=37 AND $itemno <=45){
        
    $itemtype "Books";
        } 
    can the above be reduced to get the same result ?

    and the second if statment which does not work for some reason

    PHP Code:
    if($itemno >=AND $itemno <=3) or ($itemno >=10 AND $itemno <=12) or ($itemno >=19 AND $itemno <=21) or ($itemno >=28 AND $itemno <=30) or ($itemno >=37 AND $itemno <=39){
        
    $subcat "Bestsellers";
        }
        elseif(
    $itemno >=AND $itemno <=6) or ($itemno >=13 AND $itemno <=15) or ($itemno >=22 AND $itemno <=24) or ($itemno >=31 AND $itemno <=33) or ($itemno >=40 AND $itemno <=42){
        
    $subcat "New Releases";
        }
        elseif(
    $itemno >=AND $itemno <=9) or ($itemno >=16 AND $itemno <=18) or ($itemno >=25 AND $itemno <=27) or ($itemno >=34 AND $itemno <=36) or ($itemno >=43 AND $itemno <=45){
        
    $subcat "Gifted";
        } 
    again if this can be condensed and corrected i'd really appreciate it

    many thanks
    Luke
    Last edited by LJackson; 05-06-2009 at 01:39 AM.

  • #2
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    and the second if statment which does not work for some reason
    Do you have an error you could post?

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    yep sorry mate forgot to include it ooops...

    Parse error: syntax error, unexpected T_LOGICAL_OR in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainmentPCtest.php on line 68

  • #4
    Regular Coder
    Join Date
    Dec 2007
    Posts
    145
    Thanks
    5
    Thanked 5 Times in 5 Posts
    Also, shouldn't
    or be replaced with ||
    and AND be replaced with &&

    Correct me if I"m wrong...

  • Users who have thanked nobackseat88 for this post:

    LJackson (05-06-2009)

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    dunno mate it seems to work ok for the first one? i'll try it though

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    hi i tried this

    PHP Code:
        if($itemno >=&& $itemno <=3) || ($itemno >=10 && $itemno <=12) || ($itemno >=19 && $itemno <=21) || ($itemno >=28 && $itemno <=30) || ($itemno >=37 && $itemno <=39){
        
    $subcat "Bestsellers";
        } 
    but am getting

    Parse error: syntax error, unexpected T_BOOLEAN_OR in /customers/kernow-connect.com/kernow-connect.com/httpd.www/entertainmentPCtest.php on line 68

    cheer

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    My shot at this.
    Not necessarily shorter, but easier ...
    PHP Code:
    <?php

    $itemno
    =30;

    if(
    $itemno<=45){
    $itemtype "Books";
    }
    if(
    $itemno<=36){
    $itemtype "Blu-Ray";
    }
    if(
    $itemno<=27){
    $itemtype "Video Games";
    }
    if(
    $itemno<=18){
    $itemtype "CD";
    }
    if(
    $itemno<=9){
    $itemtype "DVD";
    }

    echo 
    $itemtype;
    ?>

  • Users who have thanked mlseim for this post:

    LJackson (05-06-2009)

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    thanks mlseim that looks tidy and easier to read

    but does it work lol cant test it until the other one is resolved...
    im sure it will.

    cheers mate
    Luke

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    As for the second if, its because you're brackets are mismatched:
    PHP Code:
    if($itemno >=AND $itemno <=3) or ($itemno >=10 AND $itemno <=12) or ($itemno >=19 AND $itemno <=21) or ($itemno >=28 AND $itemno <=30) or ($itemno >=37 AND $itemno <=39){ 
    It evaluates the if ($itemno >=1 AND $itemno <= 3) and then stops the conditional and fails on the 'or'. If you use a complex statement like this, you can exploit the operator precedence - || has a higher weight than AND, so you can get away without any brackets and use the || and AND operators.

    As for you're first question, yes, by exploiting bounds.

    PHP Code:
    if($itemno <= 9)
    {
        
    $itemtype "DVD";
    }
    elseif (
    $itemno <=18)
    {
        
    $itemtype "CD";
    }
    elseif (
    $itemno <=27)
    {
        
    $itemtype "Video Games";
    }
    elseif (
    $itemno <=36)
    {
        
    $itemtype "Blu-Ray";
    }
    elseif (
    $itemno <=45)
    {
        
    $itemtype "Books";

    Edit:
    Holy crap, this had tons of posts, there were none when I started writing >.<
    AND and OR are valid operators, but have lower precedence weights than && and ||.
    Last edited by Fou-Lu; 05-06-2009 at 01:16 AM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    LJackson (05-06-2009)

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    lol... thank you Fou-Lu that works a treat.

    cheers everyone for your help
    Luke

  • #11
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Oh, I guess I'm too late to join in on the fun. And boy are those solutions still complicated.

    Here's my version:

    PHP Code:
    $itemtypes = array("DVD""CD""Video Games""Blu-Ray""Books");
    $subcats = array("Bestsellers""New Releases""Gifted");

    $itemtype $itemtypes[ceil($itemno 9) - 1];
    $subcat $subcats[ceil((($itemno 1) % 1) / 3) - 1]; 
    Or, make those magic numbers dependent on array length, then you can easily add new categories by adding elements to the arrays.
    Last edited by venegal; 05-06-2009 at 01:48 AM.

  • Users who have thanked venegal for this post:

    LJackson (05-06-2009)

  • #12
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Your never to late to join in the fun

    thank you for your input venegal it is definatly more condensed thank the previous examples but when looking back at this in a month or two im not going to know what the code does or how to change it
    so im going to stick with what ive got as i am able to understand it :P

    its nice to see the different ways in which the same thing can be accomplished

    just out of curiosity what does ceil mean and the numbers after it??? might help me understand whats going on

    cheers mate
    Luke

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,537
    Thanks
    8
    Thanked 1,093 Times in 1,084 Posts
    Venegal ... smarta**

    but it's fun to see the various ways ... good for PHP novices.

  • #14
    Senior Coder
    Join Date
    Jun 2008
    Location
    Cornwall
    Posts
    2,099
    Thanks
    297
    Thanked 12 Times in 12 Posts
    Quote Originally Posted by mlseim View Post
    venegal ... Smarta**

    but it's fun to see the various ways ... Good for php novices.
    agreed!!!

  • #15
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Alright, first line, let's start from the inside out:
    itemno 1-9 are in the first category, 10-18 in the second cat, and so on.
    If you divide those numbers by 9, the numbers 1-9 become some float larger than 0 and smaller or equal 1; 10-18 similarly become floats between 1 and 2, and so on.
    ceil() returns the next highest integer, so what was originally the numbers 1-9, then something between 0 and 1, now all become 1; 10-18 all become 2, and so on.
    Now just substract one, because the array keys start from 0, not from 1.

    Second line:
    Maybe you can figure it out on your own. If you have questions, just ask.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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