ThunderVike Posted August 23, 2010 Share Posted August 23, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/ Share on other sites More sharing options...
jcbones Posted August 24, 2010 Share Posted August 24, 2010 So you want code to be written dynamically, from static, that doesn't display, but processes. But then you are having an issue that echo is returning errors on some dynamically created PHP code, that is written from a static point of view. And that is about as confusing as your post is... 1. Do you want the PHP code written to the page? 2. You want $_POSTED data, to be evaluated by the PHP interpreter, processed, then the results echo'd back to the page? IF it is not 1, I suggest you re-think what exactly you want to do. 2 is very dangerous, and can read('will') lead to a compromised server. Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1102944 Share on other sites More sharing options...
ThunderVike Posted August 24, 2010 Author Share Posted August 24, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1102952 Share on other sites More sharing options...
ThunderVike Posted August 24, 2010 Author Share Posted August 24, 2010 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..... Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1102954 Share on other sites More sharing options...
jcbones Posted August 24, 2010 Share Posted August 24, 2010 You are looping through known keys with the foreach(), and when you find a key that starts with the desired criteria, you then check to see if it is NOT set. But, it will be set, because you are looping through those keys. Perhaps you want to check if it is an array. 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. } } } 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 (!is_array($_POST[$meta_key])) $_POST[$meta_key] = array(); //if POST[key] is not an array. } } } Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1102958 Share on other sites More sharing options...
ThunderVike Posted August 24, 2010 Author Share Posted August 24, 2010 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... Perhaps you want to check if it is an array. 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.... Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1103188 Share on other sites More sharing options...
ThunderVike Posted August 24, 2010 Author Share Posted August 24, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1103267 Share on other sites More sharing options...
DavidAM Posted August 24, 2010 Share Posted August 24, 2010 ThunderVike, I think I finally get it. You are trying to collect the code before the form is displayed because the $_POST array will not contain the checkbox name array if they are all unchecked. I'm not sure if you can make this work, since when you display the form the script ends and that code you collected will no longer exist. Maybe you can add a hidden field to the form that contains a comma-separated list of names of the checkboxes you expect to get back: <INPUT type="hidden" name="cp_checkbox_names" value="cp_checkbox_ONE,cp_checkbox_THREE,cp_checkbox_SIX"> The field is hidden so the user does not see it. Then when you get the post you can check to see if this hidden field exists, and process the names you find in it: if (isset($_POST['cp_checkbox_names'])) { $cb_names = explode(',' $_POST['cp_checkbox_names']); foreach ($cb_names as $cb_name) { if (!isset($_POST[$cb_name])) $_POST[$cb_name] = array(); } } Then the loop process we worked out before should find the checkbox array in $_POST and find it empty and clear them out. Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1103352 Share on other sites More sharing options...
ThunderVike Posted August 25, 2010 Author Share Posted August 25, 2010 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... Quote Link to comment https://forums.phpfreaks.com/topic/211560-getting-a-loop-to-simply-write-static-code/#findComment-1103469 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.