WordPress.org

Ready to get started?Download WordPress

Forums

Custom Content Type Manager
If/Else Statement for Custom Checkbox (23 posts)

  1. WebmistressM
    Member
    Posted 3 years ago #

    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:

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

  2. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  3. WebmistressM
    Member
    Posted 3 years ago #

    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: http://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: http://leopardspot.endofinternet.net:81/ImageServer/nooptionschecked.jpg

  4. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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.';
       }
    } ?>
  5. WebmistressM
    Member
    Posted 3 years ago #

    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. :-D

    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:

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

  6. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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.';
    }
    ?>
  7. WebmistressM
    Member
    Posted 3 years ago #

    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.

  8. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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 http://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:
    http://wpcctm.com/cctm-dev.zip

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

  9. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  10. WebmistressM
    Member
    Posted 3 years ago #

    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.

  11. WebmistressM
    Member
    Posted 3 years ago #

    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.

  12. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  13. WebmistressM
    Member
    Posted 3 years ago #

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

  14. WebmistressM
    Member
    Posted 3 years ago #

    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?

  15. fireproofsocks
    Member
    Plugin Author

    Posted 3 years ago #

    The version on WordPress.org 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.

  16. WebmistressM
    Member
    Posted 3 years ago #

    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.

  17. WebmistressM
    Member
    Posted 2 years ago #

    Now I just gotta update my code for arrays, since CCTM nwo does em on checkbox type content fields.

    Current code I need to modify is:
    
    <strong>Available Options:</strong>
    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
    	print '<li>';
    	print $item;
    	print '</li>';
    }
    ?>
  18. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    em? Not sure what you mean. Have you used the output filters for this? Specifically the "formatted_list" filter:
    http://code.google.com/p/wordpress-custom-content-type-manager/wiki/formatted_list_OutputFilter

    <?php
    print_custom_field('product_options:formatted_list', array('<li>[+value+]</li>','<ul>[+content+]</ul>') );
    ?>

    You can always iterate over the array manually, but the Output Filters were designed to make printing your data as simple as possible.

  19. WebmistressM
    Member
    Posted 2 years ago #

    Well, I need each option in this array to have a corresponding tooltip. None of the "tooltip" plugins let you have much control, so I imagine Ill have to load the cluetip jquery library and call it. Its just Im not sure how to have the cluetip scan for certain values so it can display the correct corresponding tool tip.

  20. fireproofsocks
    Member
    Plugin Author

    Posted 2 years ago #

    Yep, you'll have to load up the tooltip JS/CSS in your template -- some of those plugins require that you set a title attribute for the link.

    You're using a multi-select field, right? Not a checkbox field ... otherwise you wouldn't be returning an array. So you could either define a global array of tooltips (e.g. at the top of your template file) such as

    $tooltips = array(); // initialize
    $tooltips['some_value'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';

    Then use each value returned from the array to get your tooltip, e.g.

    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
        $tooltip = $tooltips[$item];
        print "<li><span title="$tooltip">$item</span></li>";
    }
    ?>

    OR you could play around with the "use distinct options/values" option when you define your multi-select field, then iterate over the array using full key/value pairs, e.g.

    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $k => $v) {
        print "<li><span title="$k">$v</span></li>";
    }
    ?>
  21. WebmistressM
    Member
    Posted 2 years ago #

    $tooltips = array(); // initialize
    $tooltips['some_value'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';

    http://craigsworks.com/projects/qtip2/demos/simple?source
    Here is the jquery library Im looking towards. It uses $('a[title]').qtip(); so I guess the above statement would be slightly different.

  22. WebmistressM
    Member
    Posted 2 years ago #

    Ack. As of this morning, qtip2's site is returning Internal Server Error. Sorry to send you to a currently-not-working link.

    (for anyone else following the thread) Just gotta wait til this page is back up and see if it is the solution.

    http://craigsworks.com/projects/qtip2/tutorials/advanced/#wordpress

    Will post info once I can try it out (once the site is accessible)

  23. WebmistressM
    Member
    Posted 2 years ago #

    Okay, here is what I have so far. Things still arent working but..

    In functions.php of my theme:

    if(!is_admin()){
       wp_deregister_script('jquery');
       wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"), false, '');
    }

    In header.php of my theme (just before wp_head):

    <script type="text/javascript"
       src="<?php bloginfo("template_url"); ?>/js/jquery.qtip.min.js"></script>

    And on single-product.php:

    <script type="text/javascript">
    $tooltips = array(); // initialize
    $tooltips['No Options'] = 'This means something...';
    $tooltips['other_value'] = 'Note this here';
    </script>
    <strong>Available Options:</strong>
    <?php $my_array = get_custom_field('product_options');
    foreach ($my_array as $item) {
        $tooltip = $tooltips[$item];
    	print '<li> <span title="$tooltip">';
    	print $item;
    	print '</span> </li>';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic