codeigniter - PHP Foreach for single result?


So in codeigniter I have the following in a model:

function get_product_requirements()
    $query = $this->db->get_where('settings', array('name' => 'product_laws'));
    return $query->result_array();

As you could guess this only returns one value. Right now I have a foreach to display this in the browser. It doesn't make sense to me to put a foreach for a single returned value.

<?php foreach($laws as $laws){ echo $laws['content']; } ?>

Is there an alternative to how I can display this item?




CodeIgniter's Active Records have a native way of returning one row:

return $query->row_array();

Then, to access your value, use this:

echo $laws['content']; // not inside the foreach loop, of course

Read more about different ways to fetch rows here:




if you sure that you get one row every time you can write your model code in this way:

function get_product_requirements()
    $query = $this->db->get_where('settings', array('name' => 'product_laws'));
    return $query->row();

and get your content with:

$law = $this->model_name->get_product_requirements();
echo $law['content'];

but if you want to stay on your way you can access to your data with



People are also looking for solutions to the problem: javascript - Get Request Header in Node.js to get PHP Session ID


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.