mwstewart Posted January 17, 2010 Share Posted January 17, 2010 All, I have an AJAX function which passes a POST array to a PHP method. Pat of the method is below: /* /* Update the current article based on information stored in the given array /* ACCEPTS: Array containing update Form data /* RETURNS: */ function articleEditUpdate($postArray){ ksort($postArray); // Sort postArray array $cleanArticleID = siteSanitiseInput($postArray["edit_parent_article_id"]); print_r($postArray); /* * OPTIONS edit_options_ */ // Loop through postArray foreach ($postArray as $postKey => $postValue){ echo $postKey."\n"; // Select the optiosn values if (preg_match('/edit_options_.*/', $postKey)) { // Strip off edit_options_ to leave the solumn name from mws_article $optionColumn = preg_replace('/edit_options_/', "", $postKey); $cleanOption = siteSanitiseInput($postValue); $sqlUpdateOption = sprintf("UPDATE `mwstewart`.`mws_article` SET `%s` = '%s' WHERE `article_id` = %d", $optionColumn, $cleanOption, $cleanArticleID); if(!mysql_query($sqlUpdateOption)){ return "sqlUpdateOption | " . mysql_errno() . ": " . mysql_error(); } } } // End loop through postArray unset($postKey); // Destroy foreach elements unset($postValue); The output of print_r($postArray): Array ( [edit] => true [edit_cancel] => Revert [edit_cancel_exit] => Exit [edit_options_admin_only] => n [edit_options_allow_comments] => y [edit_options_area] => Projects [edit_options_classification] => Modification [edit_options_content_id] => 121 [edit_options_format] => Standard [edit_options_info_centre] => y [edit_options_rss] => n [edit_options_short_title] => mk3si2litre [edit_options_type] => car [edit_parent_article_id] => 302 [edit_parent_car_id] => 42 [edit_parent_content] => <a href=\"/images/2390/\" title=\"Fiesta Si Restoration and 2.0 Conversion\" rel=\"lightbox\"><img class=\"thumb\" src=\"/images/2391/\" width=\"150\" height=\"113\" alt=\"Fiesta Si Restoration and 2.0 Conversion\" title=\"Fiesta Si Restoration and 2.0 Conversion\"></img></a> [edit_parent_content_id] => 121 [edit_parent_date] => 2010-01-12 12:35:03 [edit_parent_image_directory] => /images/cars/Ford/mk3si [edit_parent_image_options] => NONE [edit_parent_title] => Fiesta Si Restoration and 2.0 Conversion [edit_referrer] => /editarticles/mk3si2litre/ [edit_save] => Save [edit_save_exit] => Save and Exit ) The output of echo $postKey."\n" edit edit_cancel edit_cancel_exit edit_options_admin_only edit_options_allow_comments edit_options_area edit_options_classification edit_options_content_id edit_options_format edit_options_info_centre edit_options_rss edit_options_short_title edit_options_type edit_parent_article_id Why is foreach failing to iterate through all of the array elements? I've been pulling my hair out over this one, so any help appreciated. Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/ Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 $cleanArticleID = siteSanitiseInput($postArray["edit_parent_article_id"]); what does this function do? "edit_parent_article_id" is where the foreach ends it's loop. I'm assuming this function does something to the array after key "edit_parent_article_id". Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996299 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Your issue is your mysql queries.. If one of them fails it will break the loop.. return "sqlUpdateOption | " . mysql_errno() . ": " . mysql_error(); instead of returning the error, which doesnt work anyway, try adding each mysql_error into an array $errors[$key] = "sqlUpdateOption | " . mysql_errno() . ": " . mysql_error(); and check for errors AFTER the loop.. then your loop will continue and errors are recorded for you to display Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996320 Share on other sites More sharing options...
mwstewart Posted January 17, 2010 Author Share Posted January 17, 2010 Catfish - I use it to sanitise any GET or POST variables before I use them, it uses preg_replace to turn potential URLS into something safe. Buddski- Thanks, that behavior is intentional as I wan to exit the method if anything fails, I evaluate the return of the method; if string then I display as an alertbox to the user, if true I continue to process the rest of the script which called this particular method. Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996575 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 If its intentional then you already know why its not working... change it to echo "sqlUpdateOption | " . mysql_errno() . ": " . mysql_error(); return; as you arent actually 'returning' to a var or echo you need to echo it then break the loop. Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996578 Share on other sites More sharing options...
trq Posted January 17, 2010 Share Posted January 17, 2010 Buddski- Thanks, that behavior is intentional as I wan to exit the method if anything fails, I evaluate the return of the method; if string then I display as an alertbox to the user, if true I continue to process the rest of the script which called this particular method. Intentional or not, it answers the question. Can you mark this thread solved? Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996580 Share on other sites More sharing options...
mwstewart Posted January 17, 2010 Author Share Posted January 17, 2010 Thanks, but I don't want the error echoed in this method, if the return is a string (which i would be if there was an error), then the calling method would echo it. I don't want echo within the method pasted at the start of the thread. I can't see how echoing the error within this method rather than returning an error string would change it's functionality; If the method was stopping due to a mysql error that would be fine, but it's not (I've not got any mysql error messages, or returned strings). I've commented out the mysql query section anyway but the problem remains. [code=php:0] /* /* Update the current article based on information stored in the given array /* ACCEPTS: Array containing update Form data /* RETURNS: */ function articleEditUpdate($postArray){ ksort($postArray); // Sort postArray array $cleanArticleID = siteSanitiseInput($postArray["edit_parent_article_id"]); //print_r($postArray); /* * OPTIONS edit_options_ */ // Loop through postArray foreach ($postArray as $postKey => $postValue){ echo $postKey."\n"; // Select the optiosn values if (preg_match('/edit_options_.*/', $postKey)) { // Strip off edit_options_ to leave the solumn name from mws_article $optionColumn = preg_replace('/edit_options_/', "", $postKey); $cleanOption = siteSanitiseInput($postValue); $sqlUpdateOption = sprintf("UPDATE `mwstewart`.`mws_article` SET `%s` = '%s' WHERE `article_id` = %d", $optionColumn, $cleanOption, $cleanArticleID); /*if(!mysql_query($sqlUpdateOption)){ return "sqlUpdateOption | " . mysql_errno() . ": " . mysql_error(); }*/ } } // End loop through postArray unset($postKey); // Destroy foreach elements unset($postValue); Same problem. edit edit_cancel edit_cancel_exit edit_options_admin_only edit_options_allow_comments edit_options_area edit_options_classification edit_options_content_id edit_options_format edit_options_info_centre edit_options_rss edit_options_short_title edit_options_type edit_parent_article_id Is it bad practice to return the output of a mysql error, if there is one? Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996584 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 I completely missed the whole "its in a function part" so your returning method is fine.. As for the array, Catfish mentioned earlier about the siteSanitiseInput function being a probable cause. Can you show us this function? Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996587 Share on other sites More sharing options...
mwstewart Posted January 17, 2010 Author Share Posted January 17, 2010 Sure: /* * Santise supplied input. Stip to given length (from - to), strip tags, slashes and URL elements. */ function siteSanitiseInput($input, $from = null, $to = null){ $input = @strip_tags($input); // Remove HTML tags $input = @stripslashes($input); // Remove slashes if ($from && $to != null){ $input = @substr($input, $from, $to); // trim to expected size if over } return $input; } Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996588 Share on other sites More sharing options...
Catfish Posted January 17, 2010 Share Posted January 17, 2010 I can't see siteSanitiseInput being the issue, if you commented out the if (...) return (...) statements and it is still failing to output the entire array, this ones got me stumped. have you checked the contents of $postArray using a print_r() from inside the foreach loop? it will print it about 20 times or so but it will verify the array is remaining how you expect it to be. There seems to be two options for what is going on: 1. the $postArray value is being altered somewhere or 2. your code is returning from the foreach loop before the complete iteration is done. (The only other option is you are playing silly buggers with us ) Given that siteSanitiseInput() is only stripping tags and slashes from a value in the array, I can't see the $postArray being altered in anyway, so I would go back to Buddski's line of thought that the function is failing on a mysql query call and is returning out of the foreach loop - that really looks like the only viable option. However you say that commenting the lines out makes no difference... so... ? can you put an echo call AFTER the foreach loop to ensure that the code is exiting the foreach loop as expected? Eg: foreach (...) { // blah } echo "Exit foreach<br/>\n"; Quote Link to comment https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/#findComment-996735 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.