-
Notifications
You must be signed in to change notification settings - Fork 26
Making POST array persistent
(Originally posted in this topic [url=http://www.codeigniter.com/forums/viewthread/312/]pagination and post variables[/url])
If the values from the [i][b]$_POST[/b][/i] array are used in a database query to extract a subset of records from the main database. and if this subset consists of more records than can be displayed on one page. Then, when a pagination link is clicked to create another page of records, the [i][b]$_POST[/b][/i] array is returned empty and the values are not available to replicate the filtering.
Therefore the problem is to make the [i][b]$_POST[/b][/i] array values persistent.
My way of doing this is involves extending the session class with an adaption of the code posted in this thread [url=http://www.codeigniter.com/forus/viewthread/337/]http://www.codeigniter.com/forums/viewthread/337/[/url] by Kelvin Luck.
First, update your database with the following SQL:
[code]ALTER TABLE ci_sessions ADD lastpost_data TEXT[/code]
then add the following code to the session class code (system/libraries/Session.php)
[code]// --------------------------------------------------------------------
// Code to facilitate transfer of $_POST array to and from session database // Based on code by Kelvin Luck published here http://www.codeigniter.com/forums/viewthread/337/
var $last_post;
/**
-
Check if the $_POST global variable contains any data.
-
if it does, copy it to the lastpost_data field in the sessions database.
-
if it is empty, reload it with the last array .
-
@author Dodmo
-
@access public
-
@param boolean
-
@return void / function get_last_post($clear='TRUE'){ if ((count($_POST)==0)&&($this->_is_last_post())){ $_POST = $this->last_post; } else if (count($_POST)>0){ if($clear){ $last_post = array(); $this->set_last_post($_POST); } else{ $this->set_last_post($_POST); $_POST = $this->last_post; } } } /*
-
Test for previous $_POST data stored in session database
-
@author Dodmo
-
@access private
-
@return boolean */ function _is_last_post() { $this->_readlast_post(); $myresult = (count($this->last_post)>0) ; return $myresult; }
/**
-
Fetch a specific item from the lastpost_data array in the session database
-
(not required for get_last_post function but retained for future use.
-
@authorKelvin Luck adapted by Dodmo
-
@accesspublic
-
@paramstring
-
@returnstring */ function last_post($item) { if ($this->_readlast_post() ) { return ( ! isset($this->last_post[$item])) ? FALSE : $this->last_post[$item]; } else { return FALSE; } } // END last_post
/**
- Add or change data in the the lastpost_data array in the session database
- @authorKelvin Luck adapted by Dodmo
- @parammixed
- @paramstring
- @returnvoid */ function set_last_post($newdata = array(), $newval = '') { if ($this->_readlast_post()) { if (is_string($newdata)) { $newdata = array($newdata => $newval); }
if (count($newdata) > 0) { foreach ($newdata as $key => $val) { $this->last_post[$key] = $val; } }
$this->_writelast_post(); } } // END set_last_post
/**
- Internal function to read and unserialize the lastpost_data from the database
- @authorKelvin Luck adapted by Dodmo
- @accessprivate
- @returnboolean
*/
function _readlast_post(){
if ($this->use_database === TRUE) {
if (!isset($this->last_post)) {
$result = $this->object->db->query('SELECT lastpost_data FROM
'.$this->session_table.'WHERE session_id=?', array($this->userdata['session_id'])); if ($result->num_rows() > 0) { $row = $result->row(); $session = $row->lastpost_data; if (count($session)>0){ $session = @unserialize($session); } if ($session == '') { $session = array(); } $this->last_post = $session; return TRUE; } else { log_message('error', '_readlast_post called when there is not a valid session in the database'); return FALSE; } } else { // already read it! return TRUE; } } else { log_message('error', 'You cannot access session->last_post unless you are using databases for your session!'); return FALSE; } } // END _readlast_post
/**
- Internal function to serialize and write the lastpost_data to the database
- @authorKelvin Luck adapted by Dodmo
- @accessprivate
- @returnvoid */ function _writelast_post() { $server_data_serialized = serialize($this->last_post); $this->object->db->query($this->object->db->update_string($this->session_table, array('lastpost_data' => $server_data_serialized), array('session_id' => $this->userdata['session_id']))); }
// END last_post
// End added code to facilitate transfer of $_POST array to and from session database [/code]
Now you have a new method you can call
[code] $this->session->get_last_post(); // (the parameter defaults to TRUE) [/code]
If [i][b]get_last_post()[/b][/i] is passed [i][b]TRUE[/b][/i] parameter or left void then the [i][b]lastpost_data[/b][/i] field in the sessions database will be cleared before the a new [i][b]$_POST[/b][/i] array is inserted ( [i][b]$_POST[/b][/i] is left unmodified) If the function is passed [i][b]FALSE[/b][/i] parameter then the existing [i][b]lastpost_data[/b][/i] will be amalgamated with the fresh [i][b]$_POST[/b][/i] data (exising keys are updated new ones added) and this amalgam is passed back into [i][b]$_POST[/b][/i] for the application to use.
[b]DISCLAIMER[/b] I am new to php/mysql and anyone using this code should bear that in mind.