Working with $_POST array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Working with $_POST array

Post 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!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I think you want to use serialize() instead.
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post 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? :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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()
idotcom
Forum Commoner
Posts: 69
Joined: Thu Mar 04, 2004 9:24 pm

Post 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??
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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";
}
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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

Code: Select all

explode("|", $data);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply