ThunderVike Posted August 16, 2010 Share Posted August 16, 2010 wildteen88 and DavidAM have been of enormous help so far and I must again extend my grateful thanks. Now I have discovered the rest of the hard-code in the same Wordpress example that I must replace with a dynamic looping equivalent. This function takes custom fields that have already been entered into a form and passes them along to another page so that the user can revise or modify the original values in the form that the user filled out. The complication in this has been that it used checkboxes whose final arrays of values have to be "exposed" and imploded with commas before being saved to the database. Then when retrieved just those checkbox fields with comma delimited values have to be exploded and put into lists separate from other custom fields which only store a simple string. The other factor is that the editing form must detect if duplicated checkboxes in the Edit form have been completely UNselected this time before saving. If this time checkboxes are completely unselected then on UPDATE the old array must be emptied out when saved to the database this time. Here is the version that WORKS...with Hardcode for 3 checkboxes with known names. But, I want to convert any code that refers to 'cp_checkbox.....' with a dynamic loop that does the same thing. And it will then depend on knowing which checkboxes are being called and returned to this form for editing. This function is called on the tpl-edit-item.page as // update the ad content $the_msg .= cp_update_listing(); and now, without further ado, ladies and gentlemen, the function in question !! // // saves the ad on the tpl-edit-item.php page template function cp_update_listing() { global $wpdb; // check to see if html is allowed if (get_option('cp_allow_html') != 'yes') $post_content = cp_filter($_POST['post_content']); else $post_content = $_POST['post_content']; // keep only numeric, commas or decimal values if (!empty($_POST['cp_price'])) $_POST['cp_price'] = cp_clean_price($_POST['cp_price']); // keep only values and insert/strip commas if needed and put into an array if (!empty($_POST['tags_input'])) $_POST['tags_input'] = cp_clean_tags($_POST['tags_input']); $new_tags = explode(',', $_POST['tags_input']); // put all the ad elements into an array // these are the minimum required fields for WP (except tags) $update_ad = array(); $update_ad['ID'] = trim($_POST['ad_id']); $update_ad['post_title'] = cp_filter($_POST['post_title']); $update_ad['post_content'] = trim($post_content); $update_ad['tags_input'] = $new_tags; // array //$update_ad['post_category'] = array((int)cp_filter($_POST['cat'])); // maybe use later if we decide to let users change categories //print_r($update_ad).' <- new ad array<br>'; // for debugging // update the ad and return the ad id $post_id = wp_update_post($update_ad); $substitute = array(0); if($post_id) { // Make sure the checkbox arrays exist // these lines must be replaced with dynamic equivalent !!! 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(); // here is a function I wrote that seems to create the same thing as above // it returns the three checkbox names when I do a print command // cp_checkbox_charley // cp_checkbox_help // cp_checkbox_hello // I would like someone to comment on this if it makes sense in a strict PHP sense of logic // foreach($_POST as $meta_key=> $meta_value) { // if (cp_str_starts_with($meta_key, 'cp_checkbox')) { // if (! isset($_POST[$meta_key])) $_POST[$meta_key] = array(); // } // now comes the next hard code that I must duplicate in a loop // I thought I would build on what I had done above // and make it loop through for each $meta_key that my condition above creates // and re-create the same essential statements and actions // however, different ways I have tried this don't seem to loop the $meta_key= '' if one of those checkboxes // in completely unselected--after update the checkboxes still retain their original values // so instead of showing you my code that fails to update unchecked checkboxes I will leave the WORKING FUNCTION // that I must recreate in a dynamic loop version where the code will not hardcode the cp_checkbox names // 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); } $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; } So, again the code I need to make dynamic so that if I use 5 or 6 checkboxes with different names and values or just 1 or 2...the code will loop and handle it....... // / / 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); } [ THANK YOU VERY MUCH if you can HELP. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.