Discussions
Categories
Groups
Advertisement
Child Item
Home
Topics
Technology & Internet
Software & Web Development
Design
copying array into db - PHP MYSQL
ChicoMendez
Hi
i need to copy the $_POST array into a db so i can use it later
what im doing not is im assigning $post=print_r($_POST, true) into a db field.
however the problem is that when i pull the $post out of the db it is only a string now - anyone know how i convert the following string into an array :
Array ( [option] => com_profile [Itemid] => 3 [task] => searchprofile [choice] => 0 [sport] => 0 [gender] => 0 [country] => AU [city] => [radius] => [username] => [photo] => 0 [body] => 0 [agefrom] => [ageto] => [heightfrom] => 0 [heightto] => 0 [life] => 1 [status] => 0 [children] => 0 )
ttnx
Find more posts tagged with
Quick Links
All Categories
Recent Posts
Activity
Unanswered
Groups
Best Of
Advertisement
Advertisement
Comments
seamus
$_POST is already an associative array. So all you really need to do is try format the input to the database, so it can be more easily extracted later on.
The best way to do it would be to have a separate table for the data - three columns; RefID (whatever the related reference in another table is), varname, value.
Then when you need to get the POST data again, you just call
SELECT varname, value from postdata where refid = $id
and pull the data from the result into an array.
If you want to dump all of the post data into a single varchar column, then perhaps just format it the way you want it.
Before you input the data, choose a character which separates each key/val pair, say#. Then choose a character which separates the key from the val, say ~.
Then format a string like that, e.g.
[php]
$data_string = "";
foreach($_POST as $key => $val) {
if($data_string == "") { //This checks to make sure we don't put a # at the start or end of the string.
$data_string .= "#"
}
$data_string .= "$key~$val";
}
[/php]
The end result is that $data_string will look something like
#option~com_profile#Itemid~3#task~searchprofile#choice~0#sport~0#gender~0
This looks odd, but it's a predicatable format taht you can use.
Then when you extract this string from the database:
[php]
$postdata = array();
$pairlist = split("#", $datastring); //This creates an array of strings, each string being a key/val pair.
foreach($pairlist as $pair) {
list($key, $val) = split("~", $pair);
$postdata[$key] = $val;
}
[/php]
I would still recommend going with my original suggestion though.
The second option can be messy and troublesome.
forbairt
This sounds like a job for superted ....
er I mean ... Serialize ...
Investigate the following ...
http://www.php.net/manual/en/function.serialize.php
then .. investigate unserialize