Jump to content

Recommended Posts

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.

 

Link to comment
https://forums.phpfreaks.com/topic/188737-foreach-missing-array-elements/
Share on other sites

$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".

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

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.

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.

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?

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?

 

 

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

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";

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.