Welcome Guest, Not a member yet? Register   Sign In
Meta key and Meta value in CodeIgniter
#1

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
Reply
#2

(This post was last modified: 04-28-2016, 04:08 PM by JayAdra.)

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.
Reply
#3

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) : ?>
    <li><?= $row['meta_key'] ?> = <?= $row['meta_value'] ?></li>
<?php endforeach ?>

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

// $values = [
    'ABN WEBETCH',
    'http://abnwebtech.com',
    'Web Make Web Beautiful'
]

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)
{
    $output = [];

    foreach ($rows as $row)
    {
        $output[$row['meta_key']] = $row['meta_value'];
    }

    return $output;
}

Then you have an array more like:

Code:
[
    'site_name' => 'ABN WEBETCH',
    'site_url' => 'http://abnwebtech.com',
    'site_slogan' => 'We Make Web Beautiful'
]

which is much more pleasant to work with in the view.
Reply
#4

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

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.

Thanks 

but i am looking for function.
Reply
#5

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

Code:
<?= foreach ($rows as $row) : ?>
   <li><?= $row['meta_key'] ?> = <?= $row['meta_value'] ?></li>
<?php endforeach ?>

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

// $values = [
   'ABN WEBETCH',
   'http://abnwebtech.com',
   'Web Make Web Beautiful'
]

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)
{
   $output = [];

   foreach ($rows as $row)
   {
       $output[$row['meta_key']] = $row['meta_value'];
   }

   return $output;
}

Then you have an array more like:

Code:
[
   'site_name' => 'ABN WEBETCH',
   'site_url' => 'http://abnwebtech.com',
   'site_slogan' => 'We Make Web Beautiful'
]

which is much more pleasant to work with in the view.

  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.
Reply
#6

(This post was last modified: 04-29-2016, 03:23 AM by ivantcholakov.)

@nkhan

Like this?

Code:
function get_meta($key) {

    $row = get_instance()->db
        ->select('meta_value')
        ->from('site_settings')
        ->where('meta_key', $key)
        ->limit(1) // 'meta_key' should be unique anyway, this is just in case.
        ->get()
        ->result_array();

    return isset($row['meta_value']) ? $row['meta_value'] : null;
}

Edit: You can examine this library: https://github.com/ericbarnes/ci-settings and to make something simplified for your case.
Reply
#7

Thank to all of you for replying me
Reply




Theme © iAndrew 2016 - Forum software by © MyBB