• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple question - $this->load->vars(array) not working

#1
[eluser]chops[/eluser]
i have used this method before and it worked (pretty sure it was the same syntax), basically trying to load two queries in one view, however i get an error like this
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined variable: newsdata

Filename: controllers/casestudies.php

Line Number: 19
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: controllers/casestudies.php

Line Number: 20
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: newsquery

Filename: controllers/casestudies.php

Line Number: 22
I thought i could load it via vars but i can't see where i'm going wrong
Code:
function index()
    {
        $data['query'] = $this->db->get('casestudies');
        $data['newsquery'] = $this->db->get('news', 3);        
        

        $this->load->vars(array(
            'data' => $data,
            'newsdata' => $newsdata,
            'query' => $query,
            'newsquery' => $newsquery
        ));
        $this->load->view('casestudies_view');
    }
and my view
Code:
<?php $this->load->view('header'); ?>
        <h2>Case Studies</h2>
        
            &lt;?php foreach($query->result() as $row): ?&gt;

                <h3>&lt;?=$row->title ?&gt;</h3>

                <img >title ?&gt;/thumbs/&lt;?=$row->image ?&gt;.jpg" alt="&lt;?=$row->title ?&gt;" />  

        <p>&lt;?=anchor('casestudies/project/'.$row->id, 'read the article') ?&gt;</p>
            &lt;?php endforeach; ?&gt;

        
</div>&lt;!-- end content --&gt;    
<div id="sidebar">
    <h2>Recent News</h2>

                    
    &lt;?php foreach($newsquery->result() as $row): ?&gt;

            <h3>&lt;?=$row->title ?&gt;</h3>
            <p>&lt;?=$row->date ?&gt;</p>
            <p>&lt;? echo word_limiter($row->body, 28) ?&gt;<br />
            &lt;?=anchor('news/article/'.$row->id, 'read the article') ?&gt;</p>

        <hr />
    &lt;?php endforeach; ?&gt;
</div>&lt;!-- End sidebar --&gt;
&lt;?php $this->load->view('footer'); ?&gt;

why doesn't the query load into the view? is my syntax wrong?

#2
[eluser]Seppo[/eluser]
You should just...

Code:
function index()
    {
        $data['query'] = $this->db->get('casestudies');
        $data['newsquery'] = $this->db->get('news', 3);        

        $this->load->view('casestudies_view', $data);
    }

#3
[eluser]xwero[/eluser]
Or
Code:
function index()
    {
        $this->load->vars(array(
            'query' => $this->db->get('casestudies'),
            'newsquery' => $this->db->get('news', 3)
        ));
        $this->load->view('casestudies_view');
    }
Or
Code:
function index()
    {
       $vars['query'] = $this->db->get('casestudies'),
       $vars['newsquery'] = $this->db->get('news', 3)
       $this->load->vars($vars);
        $this->load->view('casestudies_view');
    }


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.