Fatal error: Call to a member function query() while my class extends controller

#1
[eluser]Unknown[/eluser]
Hello.
I've made a class it's being loaded and everything is fine with that.
Unfortunately when I want to get access to database PHP generates me fallowing error:
Fatal error: Call to a member function query() on a non-object in D:\WWW\CodeIgniter\system\application\controllers\news.php on line 8

File looks like that:
Code:
<?php
class News extends Controller {
    function index()
    {
        $cnt='';
        $this->lang = $this->lang->user_lang;
        $this->load->database();
        $res = $this->db->query('SELECT * FROM `news` WHERE lang=\''.$this->lang.'\' ORDER BY n_timestamp');
        if($res->num_rows() > 0)
        {
            foreach($res->result_array() as $row)
            {
                $cnt = '<div id="news">
                            <div class="title">'.$row['n_title'].'</div>
                             <div class="date">'.date('H:i d.m.Y', strtotime($row['n_timestamp'])).'</div>
                             <div class="content">'.nl2br($row['n_content']).'</div>
                             </div>';
            }
        }
        $vars = array('lang' => $this->lang, 'title' => $this->get_title(), 'content' => $cnt);
        $this->page->set_vars($vars);
        $this->page->show();
    }
    function get_title()
    {
        $titles['pl'] = 'Nowosci';
        $titles['en'] = 'News';
        return $titles[$this->lang];
    }
}
?&gt;
I've figure out that the problem is in Loader.
It doesn't returns me anything and I don't know why but when it's called by Session class everything is fine. What should I do ?
edit>
Ok now i know why.
Library classes can't extend controller.
Then how to use for example view in my Lib class ?

#2
[eluser]pr0digy[/eluser]
Hi orglee, it seems you're a bit confused. The code above, really belongs to a model (Models are used to work with DB). Normally, you would make a call to a Model function from one of your controllers (when you load DB class within a controller it automatically becomes available loaded models) to obtain the necessary data. Those data, in turn, are assigned to a view, which is then rendered.

There are many tutorials that you should go through, both video and text. I suggest you give them a try first.

#3
[eluser]Unknown[/eluser]
I think Ive explained it in wrong way.
I've created an Library Class named Page that I'm calling in on page News.
Class Page creates skeleton for my whole Page.
In News I'm only inserting main content and title.
Problem was in Page class which I was extending with Controller and that was bad idea.
I forgot that in CodeIgniter manual is written:
Quote:$this, however, only works directly within your controllers, your models, or your views. If you would like to use CodeIgniter's classes from within your own custom classes you can do so as follows:

First, assign the CodeIgniter object to a variable:
$CI =& get_instance();
And that was my problem.
You are saying that this code belongs to a model.
It isn't my first experience with MVC but you are maybe right that I'm a little confused.
Can you tell me then where this code belongs and how should I use it ?

And if it comes to manual and video tutorials I've passed trough them before I've started to use CodeIgniter Tongue

#4
[eluser]pr0digy[/eluser]
What threw me off, was the fact that you made DB calls within the controller (it's permissible but not advisable) and then assembled an unordered list within the same method (which should be done by the view). Essentially you're not using Models and using Views somewhat incorrectly.

Ok, here is how it should be... Smile

Model code:
Code:
public function getItems($lang){
    $sql = "SELECT * FROM news WHERE lang = '$lang' ORDER BY n_timestamp";
        $res = $this->db->query();
        if($res->num_rows() > 0){
            return $res->result_array();
    } else {
        return array();
    }
}

Controller code:
Code:
public function index(){
    $this->load->model('Your_model_name', 'users');
    $this->lang = $this->lang->user_lang;
    $items = $this->users->getItems($this->lang);
    
    $this->page->set_vars($items);
        $this->page->show();
}

View code:
Code:
<div id="news">
    <div class="title"><=$row['n_title'];?&gt;</div>
    <div class="date">&lt;?=date('H:i d.m.Y', strtotime($row['n_timestamp']));?&gt;</div>
    <div class="content">&lt;?=nl2br($row['n_content']);?&gt;</div>
</div>

This really is an approximation, but I hope you get the general idea.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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