Firemouth
08-21-2007, 04:06 PM
I've searched google far and wide (but maybe not with the right params?) but I have run out of options to try to accomplish my seemingly simple task.
My problem is I need to pass a multidimensional array between JS and PHP. I have successfully transfered my PHP array into JS, but I am unable to reverse the transaction without knowing all of the fields on the JS array.
This may seem like a PHP question (since I talk about it alot) but the problem I'm having is going from Javascript to PHP. I felt this was more of a Javascript question than a PHP question... but its debatable.
Here is some background information regarding the "why the heck are you doing this". I have a very ajax-i-fied page that I'm using to generate reports from. I am going to have many, many different options that will affect how the data is filtered / sorted / displayed on this report. Due to the amount of options, and attempting to reduce the number of tables and fields in my database, the method I chose to accomplish this is to store everything in a multi-dimensional array, serialize it, then base64 encode it, and then save that into a field in my database (yes, its a huuuuge field). Sounds complicated, but it works beautiful as far as PHP is concerned.
So the order of events when a report is called up is this:
1. Select report record from database (including the huge string)
2. Decode/unserialize the string into my multi-dimensional array.
3. Read out all the options on the page (using a foreach loop via PHP) to set the initial values of the report options and all that good stuff.
After the initial page load, I'm currently maintaining that ginormous saved settings string whenever someone makes a change. Which means i decode/unserialize, make my change, then serialize/encode and then save it back to a hidden input. The reason I'm going through this painstaking process is so I have one variable I need to be concerned with when saving information. Also, it allows me to store more than just a name and a value that a hidden input would limit me to. That way I don't 1. miss any info or 2. have to change 10 different spots to add a new saved setting, just append the array and I'm done as far as preserving the information goes.
A different process (this is what I'm trying to do now) is to restore the array from the DB using PHP (same as I'm doing now), put it into a Javascript array, and manipulate this JS array while the user is modifying the report. No PHP is involved after the initial load. Then, if the user wants to save this report, pull the array back out of JS, serialize / encode it using PHP and put it back into the database. I've successfully completed this, except for the last part of getting the array out of javascript and into PHP. I can't find a JS serialize method that is fully compatible with PHP unserialize(). Every function I've found says that there's a bug with multi-byte characters. Javascript handles them fine, but PHP blows up on them. So I'm trying to find a work-around.
Any ideas?
One idea I'm currently exploring is converting the JS array into XML, then parse the XML into a PHP array, and bam I've got it. I think this might work, but it's still a work in progress. If I get that working I'll post back here on exactly what I did to make it work.
I hope this post makes sense. Thanks for reading and thanks in advance for any replies!
My problem is I need to pass a multidimensional array between JS and PHP. I have successfully transfered my PHP array into JS, but I am unable to reverse the transaction without knowing all of the fields on the JS array.
This may seem like a PHP question (since I talk about it alot) but the problem I'm having is going from Javascript to PHP. I felt this was more of a Javascript question than a PHP question... but its debatable.
Here is some background information regarding the "why the heck are you doing this". I have a very ajax-i-fied page that I'm using to generate reports from. I am going to have many, many different options that will affect how the data is filtered / sorted / displayed on this report. Due to the amount of options, and attempting to reduce the number of tables and fields in my database, the method I chose to accomplish this is to store everything in a multi-dimensional array, serialize it, then base64 encode it, and then save that into a field in my database (yes, its a huuuuge field). Sounds complicated, but it works beautiful as far as PHP is concerned.
So the order of events when a report is called up is this:
1. Select report record from database (including the huge string)
2. Decode/unserialize the string into my multi-dimensional array.
3. Read out all the options on the page (using a foreach loop via PHP) to set the initial values of the report options and all that good stuff.
After the initial page load, I'm currently maintaining that ginormous saved settings string whenever someone makes a change. Which means i decode/unserialize, make my change, then serialize/encode and then save it back to a hidden input. The reason I'm going through this painstaking process is so I have one variable I need to be concerned with when saving information. Also, it allows me to store more than just a name and a value that a hidden input would limit me to. That way I don't 1. miss any info or 2. have to change 10 different spots to add a new saved setting, just append the array and I'm done as far as preserving the information goes.
A different process (this is what I'm trying to do now) is to restore the array from the DB using PHP (same as I'm doing now), put it into a Javascript array, and manipulate this JS array while the user is modifying the report. No PHP is involved after the initial load. Then, if the user wants to save this report, pull the array back out of JS, serialize / encode it using PHP and put it back into the database. I've successfully completed this, except for the last part of getting the array out of javascript and into PHP. I can't find a JS serialize method that is fully compatible with PHP unserialize(). Every function I've found says that there's a bug with multi-byte characters. Javascript handles them fine, but PHP blows up on them. So I'm trying to find a work-around.
Any ideas?
One idea I'm currently exploring is converting the JS array into XML, then parse the XML into a PHP array, and bam I've got it. I think this might work, but it's still a work in progress. If I get that working I'll post back here on exactly what I did to make it work.
I hope this post makes sense. Thanks for reading and thanks in advance for any replies!