CodeIgniter Database Rows Into Arrays To Use By Pa

2019-08-22 03:33发布

1) My original settings table design:

-------------------------------------------
site_name | site_slogan | site_url
-------------------------------------------
Company   | This rocks! | http://localhost/
-------------------------------------------

However, I would like to redesign so I can explicitly put different datatypes like char(255) or varchar(512), or maybe INT in the future for each row:

-------------------------------
name        | value
-------------------------------
site_name   | Company
site_slogan | This rocks!
site_url    | http://localhost/
site_phone  | 123465798
-------------------------------

In my controller, I would like to use $this->parser->parse('page',$data); but I don't know how to put the result of my database query into arrays in a loop so that in my Views I can use them like this:

<a href="{site_url}">{site_name} - {site_slogan}</a>

Edit:

This the result of $data = $this->db->select("*")->from('settings')->get()->result_array(); from the feedback I received.

Array
(
    [0] => Array
        (
            [name] => site_name
            [value] => My Name
        )

    [1] => Array
        (
            [name] => site_tagline
            [value] => My Tagline
        )

    [2] => Array
        (
            [name] => site_url
            [value] => My URL
        )

)

So this is how I parse and make them usable in views {site_name} {site_tagline} {site_url} .

$data = array(
   'site_name' => $data['0']['value'],
   'site_tagline' => $data['1']['value'],
   'site_url' => $data['2']['value']
);
$this->parser->parse('page', $data);

1条回答
虎瘦雄心在
2楼-- · 2019-08-22 04:02

You could assign the $data values to a new array and pass the new array instead :

foreach ($data as $key => $cfg) {
    $new_data[$cfg['name']] = $cfg['value'];
}
$this->parser->parse('page', $new_data);
查看更多
登录 后发表回答