Meta key and Meta value in CodeIgniter |
I have a table
CREATE TABLE IF NOT EXISTS `site_settings` ( `id` int(11) NOT NULL AUTO_INCREMENT, `meta_key` varchar(255) NOT NULL, `meta_value` varchar(255) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ; INSERT INTO `site_settings` (`id`, `meta_key`, `meta_value`) VALUES (1, 'site_name', 'ABN WEBETCH'), (2, 'site_url', 'http://www.abnwebtech.com'), (3, 'site_slogan', 'Web Make Web Beautiful'), is there any way to get meta_value in view without using foreach loop , Actually looking for custom function . http://prnt.sc/ay22ht Thanks
If there were a PHP function to do this, I imagine it would internally use a loop anyway.
I actually have this scenario come up often, and wrote a custom function for it, which just loops through and tests the specified key for the specified value. Unless someone else knows a better way, I'd suggest just looping.
At some point you're going to have to use a foreach loop anyway, aren't you? To display those items in the view you need to loop over the collection.
Code: <?= foreach ($rows as $row) : ?> However, you can get all of the meta_value values with array_column, which would provide an array of just that column. Code: $values = array_column($rows, 'meta_value'); Although, if you know they're always going to come back in that sequence, you can pull it directly from the array with $rows[1]['meta_value']. Probably the simplest solution is to build a little function that returns something you can more easily use in the view: Code: public function formatArray(array $rows) Then you have an array more like: Code: [ which is much more pleasant to work with in the view. (04-28-2016, 04:07 PM)JayAdra Wrote: If there were a PHP function to do this, I imagine it would internally use a loop anyway. Thanks but i am looking for function. (04-28-2016, 07:04 PM)kilishan Wrote: At some point you're going to have to use a foreach loop anyway, aren't you? To display those items in the view you need to loop over the collection. Thank for reply. but i am looking for WordPress like function such as get_meta() for get the meta value by meta_key Exp: get_meta('site_url'); Output : http://abnwebtech.com/ update_meta() for for insert or update meta Exp: update_meta('admin_email', '[email protected]'); Thanks.
@nkhan
Like this? Code: function get_meta($key) { Edit: You can examine this library: https://github.com/ericbarnes/ci-settings and to make something simplified for your case. |
Welcome Guest, Not a member yet? Register Sign In |