Welcome Guest, Not a member yet? Register   Sign In
Sending variables from the controller to the view with Active Record Query (newbie question)
#1

[eluser]Juan Velandia[/eluser]
Im sending a query directly from the controller to the view to make a list within a list like this:

placeholder 1
- section1
- section2
- section3

placeholder 2
- section4
- section5
- section6

the query in the controller:

Code:
$data['query1'] = $this->db->get('cms_placeholder');

Code:
$data['query2'] = $this->db->where('idplaceholder',$idplaceholder);
$data['query2'] = $this->db->get('cms_section');

where the table cms_section has a column named idplaceholder, and here's the view:

Code:
<?php foreach ($query1->result() as $row): ?>
<h1>&lt;?echo $row->placeholder_name?&gt;</h1>
&lt;? $idplaceholder = $row->idplaceholder; ?&gt;
&lt;?php foreach ($query2->result() as $row): ?&gt;
<h1>&lt;?echo $row->section_name?&gt;</h1>
&lt;?php endforeach; ?&gt;
&lt;?php endforeach; ?&gt;

But I always get this: Message: Undefined variable: placeholder. I don't want to pass variables via URL therefore I was thinking to put a query within the view to make the $placeholder available, but I'm sure there's a more elegant solution, Thanks in advance for any idea!
#2

[eluser]Ben Edmunds[/eluser]
The correct syntax is:

Code:
$data['query2'] = $this->db->get('cms_section')
$data['result2'] = $data['query2']->result();

Or you can use method chaining if your using php5.

Also, are you using $this->load->view('yourView', $data) in your controller?
#3

[eluser]Juan Velandia[/eluser]
@Ben. thanks a lot for your quick answer. I'll check it!. And yes I'm using $this->load->view(‘yourView’, $data) in my controller., Best Regards!




Theme © iAndrew 2016 - Forum software by © MyBB