Welcome Guest, Not a member yet? Register   Sign In
What is best way to get and unserialize data in codeigniter?
#1

(This post was last modified: 06-27-2015, 05:00 AM by wolfgang1983.)

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?
There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#2

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']))
);

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply
#3

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB