Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have table name 'preferences' column(key,value)

http://imageshack.com/a/img202/2909/lvaz.jpg

I use cache in codeigniter

look this :

$pref = $this->ci->db->get('preferences')->result();

$this->ci->cache->save('preferences', $pref, 30000);

save cache :

a:3:{s:4:"time";i:1386246188;s:3:"ttl";i:30000;s:4:"data";a:87:{i:0;O:8:"stdClass":2:{s:3:"key";s:10:"site_title";s:5:"value";s:13:"CARS Big";}i:1;O:8:"stdClass":2:{s:3:"key";s:11:"forum_title";s:5:"value";s:14:"CARS Big forum";}i:2;O:8:"stdClass":2:{s:3:"key";s:14:"forum_per_page";s:5:"value";s:2:"10";}...

Call cache use:

$data = $this->ci->cache->get('preferences');

print_r($data);

output:

Array( 
            [0] => Array
            (
                [key] => site_title
                [value] => CARS Big
            )

        [1] => Array
            (
                [key] => forum_title
                [value] => CARS Big forum
            )

        [2] => Array
            (
                [key] => forum_per_page
                [value] => 10
            )

        [3] => Array
            (
                [key] => forum_section_per_page
                [value] => 10
            )

        [4] => Array
            (
                [key] => forum_replies_per_page
                [value] => 5
            )

        [5] => Array
            (
                [key] => forum_can_add_pictures
                [value] => 1
            )

        [6] => Array
            (
                [key] => forum_can_add_poll
                [value] => 1
            )

        [7] => Array
            (
                [key] => forum_can_set_time_to_close
                [value] => 1
            )

        [8] => Array
            (
                [key] => forum_can_set_replies_to_close
                [value] => 1
            )

        [9] => Array
            (
                [key] => forum_auto_active_topics
                [value] => 1
            )

        [10] => Array
            (
                [key] => market_title
                [value] => market CARS Big
            )

        [11] => Array
            (
                [key] => market_per_page
                [value] => 5
            )

        [12] => Array
            (
                [key] => market_section_per_page
                [value] => 3
            )


    )

How do I make the content of the column key is the key

And the column value is the content

Like this:

$data['site_title']

I need $data['site_title'] to print : CARS Big

so as to call this function

function pref($key=NULL)
{
$data = $this->ci->cache->get('preferences');
return $data[$key];
}

**

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

you can't, since the cache save implementation is a fixed process. only if you make your own cache implementation.

but you can perform your return function like this

function pref($key=NULL)
{
    // call pref data
    $data = $this->ci->cache->get('preferences');
    if( ! $data ) {
        // cache not present request new
        $data = $this->ci->db->get('preferences')->result();
        $this->ci->cache->save('preferences', $data, 30000);
    }

    // loop
    foreach( $data as $preferences ) {
        if( isset( $preferences['key'] ) && $preferences['key'] == $key ){
            return $preferences['value'];
        }
    }
    return false;
}
share|improve this answer
    
Thank you very much ^_^ –  teto dkil Dec 5 '13 at 14:19
    
Really , It Smart Idea :) –  teto dkil Dec 5 '13 at 14:48
    
glad to hear it helped you :) –  ins0 Dec 5 '13 at 14:59
add comment

Essentially you are looking to loop the values and re-assign the keys so something like this could work:

// loop through data
foreach($data as $k=>$v)
{
    // unset the original array item to get rid of $data[0], $data[1], $data[2] as so forth
    unset($data[$k]);

    // $k is a digit (0,1,2,3,4,5,....)
    // $v is the array of values so $v['key'] is 'site_title' and $v['value'] is 'CARS Big'
    // so essentially we are doing $data['site_title'] = 'CARS Big'; in the line below
    $data[$v['key']] = $v['value'];
}
share|improve this answer
    
Thank you for trying :) –  teto dkil Dec 5 '13 at 14:21
    
Was it broken? I didn't test it out or check for syntax errors lol. Glad you got the answer though! –  MonkeyZeus Dec 5 '13 at 14:22
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.