this was one of our seatworks earlier at school. I don't know php that much and I want to know why my model isn't connecting with my controller.
Controller
class Main extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
}
public function login()
{
/* Display the login page */
$this->load->view("form");
$this->user_model->get_all_users;
}
public function validate()
{
/* Validate the login details passed via POST parameter.
If successful, create session and redirect to people list page.
If failure, redirect to login page. */
if (isset($_POST["name"], $_POST["password"])) {
$name = $_POST["name"];
$password = $_POST["password"];
}
Model
class user_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->database();
}
public function get_all_users() {
$res = $this->db->from('account');
if ($res) {
return $res->result_array();
}
return null;
}
public function get_user($id) {
$res = $this->db->from("account")->where("id", $id)->get();
if ($res) {
return $res->result_array();
}
return null;
}
}
just like when I use user_model in the controller, It also says that result_array not found in the model. Assuming my database is configured correctly. Can anyone help me with this?
db settings
$db['default'] = array(
'dsn' => '',
'hostname' => '127.0.0.1',
'username' => 'root',
'password' => 'correctpassword',
'database' => 'challenge',
'dbdriver' => 'mysqli',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => (ENVIRONMENT !== 'production'),
'cache_on' => FALSE,
'cachedir' => '',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'encrypt' => FALSE,
'compress' => FALSE,
'stricton' => FALSE,
'failover' => array(),
'save_queries' => TRUE
);
You should give and call to you model method.
Use this
$this->user_model->get_all_users();
Instead of
$this->user_model->get_all_users;
Every item of database
foreach ($query->result() as $row)
{
echo $row->title;
echo $row->name;
echo $row->body;
}
In your model use get
instead of from
$res = $this->db->get('account');
if you want to use any of CI's helpers, models or other libraries, you need to do this through the CI instance. This is achieved by doing this:
public function __construct()
{
$this->CI =& get_instance();
}
You should call your model method in this way.
$this->user_model->get_all_users();
and make sure you make autoload your model in library
Generally if your working in codeignitor make sure your file name and Class Name are same , it is good practice if you keep the First letter to be capital of controller file and model file , as it might create a problem while running on actual server .
For your problem please do these changes and check and if you have still error please share screenshot in comment
Controller File
public function __construct()
{
parent::__construct();
$this->load->model("user_model", "um");
}
Model File
Make sure you make Capital First letter of model file and class Name
public function get_all_users() {
$res = $this->db->select('*')
->from('account')
->get();
if ($res) {
return $res->result_array();
}
return null;
}