Jump to content

Wordpress Kill Array for unselected checkboxes ?


ThunderVike

Recommended Posts

This function in WP for a classified ad theme needs to "empty out" a checkbox array that has already been saved with values IF this POST form is modified so that the checkboxes are now completely unchecked.

 

I have a hard-coded series of actions that WORK....in this editing mode if various checkbox forms come back with values (that have been imploded with commas when saved) the following lines detect that these three specific checkbox forms are NOW UNCHECKED (if that is true) when the ad submit button is clicked to UPDATE the ad.

 

 


    // 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);
        }

 

All well and good, except that I need to make the above code dynamic, responding to situations where the checkbox forms may have different names and instead of 3 checkboxes a variable number of checkboxes associated with this post.

 

In the following code I have made some of the code dynamic so that it loops, finds all the $meta_key values that reflect the name of the individual checkbox forms...such as "cp_checkbox_charley"...etc....etc.

 

 

And in the following code if the checkboxes are re-checked for different values the ad / post DOES store the new checkbox values in a comma delimited array.  That part is successful.

 

However, if the checkboxes are UNSELECTED now before saving, the $meta_value for each checkbox form  "checkbox_charley, checkbox_help, checkbox_hello" (in this situation) is not overwritten , not updated. The form returns the previously saved array instead of an empty ' '  array.

 

So the LOOP is not getting included that causes the

$meta_value = ' ';

 

I am hoping someone can spot what is wrong. 

 

The if (isset('cp_checkbox_name'  is supposed to ELSE if that isset check is false and the checkbox is NOT set because all checkboxes in that checkbox form name have been unselected.

 

Most ways I have tried to rewrite this give me an error on the "else".  The following does not error and allows updating the checkboxes accurately. The only condition that does not work is the NOT SET condition that is supposed to assign that checkbox with an empty value.

 

    // update the ad and return the ad id
    $post_id = wp_update_post($update_ad);
$substitute = array(0);
if($post_id) {



foreach($_POST as $meta_key => $meta_value) { 
if(cp_str_starts_with($meta_key, 'cp_checkbox'))
if (! isset($_POST[$meta_key])) {
echo  $_POST[$meta_key] = 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')) && (isset($_POST[$meta_key]))) {
  $meta_value= implode(',', $_POST[$meta_key]); }

if ((cp_str_starts_with($meta_key, 'cp_checkbox')) && (! isset($_POST[$meta_key]))) { 
$meta_value= '';	return $meta_value;	
}
}
        update_post_meta($post_id, $meta_key, $meta_value);
        }

        $errmsg = '<div class="box-yellow"><b>' . __('Your ad has been successfully updated.','cp') . '</b> <a href="' . CP_DASHBOARD_URL . '">' . __('Return to my dashboard','cp') . '</a></div>';

    } else {
        // the ad wasn't updated so throw an error
        $errmsg = '<div class="box-red"><b>' . __('There was an error trying to update your ad.','cp') . '</b></div>';

    }

    return $errmsg;

}

 

 

 

Link to comment
Share on other sites

Let me try this again

 

The following is a section of code in WordPress in a form that updates the database...the problem I am having is that it does not update different checkbox arrays, it does not wipe out the previous stored array IF those checkboxes are all unchecked.

 

I have some hardcode that does it perfectly...but I have to make this function dynamic so that it will create the same loop once it knows what checkboxes are used in the form and it produces the names of those checkboxes stored as $meta_key

 

Here is the dynamic loop that I created that will update the checkboxes and other custom fields just fine...whatever changes are made they are updated when the form is submitted...EXCEPT that if these checkboxes are COMPLETELY unselected...then I keep getting the previously saved array.....

 

 

 

   // update the ad and return the ad id
    $post_id = wp_update_post($update_ad);
$substitute = array(0);
if($post_id) {


foreach($_POST as $meta_key => $meta_value) { 
if(cp_str_starts_with($meta_key, 'cp_checkbox'))
if (! isset($_POST[$meta_key])) {
echo  $_POST[$meta_key] = 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')) {   
if(!isset($_POST[$meta_key])) {
$meta_value= ''; }
else {
$meta_value= implode(',', $_POST[$meta_key]); }
	 }
}

 

! isset  or isset is used to determine if a checkbox such as "cp_checkbox_charley" is unchecked or checked with some values being passed into the new array held in cp_checkbox_charley[]

 

The hardcode that WORKS does this simply  but in the ten-thousand different ways I have re-written and tested the code ABOVE (trying to make it Replace the code below) I cannot get the

(!isset($_POST[$meta_key])) {
$meta_value= '';

 

to do the same thing as the ELSE below where if the particular checkbox ISSET then it implodes the values....ELSE it sets the $meta_value for this iteration as NOTHING....

 

So, looking at the code BELOW can you tell me what I should change in the code ABOVE to make it loop through and perform the same comparison of "not set" and ""?

 

Trying to duplicate the hardcode with the else does not work...either PHP objects strenuously with Errors or nothing happens when all checkboxes are unselected....by "nothing" I mean, I get back the same array that was last saved when it had one value or more.

 

 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);
        }

 

