• I set up a multi-select field in my content type “Product”. With this, the admin user could use checkboxes to indicate which options are available for a particular product. Then, with an additional code for a cluetip, someone could mouseover the name of the options in the front-end, and have a cluetip pop up which has further information about that option (what it costs extra, what kinds of modifications are done from the original product to achieve this option, etc…) However, I want to make sure that each of these options (output filter is set to “array” right now) only displays if the administrator has checked the box. If not, the below code should just echo “nope” for every unchecked option.

    <strong> Option Array: </strong>
    <?php
    $my_arr = get_custom_field('product_options');
    
    $opts = array(
        'Option 1' => '<div>Option 1 description</div>',
        'Option 2' => '<div>Option 2 description</div>',
        'Option 3' => '<div>Option 3 description</div>',
    );
    
    foreach($my_arr as $val) {
    if (in_array($val, array_keys($opts))) echo $val . "<br/>"; else echo 'nope<br/>';
    } ?>

    Can this be done with said conditionals? Currently my efforts to have the word “nope” echoed in place of an option (in that array) which is not toggled on in the backend, has not been successful.

    How would it look if neither of the 3 options were checked. Currently, with the above code, if I have a product content type with no additional optiosn checked on by an admin, it just returns the error:

    Warning: Invalid argument supplied for foreach()

    For those who would want to suggest I ask this on a PHP forum (since it does deal with if/else statements), the current topic is going on in this Stack Overflow question:

    https://stackoverflow.com/questions/6741370/in-array-based-if-then-else-statement

