Welcome Guest, Not a member yet? Register   Sign In
Not able to display query results in View
#1

[eluser]chotz[/eluser]
I am loading a model in the controller which run the query
and gives back the results to controller.
I am able to display the results in controller.


Then i am passing the $data to view page:
$this->load->view('/GameDev/newsad_view',$data);

but i am not able to display the results in the view page

I am using the following code:
<?php echo news_headln; ?>

('news_headln' is my table column stored in the $data array)

i also tried <?php echo $data['news_headln'];?>

I get the error Undefined variable $data.

Please help.

-Arun.
#2

[eluser]chotz[/eluser]
Actually i have the following code in my controller

Code:
$this->load->model('/GameDev/Newsad_model');
        $data['query']=$this->Newsad_model->get_entries();
        

        //print_r($data['query']);
        $this->load->view('/GameDev/newsad_view',$data);

In the model class i have:

Code:
$query=$this->db->get('News_admn');
return $query->result();

for some reason i am not able to access $data in View
#3

[eluser]jedd[/eluser]
Hi Arun,

No, you don't have $data in your view - only the break down of the contents of the $data array (as it was in the controller).

So if you have $data['news_headln'] in your controller, you'd need to have this in your view:
Code:
echo $news_headln;

Note the obvious mistake (not sure if it was a typo) in your code snippet that you posted:

Quote:I am using the following code:
<?php echo news_headln; ?>

You definitely need the ol' $ there.
#4

[eluser]chotz[/eluser]
No, that was a typo.
But as you can see from the below line of code my results are in 'query' array

Code:
$data['query']=$this->Newsad_model->get_entries();

query itself will hold an array.

So i made the $data hold just my
table columns.
Code:
$data=$this->Newsad_model->get_entries();


and changed my model class to
Code:
foreach ($query->result() as $row)
        {
            $item['news_headln']=$row->news_headln;
            $item['news_detl']= $row->news_detl;
            $item['news_date']= $row->news_date;
        }
    return $item;

i.e basically it means $data=$item;


Now it works fine Smile
#5

[eluser]jedd[/eluser]
Glad to hear you resolved it. I'd recommend you still assign the return from your model to an element of $data[] - as you are likely to want to have more than just the contents of that one returned value in your view. Re-introducing this indirection, now you have the basics working fine, should be very easy.
#6

[eluser]chotz[/eluser]
ok.You are right. I tried that too. but could not
display the elements in view for some reason.
Anyway i will figure it out.

Thanks for your help!




Theme © iAndrew 2016 - Forum software by © MyBB