Welcome Guest, Not a member yet? Register   Sign In
extracting query information from the controller
#1

[eluser]millo[/eluser]
hi,

i've been used to setting up the controller like such:

Code:
function page() {
    $page_id = $this->uri->segment(3);
    $this->load->model('Page_model');
    $data['page_query'] = $this->Page_model->get_page($page_id);
    $this->load->view('main/generic', $data);
}

this for example, would extract the page information which i then pass down into my view and access the information like so:

Code:
<?php $page_row = $page_query->row(); ?>
<h1>&lt;?php echo $page_row->page_title; ?&gt;</h1>
&lt;?php echo markdown($page_row->text); ?&gt;

i'm not sure however how to access the information that i need from within the controller before it get's passed to the view. for example ...

Code:
function page() {
    $page_id = $this->uri->segment(3);
    $this->load->model('Page_model');
    $data['page_query'] = $this->Page_model->get_page($page_id);
    // HERE I WANT TO ACCESS THE PAGE QUERY DATA, HOW DO I DO THAT?
    // I'VE TRIED PUTTING THE SAME CODE THAT'S USED IN THE VIEW BUT THAT DOESN'T WORK.
    $this->load->view('main/generic', $data);
}

I'm not really clear on how the array information is different from when it's in the controller to when it's in the view. If someone could point me in the right direction please?
#2

[eluser]gon[/eluser]
The controller doesn't know about $page_query, because you have assigned the data to $data['page_query']
Try:

Code:
function page() {
    $page_id = $this->uri->segment(3);
    $this->load->model('Page_model');
    $page_query = $this->Page_model->get_page($page_id);    
    // do whatever you need with $page_query
    $data['page_query'] = $page_query;  // and finally pass it to the view
    $this->load->view('main/generic', $data);
}

Perhaps you were confused by the "magic" that assigns the properties of the $data array to vars. This happens only in the view.
#3

[eluser]imzyos[/eluser]
Code:
function page() {
    $page_id = $this->uri->segment(3);
  
    $page_query = $this->load->model('Page_model');
    $page_row = $page_query->row();
    echo $page_row->page_title;

    $data['page_query'] = $page_query;



    $this->load->view('main/generic', $data);
}
#4

[eluser]millo[/eluser]
Thanks both.

Imzyos, your code didn't work correctly. It through an error but when I used Gon's I was able to use the code:

Code:
$page_row = $page_query->row();
echo $page_row->page_title;

to extract the values I needed.

Thanks again!
#5

[eluser]imzyos[/eluser]
if you want play with the values of the query after load the view use the row function on the controller




Theme © iAndrew 2016 - Forum software by © MyBB