Friday, April 16, 2010

Codeigniter Pass Object through SESSION variable how to

Codeigniter Can not pass Object through SESSION variable

Untuk yg kesulitan pass object dari controller ke view,
via session. Ini obat nya,

// controller file mu, test1.php
// Location: ./system/application/controllers/test1.php
...
$buffer = array();

$dummy_object = new stdClass;
$dummy_object->name = 'ade1';
$dummy_object->id = 'ADEX1';

array_push($buffer, $dummy_object);

$dummy_object->name = 'ade2';
$dummy_object->id = 'ADEX2';

array_push($buffer, $dummy_object);

/**
* Kalau passing nya spt ini pasti error
* can't convert object to string
*
* $this->session->set_userdata('passed_object', $buffer);
*
* Jadi gimana?
*/

$this->session->set_userdata('passed_object', serialize($buffer));
...
?>

// controller file mu, test2.php
// Location: ./system/application/controllers/test2.php
...

/**
* utk mengambil kembali data nya dlm object tinggal
* di unserialize :D
*/

$passed_object = $this->session->userdata('passed_object', unserialize($buffer));
...
?>


Smoga obat nya ampuh.

Coding for life, coding with ethic.

No comments: