#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0

    Multiple Checkbox Row in If Else Condition for Once elemnt


    I have total 6 checkboxs for 6 pages ( not group but individual ) and storing state in separate table row with own key and value.

    I have a function like in WordPress get_option() to get value for the checkbox and things are working fine for single condition.

    I have one html element and I want to activate that only on selected pages ( selected checkboxes ). This I can do with below statement.

    PHP Code:
    if(get_option('chkeckbox1') == true){
    //display my element
    }
    if(
    get_option('chkeckbox2') == true){
    //display my element
    }
    if(
    get_option('chkeckbox3') == true){
    //display my element

    But this is obviously not right way to do. I am looking some way where I can get this in such a way where I have to define only one statement something like in_array() or anything else.

    Can anyone hlep me to get this done?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Since you did not post it, it is hard to say without knowing what 'get_option' does. You asked about in_array but I don't see an array in your code. Are you planning to eliminate the function and turn it into an array? Unless I am misunderstanding what you are asking, that seems like a move in the right direction.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    And after your if() items, is the coding going to be the same for each one? If not, a loop wouldn't be your best way to go, since you are doing nothing alike.

    If the content within the 'then' of the if() IS going to be the same, then foreach() would probaly be in interest. But, with a checkbox, keep in mind that these aren't really true/false items. They pass through pretty much as true if a user selects one, but if the user does NOT select it, then it straight out doesn't exist. So, when checking condition, use the isset() function.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by gw1500se
    Since you did not post it, it is hard to say without knowing what 'get_option' does. You asked about in_array but I don't see an array in your code. Are you planning to eliminate the function and turn it into an array? Unless I am misunderstanding what you are asking, that seems like a move in the right direction.
    Sorry about misunderstanding I am new and even not completely aware with technical terms for every aspects in PHP and MySQL.

    get_option: This is the function where I need to pass the index key and it will retrieve the value

    So what exactly I am doing that I am creating 5-6 check boxes. Consider 5-6 input elements but not in group and each storing value with own key let's say home, about, contact, portfolio.

    Now here I have few pages like home, about, contact, portfolio, where I want to display one HTML element ( this will be the same on every page ) if check-box is on for that page

    Or consider Sidebar which is includes in the template now I want to display sidebar on the pages where check-box is on. If checkbox is on for Home, Contact and Portfolio than it will display on those pages and rest pages will not display sidebar.

    I also want reverse where if checkbox is on it will hide the sidebar

    I hope this will helps to understand.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    You probably need to post 'get_option'. If it is returning a value, then checking it for 'true' will not always work. Is 'get_option' trying to return if a particular box is checked? Triple_nothing offered a number of tips you need to consider in your code.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by gw1500se
    You probably need to post 'get_option'. If it is returning a value, then checking it for 'true' will not always work. Is 'get_option' trying to return if a particular box is checked? Triple_nothing offered a number of tips you need to consider in your code.
    I can get it work with simply using

    PHP Code:
    if (get_option('key')){
    //this will display code if it is true

    OR
    PHP Code:
    if (get_option('key') == true){
    //this will display code if it is true

    Both of the codes above works fine.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    Well, in that manner, the code below will also run, because it evaluates the if() as true, simply because it exists and holds a value.

    PHP Code:
    $var "something";
    if(
    $var) {
      
    // Execute this code...

    This will evaluate to false, simply because it doesn't exist, or is NULL

    PHP Code:
    if($var2) {
      
    // Some code

    This WILL run, because $var3 DOES exists, and hold a value. Granted the value may not be of visual type, it is like a piece of paper. Blank paper is a value, no paper is NULL.
    PHP Code:
    $var3 "";
    if(
    $var3) [
      
    // More code...

  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by Triple_Nothing
    Well, in that manner, the code below will also run, because it evaluates the if() as true, simply because it exists and holds a value.

    PHP Code:
    $var "something";
    if(
    $var) {
      
    // Execute this code...

    This will evaluate to false, simply because it doesn't exist, or is NULL

    PHP Code:
    if($var2) {
      
    // Some code

    This WILL run, because $var3 DOES exists, and hold a value. Granted the value may not be of visual type, it is like a piece of paper. Blank paper is a value, no paper is NULL.
    PHP Code:
    $var3 "";
    if(
    $var3) [
      
    // More code...

    But that this is what I don't want to do. I mean writing condition for every checkbox individually for the same code which is not efficient way and I believe there must be some other way to shorten the code.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Since you don't want to post 'get_option', for whatever reason, there is probably little we can do to help.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by gw1500se
    Since you don't want to post 'get_option', for whatever reason, there is probably little we can do to help.
    No its not that I don't want to post but it is built in function and I can't get it from the system.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2003
    Posts
    3,508
    Rep Power
    594
    Are you using WordPress?
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    677
    Rep Power
    7
    This should build your checkboxes into an array, and even if not checked, will still hold a value, FALSE if you wish...

    Code:
    <input type="hidden" name="checkbox_name[]" value="unchecked_value" />
    <input type="checkbox" name="checkbox_name[]" value="checked_value" />
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Originally Posted by gw1500se
    Are you using WordPress?
    No it is not wordpress
  26. #14
  27. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    11
    Rep Power
    0
    Okay I have done with simple effort just using condition with OR

    PHP Code:
    if( !(get_option('key') || !(get_option('key')....)
    //my code.. 
    this works as I wanted.. but just there are almost 12 keys to check so I was thinking if anything which can optimize the code..

    never mind thanks a lot for your help guyz. appreciated..

IMN logo majestic logo threadwatch logo seochat tools logo