Welcome Guest, Not a member yet? Register   Sign In
Undefined variable: query!
#1

[eluser]lkoreality[/eluser]
I'm getting an undefined variable: data in my controller and an undefined variable: query in my view.

My database should be loading automatically. This is the code in my view file that's causing the problem:
Code:
<? foreach ($query->result() as $sub): ?>
<?=$sub->function; ?>
<? endforeach; ?>

I'm loading a model:

Code:
<?php

class Links extends Model {

    function __controller()
    {
        parent::Link();
    }
    
    function get_db()
    {
        $query = $this->db->query('SELECT * FROM page');
        $data['query'] = $this->Links->get_db();
    }
}

to my controller. I'm using this code:
Code:
$this->load->model('Links');

I tried placing it both in the construct and a function of my controller. I'm loading the data file into a view file.

Any ideas?
#2

[eluser]pistolPete[/eluser]
Are you passing any data to the view?

I would use something like this:

Links_model.php
Code:
class Links_model extends Model {
  
    function get_db()
    {
        $query = $this->db->query('SELECT * FROM page');
        return $query;
    }
}

Links.php
Code:
class Links extends Controller {
  
    function index()
    {
        $this->load->model('links_model');
        $data['query'] = $this->links_model->get_db();
        $this->load->view('some_view', $data);
    }
}

some_view.php
Code:
<?php foreach ($query->result() as $sub)
{
   echo $sub->function;
}
?>
#3

[eluser]lkoreality[/eluser]
I tried that but i've named the model links.php

I'm getting an

Code:
Severity: Notice

Message: Undefined property: Links::$db

Filename: models/links.php
#4

[eluser]pistolPete[/eluser]
Quote:My database should be loading automatically.

But it is not, otherwise you wouldn't get the above error.
#5

[eluser]lkoreality[/eluser]
Ah! I was autoloading the core library not the libraries. Thanks!
#6

[eluser]Zack Kitzmiller[/eluser]
Wrong Topic, sorry.




Theme © iAndrew 2016 - Forum software by © MyBB