Welcome Guest, Not a member yet? Register   Sign In
halp! getting data from model to view
#1

[eluser]taro uma[/eluser]
I am just learning codeigniter, and have a question about getting variables.

so I have a model that gets data from a database,

The database has ID, Title, Message.

Code:
function get_frontpage_content()
    {
        $query = $this->db->get('FrontPage');
        return $query->result();
    }

I load this in my controller and then load my view:

Code:
$this->load->model('FrontPageModel','',TRUE);
$data['frontpagedata'] = $this->FrontPageModel->get_frontpage_content();
$this->load->view('frontpage',$data);

Then in the view, I was trying to access by $frontpagedata['Title'], etc. but it was giving a message the variable was undefined.

So I print_r the $frontpagedata and it shows me this:

Code:
Array ( [0] => stdClass Object ( [ID] => 1 [Title] => Welcome [Message] => Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent tincidunt, risus in convallis eleifend, augue odio rhoncus mi, nec iaculis mi arcu ac nibh. Suspendisse eget mi a mi semper facilisis. Duis elementum, odio id commodo fringilla, erat nunc adipiscing metus, ac sollicitudin risus est eget risus. Praesent sit amet ipsum sed metus imperdiet elementum. Pellentesque odio dui, iaculis vel, luctus ut, elementum vel, ante. Integer nec ipsum. Etiam cursus tortor id turpis. Sed auctor dui vel ligula. Ut consequat purus at leo. Cras varius. Phasellus convallis commodo nisl.))

So I can see that the variables I want are stuck in this stdClass Object, but I am not sure how to avoid this and have the variables simply part of the $frontpagedata array so I can call them like $frontpagedata['Title']
#2

[eluser]TheFuzzy0ne[/eluser]
Your model is passing back an object, and you're trying to access it as an array.

Code:
# For an object:
$object = $query->result();
$title = $object->title;

# For an array:
$array = $query->result_array();
$title = $array['title'];
#3

[eluser]taro uma[/eluser]
Thanks for the reply. I am still having a problem figuring out how to name or access the array

I changed my model code to this:

Code:
function get_frontpage_content()
    {
        $query = $this->db->get('FrontPage');
        return $query->result_array();
    }

so now I get an array, but I can't access it like this:

$frontpagedata['Title']

I have to access it like this:

$frontpagedata[0]['Title']

So I know I am doing something wrong, because I want my variables to be in the $frontpagedata array but can't see how to get them in there without being nested in yet another array.
#4

[eluser]TheFuzzy0ne[/eluser]
OK, if you're expecting only a single row, then you need to return the array using row_array() (as opposed to result_array()). That will let you access it the way you want to.

If you're expecting multiple rows, then you need to treat the result array as an array of arrays, and loop through them.

The same applies to when you want the result(s) in object form.

row() returns the first row in object form.
result() returns an array of objects for you to loop through.

I hope this helps.
#5

[eluser]taro uma[/eluser]
Ah, that's it, I see that in the User Guide now. I could see how I could get that data using a foreach loop, but it didn't make sense with one row.

Thanks again!




Theme © iAndrew 2016 - Forum software by © MyBB