Welcome Guest, Not a member yet? Register   Sign In
Fatal error: Call to a member function query() on a non-object
#1

[eluser]rochellecanale14[/eluser]
Hello today i am integrating CI and Smarty and I got an error in database query. Here's what i did.

Model:
//admin_model.php
<?php

class Admin_Model extends CI_Model{

public function __construct(){
parent::__construct();
}

public function viewAllMember(){
$sql = "SELECT * FROM tbl_admin";
$result = $this->db->query($sql); //the error?
return $result;
}

}

?>

Controller:
//admin_controller.php
<?php

class Admin_Controller extends CI_Controller {

public function __construct() {
parent::__construct();
}

public function index() {

$this->smarty->assign('title','Account Setting');

$list = $this->admin_model->viewAllMember(); //error?

foreach($list->result_array() as $row){
$id = $row['id'];
//$username = $row['username'];
}

$this->smarty->assign('id',$id); //Use for testing purposes only

$this->smarty->view('admin/index');

}

}

?>

Smarty template
//index.tpl

<html>
<head>
<title>TEST</title>
</head>
<body>
{$id}
</body>
</html>

Could you tell me where's my error? And how can i display all my tables data? where should i extract it? In controller or view? I am now studying smarty together with codeigniter.
#2

[eluser]Ckirk[/eluser]
That error sounds like haven't got the database library loaded.
Check your autoload config file "application/config/autoload.php"
There should be a line similar to this:
Code:
$autoload['libraries'] = array('database');

After that you should see no error when you call $this->db->query()


As for your controller I'm not sure if you understand how loops work. I've put notes against the following extract to help you see what's going on
Code:
public function index() {
        
        $this->smarty->assign(‘title’,‘Account Setting’);
        
        $list = $this->admin_model->viewAllMember();  //error?
        
        // Assuming that $list has records your now going to loop through each row and redefine $id each time
        foreach($list->result_array() as $row){
          $id = $row[‘id’];
          //$username = $row[‘username’];
        }
        // $id is now equal to the last $row['id'] in the loop (in other words you now only have one record id)
        
        $this->smarty->assign(‘id’,$id); //Use for testing purposes only
        
        $this->smarty->view(‘admin/index’);

      }

I haven't used Smarty but I'm guessing that you will need to generate the HTML output for your records and pass that into your smarty template. Unless of course you can run PHP in the smarty template. If that's the case then you loop through your $list->result_array() in the template.

Hope that helps
#3

[eluser]rochellecanale14[/eluser]
ok thanks.. you're right i forgot to include the database library. But when i include it nothings happened only white screen shows.
#4

[eluser]Ckirk[/eluser]
first thing to do is echo out the value of $id to make sure you have something to show.
If you do then it's a Smarty problem and beyond the scope of my knowledge here.
#5

[eluser]CroNiX[/eluser]
Make sure your error reporting is turned on and check the logs. In PHP, a white screen usually means there is an error, but you have suppressed the output so you don't see it and only get a blank page.




Theme © iAndrew 2016 - Forum software by © MyBB