Thank you very much for shedding light in darkness.

 

I am on PHP 5.3.

 

 

Link to comment
Share on other sites

I have been trying all things trying to find the logic of if,foreach, while, case... for a nested conditional...

 

The shared server environment most basically;

 

PHP Version 5.2.14

mysql  5.0.67

magic_quotes_gpc 

 

I apparently successfully converted the following hardcode in a WordPress functions file for an online form that returns previously entered form values....text, textarea, dropdown, and checkbox array.  The following lines treat the checkbox values differently because they get put into an array according to the $META_KEY name such as 'cp_checkbox_charley' or 'cp_checkbox_help'.....any number of different names may come up.

 

 

1-- In short, wherever the $meta_key that defines a string of values contains "CP_CHECKBOX ......." THOSE VALUES WILL ALREADY BE IN AN ARRAY --- AN ARRAY OF VALUES REPRESENTING EVERY SEPARATE CHECK BOX THAT WAS SELECTED FOR THAT VALUE. THE ARRAY IS HELD IN THIS [] and 'cp_checkbox_charley[]‘ can hold as many values as are default values for that particular checkbox.

2-- And, additionally, when those checkboxes show up in a string the individual values must be IMPLODED with a comma between.

3-- And, last condition, if the isset condition for a posted checkbox is FALSE--(! isset($_POST[$meta_key])) then that can ONLY MEAN that the checkboxes have this time around been completely unchecked.  According to the formula then, the $META_VALUE can only equal an empty array...represented by ''.  When the tables are now updated-- even if 1, 2, 3...or more values are sitting in the 'meta'value' field from the previous saved version, i.e. "Sunday, Tuesday, Thursday, Friday" and comma limited--they must be replaced with nothing, no value...not even a "Null".

 

This code below works -- it first makes sure that the checkboxes Exist and are NOT set...nothing selected this time....but that in expectation of changing their pre-existing values when they WERE checked in a previous save we set them specifically ready to save something in an array :

 

    // 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();

 

I converted those lines into this.

 

foreach($_POST as $meta_key => $meta_value) { 
if(cp_str_starts_with($meta_key, 'cp_checkbox'))
if (! isset($_POST[$meta_key])) {
echo  $_POST[$meta_key] = array();

}

 

 

The following little code goes through each posted field looking for the individual strings of $meta_key associated with $meta_value and for each of them where the string starts with 'cp_checkbox' then it loops through what is left....so it returns the same names just one time of each checkbox form that begins with 'cp_checkbox' but also writes in the complete name for each $meta_key.

 

foreach($_POST as $meta_key => $meta_value) { 
if(cp_str_starts_with($meta_key, 'cp_checkbox'))

 

And here is the section that has consumed many hours because I cannot figure out how to write a loop that does the same thing....again, the code below works perfectly...but it depends on knowing the checkboxes ahead of time.

 

I need to output a DYNAMIC LOOP using the values already detected---the checkbox names contained in $meta_keys where they begin with 'cp_checkbox'.

 

 

  

//  this code works perfectly and OVERWRITES the UNCHECKED checkboxes

// 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);
        }

 

I tried the FOLLOWING...among many radically different experiments....It does not error, and saves changes to the 'cp_' custom fields, and saves the changes to the 'cp_checkbox...' fields. So, up to that point it is looping and identifying that I am using 3 checkbox forms and what the names or $meta_key values are and IMPLODING the values with commas.

 

 

  // now update all the custom fields
    foreach($_POST as $meta_key => $meta_value) {
if (cp_str_starts_with($meta_key, 'cp_' && !'cp_checkbox'))  {
  	if (cp_str_starts_with($meta_key, 'cp_checkbox')) {   
if(! isset($_POST[$meta_key])) {
$meta_value= "''"; }
  {
$meta_value= implode(',', $_POST[$meta_key]); }
	 }
}
	update_post_meta($post_id, $meta_key, $meta_value);
	}

 

 

What THE CODE ABOVE does NOT do is overwrite the unselected checkboxes....if I completely UNCHECK every checkbox in a checkbox form such as 'cp_checkbox_charley', for example, it does not replace the previous values with nothing, no value.  The same previously saved values return with the same checked values as before.

 

I cannot get this loop to look for the checkboxes that are ! isset during posting and replace the $meta_value with ''.

 

I would appreciate the ANSWER very much!

 

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.