CodeIgniter Forums
What is best way to get and unserialize data in codeigniter? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: What is best way to get and unserialize data in codeigniter? (/showthread.php?tid=62273)



What is best way to get and unserialize data in codeigniter? - wolfgang1983 - 06-27-2015

On my controller I need to be able to get some serialized data but keep on getting error below

A PHP Error was encountered

Severity: Notice
Message: Undefined offset: 0
Filename: common/Dashboard.php
Line Number: 40
Backtrace:

Var dump

string(111) "a:3:{s:7:"user_id";s:2:"20";s:8:"username";s:20:"demo";s:4:"name";s:20:"John Doe";}"

PHP Code:
$results $this->get_user_activities();

foreach (
$results as $result) {

$var $result['data'];

var_dump($var);

$var unserialize($var);

echo 
$var[0]; // Should echo user_id I would think.



Model Function


PHP Code:
public function get_user_activities() {
$this->db->select('data, date_added');
$this->db->from($this->db->dbprefix 'user_activity');
$this->db->order_by('date_added''DESC');
$query $this->db->get();
if (
$query->num_rows() > 0) {
return 
$query->result_array();
} else {
return 
false;
}



How come there is a offset error? And how can I fix it?


RE: What is best way to get and unserialize data in codeigniter? - wolfgang1983 - 06-27-2015

I am just confused on the best way

I tried this code below for getting seems to work but not sure if safe


PHP Code:
$key unserialize($result['data']); 

PHP Code:
foreach ($results as $result) {
$this->lang->load('dashboard/user_activity''english');

$key unserialize($result['data']);

$data['activities'][] = array(
'comment' => anchor('user/user/' $key['user_id'], $key['name']) .' '$this->lang->line('text_' $result['key']),
'date_added' => date($this->lang->line('datetime_format'), strtotime($result['date_added']))
);




RE: What is best way to get and unserialize data in codeigniter? - Wouter60 - 06-27-2015

(06-27-2015, 04:38 AM)riwakawd Wrote: How come there is a offset error? And how can I fix it?

Your array $var doesn't have a key called 0.
echo $var['user_id'];  should give you the userid you're looking for.