Page 1 of 1
Working with $_POST array
Posted: Mon Aug 08, 2005 8:26 pm
by idotcom
Hi
I have been trying to find a solution but have had no luck.
I am trying to take the $_POST array and implode its key=value into one string that I can later explode.
Does anyone have any solution???
Example of desired array to string outcome:
$all_items = item1=23|item2=12|item3=45 .... etc...
The end result string will be inserted into the db(all into one field)
Please let me know if you have any ideas... Thank you!
Posted: Mon Aug 08, 2005 8:28 pm
by John Cartwright
I think you want to use
serialize() instead.
Posted: Mon Aug 08, 2005 8:37 pm
by idotcom
Thanks for your quick reply.
I was just checking out that function... but I'm not sure how to go about using it in this case...
Could you maybe shed some light with any example?

Posted: Mon Aug 08, 2005 8:42 pm
by John Cartwright
Yea its pretty simple, although I'm not exactly sure on how you want to handle capturing your data. I'm assuming that you want to capture all form fields and insert them into a column.
Code: Select all
$array = serialize($_POST);
$query = 'INSERT INTO `table` SET `column = \''.$array.'\'';
$result = mysql_query($query) or die(mysql_error());
Now when you want to get your array into usable form simply use unserialize()
Posted: Tue Aug 09, 2005 10:41 am
by idotcom
I tried looking more into serialize... but it is not going to work in my case. I need to filter the post data to clean and format the data before it is imploded into one string.
Any other solutions? Anyone??
Posted: Tue Aug 09, 2005 10:44 am
by nielsene
Code: Select all
$str="";
foreach($_POST as $key=>$value) {
// do any cleaning/filter here. calling continue if this item should be skipped
if ($str!="") $str.="|";
$str.="$key=$value";
}
Posted: Tue Aug 09, 2005 10:45 am
by s.dot
Code: Select all
$string = "item1=".$_POST['item1']."|item2=".$_POST['item2']."|item3=".$_POST['item3']."";
That will get your post data into a string like item1=25|item2=57|item3=28
Then later, to explode