Jump to content

Checkbox Redux...making things more Dynamic


ThunderVike

Recommended Posts

I got some great help from DavidAM a few days ago that gave me a working function for updating checkboxes when unselected during an editing process later. I was looking for a way to overwrite the old array of values if ALL of the checkboxes were unchecked.

 

The SOLUTION was done with some hard-coding of checkboxes names.

 

I have written a function to find out which checkbox names are going to be called for this form (it can be different multi-value checkboxes loaded depending on the Category of Post the user is trying to make).

 

Now I would like to apply whatever values that come up in a foreach loop after querying the WordPress database to the following function so that instead of hard-coding these three separate multi-value checkboxes I can generate the dynamic code that takes the place of the following:

 

Notice that it needs to know the names of the checkboxes first so I need a looping foreach statement that will takes the results of the query before this that returns an array such as example: (cp_checkbox_help,cp_checkbox_new,cp_checkbox_old,cp_checkbox_stuff)  What the query returns in names and the quantity count of these names will vary according to the category id that is passed when the user decides to post into a particular category.

 

if($post_id) {

    // Make sure the checkbox arrays exist 
    if (! isset($_POST['cp_checkbox_charley'])) $_POST['cp_checkbox_charley'] = array();
    if (! isset($_POST['cp_checkbox_help'])) $_POST['cp_checkbox_help'] = array();
    if (! isset($_POST['cp_checkbox_hello'])) $_POST['cp_checkbox_hello'] = array();

    // now update all the custom fields
    foreach($_POST as $meta_key => $meta_value) {
if (cp_str_starts_with($meta_key, 'cp_')) {
        if (cp_str_starts_with($meta_key, 'cp_checkbox_charley')) {
            if (isset($_POST['cp_checkbox_charley']))
                $meta_value= implode(',', $_POST['cp_checkbox_charley']);
            else
                $meta_value = '';
        }
        if (cp_str_starts_with($meta_key, 'cp_checkbox_help')) {
            if (isset($_POST['cp_checkbox_help']))
                $meta_value = implode(',', $_POST['cp_checkbox_help']);
            else
                $meta_value = '';
        }
        if (cp_str_starts_with($meta_key, 'cp_checkbox_hello'))
            if (isset($_POST['cp_checkbox_hello']))
                $meta_value= implode(',', $_POST['cp_checkbox_hello']);
            else
                $meta_value = '';
        }

        update_post_meta($post_id, $meta_key, $meta_value);
        }

 

from a query (I have now built) at this point in the process the script will have returned whatever checkbox names such as cp_checkbox_hello, cp_checkbox_hello, cp_checkbox_charley are relevant to the specific form that is attached to the online Post category a user is trying to Post to.

 

Just what checkbox names are the result of this query will vary according to the Category Id assigned to this Post form.  A different category id may bring a different post form to fill out with different multi-value checkboxes names.

 

Which is why this has to be dynamic to take 2 or 3 or 4 or whatever number of names return for checkbox names and then create the following code to handle a variable number of checkboxes that must have the name values filled in.

 

I hope I have explained this clearly enough-- that I want to make the code example dynamic-- right now it takes care of 3 specifically named checkboxes--- I want to get rid of the hardcode and write all of that dynamically, looping through to take care of every situation and every checkbox that may show up.

 

Thank you for taking the time to consider this thoughtfully!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.