Viewing 15 replies - 1 through 15 (of 22 total)
  • Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Hmm… I wouldn’t do it quite like that. I would get the array from array_keys($opts) once, then use it later on. Do this for me and paste the results back in:

    <?php
    $my_arr = get_custom_field('product_options');
    
    $opts = array(
        'Option 1' => '<div>Option 1 description</div>',
        'Option 2' => '<div>Option 2 description</div>',
        'Option 3' => '<div>Option 3 description</div>',
    );
    $my_opts = array_keys($opts);
    print_r($my_arr);
    print_r($my_opts);

    And can you confirm which options are checked for that particular post.

    Thread Starter WebmistressM

    (@webmistressm)

    Alright, in the first instance, I checked option 1 and 3 on and left Option 2 unchecked.

    It returned: Array ( [0] => Option 1 [1] => Option 3 ) Array ( [0] => Option 1 [1] => Option 2 [2] => Option 3 )
    screencap: https://leopardspot.endofinternet.net:81/ImageServer/2optionschecked.jpg

    Then I unchecked them all and it printed out:
    Array ( [0] => Option 1 [1] => Option 2 [2] => Option 3 )
    screencap: https://leopardspot.endofinternet.net:81/ImageServer/nooptionschecked.jpg

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Actually, you know what? I’m a dumbass. OF COURSE this will never print “nope” because ALL available options (i.e. members of $my_arr) are going to be in the array of options ($opts_arr). Think about it … “Option 1” is in the $opts_arr, “Option 3” is in $opts_arr. It doesn’t matter what options you check.

    So flip it around: iterate through the $opts_arr items (i.e. iterate through ALL possible items) and check if that exists in the checked items (i.e. check if they exist in the $my_arr).

    Try something like this to print the descriptions:

    <strong> Option Array: </strong>
    <?php
    $my_arr = get_custom_field('product_options');
    
    $opts = array(
        'Option 1' => '<div>Option 1 description</div>',
        'Option 2' => '<div>Option 2 description</div>',
        'Option 3' => '<div>Option 3 description</div>',
    );
    
    $opts_arr = array_keys($opts);
    
    foreach($my_opts as $opt) {
       if ( in_array($opt, $my_arr) ) {
          print $opts[$opt]; // will print the description for checked items.
       }
       else {
         print $opt . 'was not checked.';
       }
    } ?>
    Thread Starter WebmistressM

    (@webmistressm)

    First off, @fireproofsocks, you are *not* a dumbass. I have heard all degrees of experienced (and inexperienced) programmers have a page fall apart because they put in an extra comma or parantheses in a statement. I think we all do it. Thats why us web designers and programmers run on coffee. ??

    Anyhow, I put in your code. With all 3 options unchecked within the WordPress admin, it still renders this error:

    Invalid argument supplied for foreach() in ….single-product.php on line 27

    Tried to check Option 1 and Option 3 in the backend again (so we can use the same consistant conditions to test this) and I still get the above error. Here is the complete code of my single-product.php file in case there is something strange in the code surrounding this:

    https://codepad.org/cBlzfcbC (I put it in Codepad to save people’s screens who scroll in this topic)

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Haha… well, thanks for the vote of confidence.

    Aha… you’ve uncovered a bug (*hangs head in shame*). When no options are checked, the output should be an empty array… instead it’s outputting an empty STRING, so you get that error about “Invalid Argument”. I also goofed in my code above (sorry)… the $my_opts should have been $opts_arr — see the code below for a fix.

    So, I’m off to file a bug report, and meanwhile, you can add a check to your loop — in my example, my options were named “Man”, “Bear”, “Pig”:

    <strong> Option Array: </strong>
    <?php
    $my_arr = get_custom_field('othermulti');
    
    $opts = array(
        'Man' => '<div>Man description</div>',
        'Bear' => '<div>Bear description</div>',
        'Pig' => '<div>Pig description</div>',
    );
    
    $opts_arr = array_keys($opts);
    
    if ( is_array($my_arr) ) {
    	foreach($opts_arr as $opt) {
    	   if ( in_array($opt, $my_arr) ) {
    	      print $opts[$opt]; // will print the description for checked items.
    	   }
    	   else {
    	     print $opt . ' was not checked.';
    	   }
    	}
    }
    else {
    	print 'No options checked.';
    }
    ?>
    Thread Starter WebmistressM

    (@webmistressm)

    Thanks. I put this new code into my single-product.php page. I take it that this will serve as a kludge for now until you debug it for the next edition of Custom Content Type Manager.

    Just one thing I might want to reiterate…. the else statement you have is a good way to test to see if the else statement is being used in various combinations of options checked at the backend level in this custom field. However, my end point is to actually have it not list any text at all for options that are not checked. So, it would actually be print ' '; instead of print 'No options checked.';

    In regards to the final way I want checked items to appear, I am planning on putting the $opts (in your case “Bear” “Pig”) as text that will show a cluetip upon hovering. That of course is a jquery thing and not your plugin’s department to work with Tooltips straight out of the box. However, I just want to clarify that point. Inside the tooltip woudl be the second part of the $opts array (<div>Option 1 description</div>) Hopefully the fix for this issue will work for people that wish to encompass this kind of jquery enhancement into their custom content type fields.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    You can just omit the whole “else” part of that statement if you don’t want to print anything, e.g.

    if (is_array($my_arr) ) {
    // do stuff
    }
    // all done!

    I’ve already committed the fix (see https://code.google.com/p/wordpress-custom-content-type-manager/issues/detail?id=121), and that code is still compatible with it — it all hinges on the “is_array” function. No harm done if you double-check whether or not the data is an array, but with the new version (see below), you should be able to omit the “is_array” check entirely.

    You can download the latest dev version of the plugin here:
    https://wpcctm.com/cctm-dev.zip

    The fixes should be included in that zip by now (it updates every hour).

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Oh yeah, one more thing… the larger “feature/fix” here is a more thorough implementation of the multiselects (and other fields) where you can have an option and a value — currently you just specify a option, and that same option *is* the value. Imagine if you had 2 fields for each option, sorta like how you have “option 1” as the label and the value being that div tag. That’s on the agenda, but not yet implemented. For now, the code construct outlined here gets you there… more footwork, but not as streamlined.

    Thread Starter WebmistressM

    (@webmistressm)

    First off, the addition of 2 fields for each option would rock. Might make it easier for developing the code for putting the value part into a tooltip box, maybe even making it easy to show the key/option as a link so people know to hover the mouse over it for that tooltip. ??

    At any rate, I noticed that there was an upgrade to your plugin for me this morning. Looks to be version 0.9.3.3 which is the same version given in the readme of the plugin revision you just linked to in this thread.

    Thread Starter WebmistressM

    (@webmistressm)

    With the else statement removed (so that nothing shows on options unchecked for a certain item), here is what the code looks like now:

    <?php
    $my_arr = get_custom_field('product_options');
    
    $opts = array(
        'Option 1' => '<div>Option 1 description</div>',
        'Option 2' => '<div>Option 2 description</div>',
        'Option 3' => '<div>Option 3 description</div>',
    );
    
    $opts_arr = array_keys($opts);
    
    if ( is_array($my_arr) ) {
    	foreach($opts_arr as $opt) {
    	   if ( in_array($opt, $my_arr) ) {
    	      print '<strong> Option Array: </strong> <br />' . $opt . $opts[$opt]; // will print the description for checked items.
    	                     }
    	                                   }
    }
    ?>

    Note that I also moved the <strong> Option Array: </strong> part into the if statement, as it makes no sense for that to show up on product entries where none of the 3 options will show.

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    Looks good.

    Don’t trust the readme.txt… I hate the WP repo and how it pushes the “stable version”. Just know that the link there on wpcctm.com is sync’d every hour to the current dev version, regardless of what the readme.txt says.

    Thread Starter WebmistressM

    (@webmistressm)

    Alright. Im going to see if I can install your https://wpcctm.com/cctm-dev.zip without errors this time. I take it the version of the plugin on www.ads-software.com (extensions) is not the current one?

    Thread Starter WebmistressM

    (@webmistressm)

    Okay. Got the zip installed. Very very happy. Do I need to make any change to my single-product.php since the is_array check is now redundant with your newest dev version?

    Incidentally, how soon til this dev version gets committed to everyone’s update queue (i.e. shows up as CCTM needing an update)?

    PS: Is there any form of a cluetip/tooltip (and any cluetip based WordPress plugins) that you DO NOT recommend to use with outputting my <div> content into cluetips?

    Plugin Contributor fireproofsocks

    (@fireproofsocks)

    The version on www.ads-software.com is the latest “stable” version: 0.9.3.3. The stuff in development is the dev version, available via SVN or via that zip file I linked to. When will 0.9.4 be released? Dunno… still need to implement several new features. Yes, the is_array check is redundant, but no, you don’t need to change your code any — it’s perfectly fine to double-check your datatypes. Really up to you.. I constantly check my datatypes and values because you never know when data gets polluted, especially if it’s supplied by the user.

    Re a cluetip plugin, I can’t recommend for or against any. I really think the best solution is what we’ve outlined here. Frankly, I’d be shocked if there were a plugin that would do all this for you… and if amazingly there were such a plugin, I wouldn’t trust it because who knows what it’s really doing. Much better in my opinion to roll your sleeves up and handle this stuff exactly the way you need it handled — this is such a customized use-case that I can’t conceive of reliably handling it any other way.

    Thread Starter WebmistressM

    (@webmistressm)

    No problem. Either way, the few cluetip type WordPress plugins that I did try (before I got the Options situation sussed today — thanks for all your help), did not seem to do much for picking up on anything other than simple text. This is to say, it could provide a cluetip for every instance of “checkbox”, but did nothing if it was “<div>checkbox</div>”. So, yeah, time to roll up my sleeves and see if I cant tweak cluetip downloaded from the main Jquery site.

Viewing 15 replies - 1 through 15 (of 22 total)
  • The topic ‘If/Else Statement for Custom Checkbox’ is closed to new replies.