0

I have made a library class in php which can get data from my database table settings.

I can get information if it is not serialized.

But I would like to know on my get function what would be best method to be able to return unserialise information just incase it is serialized.

<?php

class Settings {

private $data = array();

public function __construct() {
$this->CI =& get_instance();      
}

public function get($key = NULL) {
if ($key != NULL) {
$value = $this->get_where($key);
return $value;
} else {
return FALSE;
}
}

public function get_where($key = NULL) {
if ($key != NULL) {
$store_id = $this->store_id();
$result = $this->CI->db->get_where($this->CI->db->dbprefix . 'setting', array('store_id' => $store_id, 'group' => 'config', 'key' => $key));
if ($result != FALSE AND $result->num_rows() > 0) { 
$data = $result->result_array();
return $data[0]['value'];
} else {
return FALSE;
}
} else {
return FALSE;
} 
}

public function store_id() {
$store_id = $this->CI->uri->segment(5);
if ($store_id !== TRUE) {
return $store_id;
} else {
return FALSE;
}
}

}
2
Contributors
1
Reply
14
Views
2 Years
Discussion Span
Last Post by pixelsoul
0

I would probably just unserialize the value, regardless of whether it is serialized or not, since I expect it to always be unserialized.

public function get($key = NULL) {
    if ($key != NULL) {
        $value = $this->get_where(@unserialize($key));
        return $value;
    } else {
        return FALSE;
    }
}

Also, I've used this function in the past to check if a string is serialized.

function isSerialized($str) {
    return ($str == serialize(false) || @unserialize($str) !== false);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.