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.
Results 1 to 5 of 5

Thread: If isset help

  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts

    If isset help

    Hello,

    I'm trying to create a simple if isset statement to determine if apples or oranges are selected as a category, so i can have the view page url be different. This works if I only have one category represented in isset like if
    Code:
    (isset($person['category']) && $person['category'] == 'Apples'
    or the other one but when I try to combine the two it doesn't work. Wondering if someone can see what I'm doing wrong? Can you not have multiple things in isset?

    Code:
    if (isset($person['person_id'])) {
                				    if (isset($person['category']) && $person['category'] == 'Apples' && $person['category'] == 'Oranges') {
                                        echo 'Edit (<a href="../apples_and_oranges/people/?id='.$person['person_id'].'">View Page</a>)</dt>';
                                    } else {
                                        echo 'Edit (<a href="../people/?id='.$person['person_id'].'">View Page</a>)</dt>';
                                    }
    							} else {
    								echo '<dt>Add Person</dt>';
    							}

  • #2
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    919
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Good evening,

    Should there only be one selected at any one time?

    So you want this:

    PHP Code:
    if(isset($person['person_id']))
    {
          if(isset(
    $person['category']))
          {
                  if(
    $person['category'] == "Apples")
                 {
                          
    //echo apple URL
                 
    }
                 else
                 {
                         
    //echo other URL
                 
    }
          }
          else
          {
                 
    //do something else
          

    }
    else
    {
          
    //do something else

    Where are you getting the values from, a form?

    Kind regards,

    LC.
    Last edited by LearningCoder; 09-13-2013 at 11:59 PM.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    yea a form...

    that works... i also found this works as well...

    Code:
    if (isset($person['category']) && $person['category'] == 'Apples' || $person['category'] == 'Oranges') {
    I was using && instead of || but 'or' seems to work. Thanks

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That won't work. At least not proper.
    If $person['category'] is not set, than it will trigger a warning when you try to see if $person['category'] is 'Orange'. The idea is right, but the precedence is wrong.
    || is lower in priority than &&. the isset($person['category']) is together with the apples check. So it will either be set AND apples, OR it will be orange. This means if its never set you will get an error.
    This will work:
    PHP Code:
    if (isset($person['category']) && ($person['category'] == 'Apples' || $person['category'] == 'Oranges')) 
    You may want to consider using strcasecmp instead so you can use insensitive searching. If the result of that is 0 than the strings are deemed the same.
    You can also exploit PHP's dual precedence handling.
    PHP Code:
    if (isset($person['category']) AND $person['category'] == 'Apples' || $person['category'] == 'Oranges'
    Will also work. AND has a lower priority than || so the comparison for the two types of category will be done first. This *shouldn't* trigger an unset error either if I'm not mistaken (I rarely recommend such an approach; brackets are far cleaner).

    My favorite:
    PHP Code:
    $aValid = array(
        
    'Apples',
        
    'Oranges'
    );

    if (isset(
    $person['category']) && in_array($person['category'], $aValid)) 
    This lets you scale it with whatever items you want and can be hardcoded or datastore driven. Case needs to match or a copy can be walked and lowered / uppered for comparison if needed.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

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

    LearningCoder (09-14-2013)

  • #5
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thanks fou-lou, your first option works just fine for my purposes. i'll look into your other ones as well but this is for a very basic site so simple is fine.


  •  

    Posting Permissions

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