Welcome Guest, Not a member yet? Register   Sign In
Using Objects in Views
#1

[eluser]ChrisMcABZ[/eluser]
Hi there. Forgive me if this has been covered already; I tried searching, but couldn't find what I was looking for.

I'm starting to roll my first CI application using 1.6.0 - it's a simple CMS for my personal site. What I would like to do is this (code shortened for berevity):

(model)
Code:
public function GetBySlug($slug='')
{
    $entry['slug'] = $slug;
        
    return $this->db->get_where('entries', $entry)->result();
}

(controller)
Code:
$data['entry'] = $this->Entry->GetBySlug($slug);
$this->load->view('entry', $data['entry']);
(view)
Code:
<h1>&lt;?= $entry->title; ?&gt;</h1>

But with this code I just get an error. If I write this instead -

Code:
<h1>&lt;?= $entry[0]->title; ?&gt;</h1>

or use a loop, it works, but that seems a waste for what will always be a single result - and it's not as pretty Smile . Am I missing something (probably something very basic!)? Or do I have to give up my objects in views and start passing result_array() instead?

Thanks in advance, CI Bods!
#2

[eluser]tonanbarbarian[/eluser]
Code:
->result()
always returns an array or objects
maybe try
Code:
return reset($this->db->get_where('entries', $entry)->result());
#3

[eluser]Sawariya[/eluser]
Hi ChrisMcABZ

in model write like this

$this->db->get_where('entries', $entry)->row();

In view

$entry->title;
#4

[eluser]ChrisMcABZ[/eluser]
Thanks guys - I'll give these suggestions a try Smile
#5

[eluser]Sawariya[/eluser]
Hi ChrisMcABZ

Try and let us know the result
#6

[eluser]ChrisMcABZ[/eluser]
[quote author="Sawariya" date="1202467686"]Hi ChrisMcABZ

in model write like this

$this->db->get_where('entries', $entry)->row();

In view

$entry->title;[/quote]

Thios one worked like a charm. Thanks a bundle!




Theme © iAndrew 2016 - Forum software by © MyBB