note 52436 added to function.extract

From: Date: Sun, 01 May 2005 03:59:19 +0000
Subject: note 52436 added to function.extract
Groups: php.notes 
Request: Send a blank email to php-notes+get-88808@lists.php.net to get a copy of this message
The following is a neat use for extract to store and manipulate large amounts of form data from. I basically loop through the $_POST and implode it seperating the key and value pairs by a space. Then store it in a db, the reversing function basically explodes the string to a array. Then converts the indexed array to a associative array then uses extract to seal the deal and make it easily available within a program. My main reason for sharing these are the fact I make some big web applications that store allot of forum data in a DB and these functions make it very easy to quickly and easily store and recall the data. I've contributed it because I spent many hours creating this code and recall going "I wish someone had previously submitted it to the page notes". Would have saved me allot of time and agony and I'm sure I'm not the only person that could really benefit from it, so I decided to share. <?php $stack = array(); foreach ($_POST as $key => $value) { array_push($stack, $key, $value); } // store it $block = implode(" ",$stack); // yeilds a space delimited string // insert query to store string in DB here, like the one below $query = "INSERT INTO sometable VALUES('".$seluser."','".addslashes($block)."');"; $result = mysql_query($query) or die("Query failed for block insert: " . mysql_error()); // note $seluser in my case is a user ID associated with that block // in one of my web apps ?> The nice thing is with the above we can quickly create a string of key and value pairs from the data the script got. Without really caring what their names are. You know how if register globals are on you say $someformvar rather than $_POST["someformvar"]; , basically the code below reads this previous created block returns it to that state. Sort of like presistant register globals. <?php // insert query to grab the previously stored string here $query = "SELECT * FROM sometable WHERE blockid = '".addslashes($bid)."';"; $result = mysql_query($query) or die("Query failed read: " . mysql_error()); $sql = mysql_fetch_array($result, MYSQL_ASSOC); $array = eplode(" ",$sql["data"]); for ($i = 0; $i < sizeof($array); $i+=2) { $myassoc[$array[$i]] = isset($array[$i+1])?$array[$i+1]:NULL; } extract($myassoc, EXTR_OVERWRITE); // now you're key and value pairs from $_POST have been restored // instead of $_POST ?> ---- Manual Page -- http://www.php.net/manual/en/function.extract.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+52436 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+52436&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+52436&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+52436&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+52436&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+52436&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+52436&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#88808) next »