Welcome Guest, Not a member yet? Register   Sign In
Meta key and Meta value in CodeIgniter
#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


Messages In This Thread
Meta key and Meta value in CodeIgniter - by nkhan - 04-28-2016, 01:47 PM
RE: Meta key and Meta value in CodeIgniter - by kilishan - 04-28-2016, 07:04 PM



Theme © iAndrew 2016 - Forum software by © MyBB