Jump to content

ThunderVike

Members
  • Posts

    58
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

ThunderVike's Achievements

Member

Member (2/5)

0

Reputation

  1. I am using the Classipress Classified Ads theme which must disable various WP functions and rewrite them to work at all. Classipress uses the wp_term_taxonomy table to assign ALL categories in the "taxonomy" field the value "ad_cat". So, all queries are by default looking for Categories and sub-categories of posts that are marked by "ad_cat" in this taxonomy. So post category id 142 might be one child of category id 12, etc. etc. But all posts are grouped by the common distinction of "ad_cat" so that they can be separated from pages and blogs. A series of posts are then listed as ADS belonging to such groups as Parent Category... Rentals....sub-category ... Apartments ( with, in my case just two levels...Parent...child of) But I cannot find a single wordpress function or query which in or outside of the loop of posts looks at the ad/post category id number and then goes UP the hierarchy to retrieve the PARENT Category which the child belongs to and then display the "nice name" such as "Rentals". It seems that there ought to be a query that keeps track of the parent category of every child category with that information ready to be displayed in every single ad every single time no matter where it is called from or how the ad is sorted. I created a function which shows the Parent category name extracted from the breadcrumb script but it only returns this information on pages related to the parent category to start with. Is there a fool-proof "always on" query which displays the Parent category of each post whether as a post summary in a list of posts or on a single post page as easily as it already displays the post title, sub category name, and all post particulars? So an Ad for "Apartment Rentals" as the sub-category or for "House Rentals" as the sub-category, whether shown in a list of Ad summary boxes or on a single page by itself, always has the Parent category name displayed with the Title and other information? So that, for instance, a search for NEWEST ADS might show the following....(right now it just shows the "Sub category" name) Breadcrumbs relies on current page values for categories.... I need something INDEPENDENT of the page values where the ads might be displayed ---------------------------------------- New Apartment in Belle Haven "Rentals" >> "Apartment Rentals" ---------------------------------------- ---------------------------------------- Customized Chevrolet Bel Air "Cars" >> "Customizations" ---------------------------------------- Thank you very much for your help!
  2. Hello David! At LAST, at last! I grabbed a function I had already created and modified it a little to give me all the checkbox names associated with the Post ID. There may be 1 to any number of separate checkbox forms that I create and save for later use...one checkbox form may hold 6 values to be used to display checkboxes for selecting property amenities. Another checkbox form may hold 7 values to list the days of the week, another checkbox may hold values to display a checkbox form for necessary requirements of a renter, etc. So one category of Ad may have different checkboxes assigned to the Ad form for that particular category. I have so many categories that I need more than six or seven checkbox forms available to assign to the different category forms. That is why I needed a dynamic way to automatically generate lines of conditional code that would vary depending on what Ad category this Edit Post/Ad page was displaying. The key to knowing WHICH checkbox names to return was the Post ID. So I modified another function that I had created that relied on the Post ID being returned for each ad that is called for in the edit page php. So, here is the ENTIRE working code.....and I thank you very much for helping me so much with this. It now creates what I needed and allows me to dispense with the hardcode, which was going to be impractical for who knows how many checkbox forms with different names and different values in the future. There is only one place in all this working code that looks awkward in terms of number of CPU load, or efficiency....where I call for the three different conditional IFS just before updating the Post id, meta_key, and meta_value in the database. I had to create TWO update actions to get it done. The second update uses the $$variable you showed me. And the whole thing works, but looks inefficient.... at any rate, David, in case you are interested in what I finally did with your generous help, here is the whole WORKING code that updates unselected checkboxes as well as newly selected checkboxes! I comment some of the lines with Questions to you about efficiency or tidying up..... // saves the ad on the tpl-edit-item.php page template after user makes any changes in custom field values 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 // update the ad and return the ad id $post_id = wp_update_post($update_ad); $substitute = array(0); if($post_id) { // I got tired here and simply made a copy of the existing post ID value that my modified function dh_return_checkboxnames // could read as-is // instead of just changing the way I had already written the post Id $postid=$post_id; // I forget whether it was you or wildteen88 who came up with this following code // the code I wanted to generate automatically according to each Ad's requirements: // 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 my function to return ALL the relevant CHECKBOX names needed to generate the code above // with your contribution modified by $option substitution function dh_return_checkboxnames($postid) { global $wpdb; // give us the complete checkbox field names held in the $meta_key values -- for just this Ad $sql = $wpdb->prepare("SELECT `wp_cp_ad_fields` . `field_label` , `wp_cp_ad_fields` . `field_name` ,`wp_postmeta` . `post_id` , `wp_postmeta` . `meta_key` , `wp_postmeta` . `meta_value` FROM wp_cp_ad_fields , wp_postmeta WHERE `wp_cp_ad_fields` . `field_name` = `wp_postmeta` . `meta_key` AND `wp_postmeta` . `meta_key` LIKE '%%checkbox%%' AND `wp_postmeta` . `post_id` = ($postid) ORDER by `field_label` ASC "); $results = $wpdb->get_results($sql); if($results) { foreach ($results as $result) : if(!empty($result->meta_key)) {$options = explode(',', $result->meta_key); foreach ($options as $option) { if (!isset($_POST[$option])) $_POST[$option] = array(); } }endforeach; } } dh_return_checkboxnames($postid); // now update all the custom fields foreach($_POST as $meta_key => $meta_value) { // if changed the next line to-- if (cp_str_starts_with($meta_key, 'cp_ && ! 'cp_checkbox')) // would that keep the first IF from also processing all the cp_checkbox strings, since cp_ does not exclude cp_checkbox ? 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]); } update_post_meta($post_id, $meta_key, $meta_value); } } // here is where I do this again for foreach($_POST as $meta_key => $meta_value) { if ((cp_str_starts_with($meta_key, 'cp_checkbox')) && (! isset($_POST[$meta_key]))){ $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; } Thank you so much for your help, DavidAM! If you see areas for improvement of this code above I would appreciate any comments...
  3. I have discovered that I am trying to do something like This: function checkbox() { 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();'; } } } checkbox(); However, I need to change this: foreach($_POST as $meta_key => $meta_value) I need it to give me not the $Post values, because that filters out the very checkboxes which are unselected....since they are NOT posted at the time of the Submission of all these custom fields. I need to get the returned NAMES of the checkboxes, ALL of them, checked or unchecked, Before the user decides to do something.... NOT AFTER . So, whether or not the user is going to wind up selecting or unselecting the returned checkboxes stored in the database and retrieved from this form....I need a PRE save...that gets all three or four...or whatever checkbox names from the $meta_keys.
  4. Jcbones, thank you so much for taking the time to respond and give me a response. I tried your code and it did not work, and I suspected it would not ahead of time. Here is the "problem"...and I have tried to say this over and over... No, I do NOT WANT TO CHECK ANYTHING FOR WHAT IS WRITTEN....THE CODE IS ALREADY PRECISELY ESTABLISHED THAT I NEED. I cannot more explicitly say this .... I AM NOT CHECKING for any conditions whatsoever WHILE THIS FUNCTION IS LOOPING. THE ONLY THING THIS FUNCTION IS FOR IS TO GENERATE THE NEEDED CODE WITH THE $META_KEY INSERTING THE NAME OF EACH CHECKBOX TWICE PER LINE. The code that WORKS EVERY SINGLE TIME FLAWLESSLY I have posted so many times in this forum over the past weeks. You are the only kind person to have responded, and I thank you! This is frustrating to me because my English is pretty clear. THIS DOES NOT REQUIRE THINKING OUTSIDE THE BOX... I HAVE ALREADY SUPPLIED THE PRECISE CODE...IT ALREADY WORKS AS A HARDCODE I JUST NEED THE EXACT LINES GENERATED AUTOMATICALLY since a simple loop for each $meta_key gives me the cp_checkbox names I need to substitute into the page code. if (! isset($_POST['cp_checkbox_ONE'])) $_POST['cp_checkbox_ONE'] = array(); if (! isset($_POST[$meta_key])) $_POST[$meta_key] = array(); But something is NOT happening correctly when I try this dynamically...whatever is outputted is not working because unselected checkboxes are not wiped out on the save....the old array values return. When I reinstall the hard code I keep including here for reference it works perfectly all the time. Unselected checkboxes, where every single checkbox is unselected, return unselected....just what I wanted. The HARD CODE sits there ready to be checked against the posted field strings, when a string starts with the Meta Key 'cp_checkbox', and THAT PARTICULAR CHECKBOX IS UNSELECTED THEN IT IS and MUST BE EQUAL TO AN ARRAY. THIS WILL NEVER WORK for this page...it has been tried and failed....: if (!is_array($_POST[$meta_key])) The hard code PRESUPPOSES THE EXACT SITUATION AHEAD OF TIME, THE CODE ALREADY KNOWS WHAT IS BEING LOOKED FOR AND WHAT IS NEEDED FOR THIS PARTICULAR SITUATION AND NOTHING ELSE WORKS!! NO CHECKING FOR ARRAYS while this function "decides" anything ...IT DOES NOT WORK THAT WAY...PERIOD. It absolutely has to be....no exceptions, no other supposItions....what has to be AVAILABLE BEFORE THE DATABASE IS UPDATED WITH ANY CHANGED CUSTOM FIELD VALUES IS THIS---VERBATIM, CHARACTER FOR CHARACTER... THIS CODE HAS TO BE AVAILABLE TO FOR COMPARISON....COMPARISON.... IF THE CUSTOM FIELDS WITH 'CP_CHECKBOX_ONE' OR 'CP_CHECKBOX_TWO' OR 'CP_CHECKBOX_THREE' ARE UNSELECTED....if (! isset($_POST['cp_checkbox_ONE'])) THEN THE FUNCTION IS TOLD THAT $_POST['cp_checkbox_ONE'] = array(); IS GOING TO BE AN ARRAY This has to exist when the checkbox is UNSELECTED because I have another function that says that IN THIS CASE $meta_value = '' Because if the checkbox is unselected before the edit form is saved again then we STILL have to return an array with '' -- we have to empty out that previous checkbox array....if the previous returned checkbox array has Sunday,Monday,Tuesday,Wednesday then THAT array has to be overwritten by ' '....nothing, erased in the database. So, again, I don't want to check for anything as a condition for running this loop. I am trying like crazy to simply DYNAMICALLY WRITE these lines.... // Make sure the checkbox arrays exist if (! isset($_POST['cp_checkbox_ONE'])) $_POST['cp_checkbox_ONE'] = array(); if (! isset($_POST['cp_checkbox_TWO'])) $_POST['cp_checkbox_TWO'] = array(); if (! isset($_POST['cp_checkbox_THREE'])) $_POST['cp_checkbox_THREE'] = array(); So, again, I must find a way to make some variation of this function work..... function checkboxloop() { //function start. foreach($_POST as $meta_key => $meta_value) { //loop the $_POST key/values. if(cp_str_starts_with($meta_key, 'cp_checkbox')) { //if the POST key starts with cp_checkbox. if (! isset($_POST[$meta_key])) $_POST[$meta_key] = array(); //if POST[key] is not set <<This condition will always revert to false, because you are checking to see if a set condition is NOT set. AND THIS IS EXACTLY WHAT I NEED----THIS CODE MUST SIT HERE READY TO CHECK IF THE POST KEY IS NOT SET ! } } } The only problem is that something about it is not working.....in practice....
  5. if (! isset($_POST['cp_checkbox_three'])) this is what gives me problems.... I just want it to be WRITTEN, NOT PROCESSED for the if (! isset($_POST['cp_checkbox_three'])) WHILE the function is substituting the $meta_key return in the loop I posted as my "solution" if I simply put an echo $meta_key . '<br>'; in that loop it will give me back the values I need, like this... cp_checkbox_one cp_checkbox_two cp_checkbox_three So I want to substitute the $meta_key iteration into if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); by doing if (! isset($_POST[$meta_key])) $_POST['$meta_key] = array(); and it keeps going, line after line, but the "IF" in if (! isset($_POST is not a LIVE condition at the time of the looping action, it is the thing that causes errors in the different ways that I try to create a dynamic write.....
  6. Sorry for the confusion....if I post the whole code then no-one answers. So, I tried to break it down to the simplest terms. As I said; I have three lines in existing code PHP code -- here they are again-- if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); if (! isset($_POST['cp_checkbox_two'])) $_POST['cp_checkbox_two'] = array(); if (! isset($_POST['cp_checkbox_three'])) $_POST['cp_checkbox_three'] = array(); These exist inside a function that updates a form and allows a user to look at the form just filled out and saved to the database. When the user looks at this form they see the values they previously filled out and have a chance at this point in the process to CHANGE the values in the form fields. Most of these are Custom fields stored in a Wordpress table. The PROBLEM has been with the CHECKBOX forms. One checkbox form may have values in common, such as Property amenities of a real estate listing, as an example. Another checkbox form may store values for days of the week. [] stores the arrays from Checkboxes....and another function IMPLODES the values selected and puts a comma between them and saves them back in the appropriate checkbox arrays with commas separating the values. So, here we go: If I know ahead of time WHICH checkbox forms are going to be used then I can write a necessary line of code for each checkbox form that says, essentially, "If this condition is present then the checkbox named 'cp_checkbox_one'[] will be an array" ...and the same for the other named checkboxes I have referred to. Again, this is the code...hard code in PHP--- I wrote it and there it sits ready to help out if referenced later. if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); if (! isset($_POST['cp_checkbox_two'])) $_POST['cp_checkbox_two'] = array(); if (! isset($_POST['cp_checkbox_three'])) $_POST['cp_checkbox_three'] = array(); So, all well and good. -But I have to REPLACE those lines with something dynamic.... so that when the function checks for any strings that contain a 'cp_checkbox' it will dynamically write the lines above so that if this form returns in the future 'cp_checkbox_three' and 'cp_checkbox_six' (you get the idea) then it will write if (! isset($_POST['cp_checkbox_three'])) $_POST['cp_checkbox_three'] = array(); if (! isset($_POST['cp_checkbox_six'])) $_POST['cp_checkbox_six'] = array(); When I tried different ways of making PHP write these kinds of lines EXACTLY as my examples based on the Form names returned I can see the names come back 'cp_checkbox...whatever, whatever'....if I test with an echo $meta_key ; But, the IF, the IF is a LIVE condition...I do not know how else to say that.... I have posted the way I was trying to do the same thing.... function checkboxloop() { 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(); } } } // HERE I ASK FOR THE FUNCTION BUT WHILE I GET NO ERRORS I GET NO RESULTS // WHICH IS A PROPER SAVE OF UNCHECKED CHECKBOXES ON SUBMIT checkboxloop;
  7. I am trying to make a loop simply write "static" code...if that is the correct explanation.... But it seems PHP wants to "process" such things as 'IF' and other Conditions which is defeating the purpose. The problem specifically is that I NEED for a dynamic write to happen based on a variable that will put the following lines that start with IF (! isset // Make sure the checkbox arrays exist if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); if (! isset($_POST['cp_checkbox_two'])) $_POST['cp_checkbox_two'] = array(); if (! isset($_POST['cp_checkbox_three'])) $_POST['cp_checkbox_three'] = array(); // here in the lines below is one way I have tried to make the the LINES ABOVE get written Dynamically // THIS PART returns the values I need, the checkbox names ---- // // foreach($_POST as $meta_key => $meta_value) { // if(cp_str_starts_with($meta_key, 'cp_checkbox')) { echo $meta_key; } // } // So that code WILL RETURN THE VALUES I NEED : // cp_checkbox_one, cp_checkbox_two, cp_checkbox_three // BASED ON THIS I WROTE THE FOLLOWING CODE-- BUT IT DOES NOT WORK AS A REPLACEMENT FOR A LINE // SUCH AS--- if (! isset($_POST['cp_checkbox_one'])) $_POST['cp_checkbox_one'] = array(); // function checkboxloop() { 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(); } } } // HERE I ASK FOR THE FUNCTION BUT WHILE I GET NO ERRORS I GET NO RESULTS // WHICH IS A PROPER SAVE OF UNCHECKED CHECKBOXES ON SUBMIT checkboxloop; I would very much appreciate knowing how to make the code keep looping whatever $meta_key names turn up on this page and then use those names to write the complete lines as STATIC PHP so that the IF conditions don't get "processed" while this function is called. In effect, I want to duplicate the effect of having the code lines WRITTEN, so that they perform exactly as if I had written them as PHP hard-code, as many lines as there will be $meta_keys, but not SEE the code--- just have it valid PHP. This function does NOT make the same thing happen internally. As soon as I replace the function with the HARD CODE PHP then it works...but, I have reached the stage where these lines of code have to happen dynamically. I am baffled how to get this to happen! If I use Echo then I get errors about the "IF". Thank you very much for your help!
  8. Well, it has been fun replying to myself. I have tried scads of things including $$variable to loop through variables but nothing works. I am stuck. Nothing I have learned or tried in the past months and a half has worked for my problem.
  9. 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!
  10. 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.
  11. 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; }
  12. colmustard, you say... "interesting, I have seen people using wordpress as a standalone cms for non-blog websites, but I've just never seen the practicality in it as you can accomplish the same effects using another cms or framework that is built specifically for the functionality you are trying to create in a much more time efficient manner." Installing WP is extremely easy and as soon as it is up you can start writing pages and then specify which one is the index page, and then generate post afer post or page after page. Indicate which pages are linked to which pages. Do an entire site by simply selecting that these are Pages and not posts. Indicate which pages are related to each other by categories you make up,...whatever. I just do not see what "practicality" you can be talking about. I have just looked at mod x and ez publish. Despite the name ez publish looks like it is is a long learning curve. They talk about components and cool stuff but not once anywhere on the website do they actually get "user-friendly" the way WordPress does. It is definitely a geek-techno website that is written by engineers. Mod X looks cool and more upfront user-friendly but YOU, colmustard, say you are using mod x AND Wordpress to get the "functionality" you are trying to create "in a more time efficient manner", but, honestly, you are not even using WordPress's everyday functions. Your site is suffering some huge issues so I don't think you have enough of a grasp to even compare "usability" or "functionality" between CMS programs, complex or simple. If you want to talk about "usability"....everything, and I mean everything is EASILY achieved using WordPress.
  13. 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.
  14. Thanks so MUCH! I am reading your post. You should write some Tutorials!! I have read a lot of them but they usually wind up with examples that JUST MISS "my" situation. I am running PHP 5+ It must be a pain trying to write public software with "fall back" options in case the PHP is an earlier version. It was weird watching the IMPLODE happening, seeing it, but not being able to Update it. Thank you for your thoroughness and willingness to wade through all that!
  15. You said "Functions do not have access to the variables that are defined OUTSIDE the function." THAT is an important point. I thought that since I could SEE the other $postvals being stored in the lines above this function that by following sequentially those stored $postvals already stored in the session with my function and naming $postvals AGAIN in my function that I was lining them up for INCLUSION on the update. PASSING the values THROUGH this function was a big concept to learn! THANK YOU AGAIN!
×
×
  • 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.