I am new to CodeIgniter and experiencing the following error after connecting the db . It is really great if some one can help me. I checked previous answers in the StackOverflow but none helped me.
Below is the controller code
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends MY_Controller {
public function index() {
$this->load->view('home');
}
public function adminRegister() {
//loading teh queries model
$this->load->model('queries');
//Calling the getRoles function inside the roles
$roles = $this->queries->getRoles();
print_r($roles);
exit();
$this->load->view('register');
}
public function login() {
echo 'login';
}
}
Model code
<?php
//Details of queries whicha re used to interact with teh database
class Queries extends CI_Model {
//Created a function with the name getRoles
public function getRoles() {
//Fetch the reocrds from the table
$roles = $this->db->get('tbl_roles');
if ($roles->num_rows() > 0) {
return $roles->results();
}
}
}
?>
Currently not using the data coming from controller. Any help will be highly appreciated. Thanks in advance.
There is no results it should be result
Refer CI Doc
class Queries extends CI_Model {
//Created a function with the name getRoles
public function getRoles() {
//Fetch the reocrds from the table
$roles = $this->db->get('tbl_roles');
if ($roles->num_rows() > 0) {
return $roles->result();
}
}
}
Related
Model :
class Users_model extends CI_Model {
function __construct() {
parent::__construct();
$this->load->database();
}
public function get_all_users() {
return $this->db->get('users');
}
}
Controller :
class Users extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
$this->load->helper('security');
$this->load->model('Users_model');
}
public function index() {
redirect('users/view_users');
}
public function view_users() {
$data['query'] = $this->Users_model->get_all_users();
$this->load->view('users/view_all_users', $data);
}
}
My question is where should i put the $this->load->database? In Model or Constructor? If possible tell me why?
And one more question, if i omit the $this->load->database, the error shown
"Undefined property: Users::$db". I'm expecting "Undefined property:
Users_model::$db".
Why is that? Is it looking for $db in both controller or model?
Thank you.
Note: i can connect to database just fine. What im actually ask is if i want to use $this->load->database(). Where should i put it? Controller or Model? And why?
You can either load the database in the controller or you can load it in the model.There's not much of a difference its just more neat and clean that all the interaction in the database is in the model and the controllers the one who connects both views and model.The controller here is like a middleman between the buyer and the seller.
load database in the controller
<?php
class Test_controller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->database('default');
$this->load->model('test_model');
}
public function index()
{
$this->db->from('test');
$query = $this->db->get();
echo "<title>CodeIgniter SQlite</title>";
$data['db']=$query->result();
//print_r($data['db']);
$count = count($data['db']);
echo "Row count: $count rows<br>";
for ($i=0; $i < $count; $i++) {
echo $data['db'][$i]->text." ";
}
}
}
?>
load database in the model
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Test_model extends CI_Model
{
public $table1 = 'test';
public $table2 = '';
public function __construct()
{
parent::__construct();
$this->load->database('default');
}
public function check_db()
{
$this->db->from($this->table1);
$query = $this->db->get();
return $query->result();
}
}
Go to autoload.php in application/config/autoload.php and add this
$autoload['libraries'] = array('database'); // add database in array(now you dont need to load database at anywhere in project)
Make database connection settings in database.php, file located atapplication/config/database.php
now try this
class Users_model extends CI_Model {
function __construct() {
parent::__construct();
//$this->load->database(); <----remove this
}
public function get_all_users() {
return $this->db->get('users');
}
}
Firstly I am getting no errors, I am trying to create an is_logged_in() method in my header model in Code Igniter, but nothing in the index method of the controller will load. I added die(); into it and even that wont execute, Here is my code:
header.php - controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header extends CI_Controller {
public function index() {
print_r($this->session->all_userdata());
$data = array();
$data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
header_model.php - Model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Header_model extends CI_Model {
public function is_logged_in($username){
$q = $this
->db
->where('email_address', $username)
->limit(1)
->get('users');
die($q->last_query());
if($q->row('username') != $username){
return FALSE;
} else {
return TRUE;
}
}
}
Note: none of the die() functions in my code work.. Anything I add into the index function of the controller (which to my understanding is loaded by default) does not get executed...
Thanks in advance
public function __construct() { parent::__construct(); }
Add this method at your model else you wont have $this->db loaded
As AdrienXL pointed out the controller is only loaded whern the url /controller_name is called.. This wasn;t the case in my user case scenario.
Also something worth pointing out as Sevtilo mentioned above if you create a construct method in CodeIgniter you ovewrite the dafult calls for things such as $this->db class, using:
public function __construct() {
parent::__contsruct();
}
Will get the parent classes contsructor.
Regards
Ric
If you want to call this code transparently (ie without having to put any extra mess in the uri) then move the code into the constructor of an extension called MY_Controller.php in application/core that looks a bit like this...
class MY_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
print_r($this->session->all_userdata());
$data = array(); $data['title'] = 'Wenso - Timesheet';
$username = $this->session->userdata('username');
$this->load->view('template/header', $data);
$this->load->model('header_model');
$is_logged_in = $this->header_model->is_logged_in($username);
die($is_logged_in);
}
}
And then in your application/controllers files extend this class like
class Some_controller extends MY_Controller{
function __construct (){
parent::__construct();
}
public function index(){
//your header code will be run before this or any other method in this class
}
}
And the code from MY_Controller.php will run before any of your methods.
This is the code for my controller:
class products extends CI_Controller {
public function index() {
$this->load->model('getproducts');
$this->load->view('productlist');
}
}
?>
When I load this, it returns a blank page, however when I comment out the line that loads the model, it returns the view as expected (pointing to an error in the model). Here is the code for the model:
<?php
class getproducts extends Model {
function listProducts() {
$query = $this->db->get('ProjectProducts');
if ($query->num_rows() > 0) {
foreach ($query->result() as $row {
$productdata[] = $row;
}
return $productdata;
}
}
}
?>
Am I missing something obvious here? I'm not even using any of the data from the model yet.
What version you use of codeigniter
please note that you should extends CI_Model not model
class getproducts extends CI_Model {
Please make sure your controller start letter be capital ie. upper
case and extend it as CI_Model
you are missing
function __construct()
{
parent::__construct();
}
and this code before your function listProducts and after class starting hopefully this will resolve your issue.
and also use same function in your controller as well
Ok I have a simple question I am using codeigniter frame work to create a simple blog. When I set up just a controller and a view I can print my database information (blog roll) to my view just fine. When I use the model controller view method i fail.
Here is what I would like to implement in to a method controller view setup.
my original view that works:
<?php
//is there an array from your search form?
if($_GET){
$books = $this->db->get('blog');//query the blog table in the database
if($books->num_rows() < 1)//are there clients to show?
{
echo 'There are no blog post'; //error message if no post
}
else
{
foreach(result() as $row)//loop through the blog
{
echo $row->title."<br>";//display each titles info
}
}
}
?>
This is what i have set for my New model
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog_Model extends CI_Model {
function get($args = null)
{
$query = $this->db->get('blog');
return $query->result();
foreach(result() as $row)//loop through the books
}
function insert($data)
{
$this->db->insert('blog', $data);
}
function update($data,$id)
{
$this->db->where("id",$id);
$this->db->update('blog', $data);
}
function delete($id)
{
$this->db->where("id",$id);
$this->db->delete('blog');
}
}
this is my new controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Blog extends CI_Controller {
public function index()
{
$this->load->model('blog_model', 'blog');
$data['blogs'] = $this->blog->get();
$this->load->view('blog',$data);
}
}
I am not sure what to do for my new view? I just want to echo the blog roll to the view
Well you´re doing a foreach in the model after the return function.
So what you return is just this:
$query->result()
You may should do the foreach in the view, this would be better placed as a model just should just return and not process information. Best place would be in this case the controller or may view - depending on how strict you are.
I did not work with CodeIgniter for a while so may try this:
Controller:
class Blog extends CI_Controller
{
public function index()
{
$this->load->model('blog_model', 'blog');
$data['blogs'] = $this->blog_model->get()->result();
$this->load->view('blog',$data);
}
}
The View
Here goes some text...
<?php
foreach($blogs as $post)
{
echo $post['someData'];
echo $post['someData2'];
}
?>
After all this code...
May you want to lookup this (CodeIgniter Doc).
Hope this helps. Try to
Controller:
$data['blogs'] = $this->blog_model->get();
Even though you load the model in order to call its function you must pass its name.
Model:
Must always have result() or row() when query are applied.
Hope this helps in your endeavors
I've just started learning Code Igniter and I've run in to a problem almost straight away.
Using the code below and a database setup with the correct privileges I get a 500 error (server error). CI is not throwing an error and I can't see anything wrong with this code.
I'm using the latest version of CI and testing this code:
Controller: products.php
class Products extends CI_Controller {
public function index() {
$this->load->model('products');
$this->load->view('products');
}
}
Model: products.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
I've put these classes in the directories they should be in with a filename of the same name as the class....if I comment out the load-model code in the controller my view is shown. My database connections are correct because if I change them to something incorrect CI throws an error. There's some sort of problem with the model or the loading of it.
You have conflicting class names, the modell and the controller class both called Products.
Try this:
class Products extends CI_Controller {
public function index() {
$this->load->model('products_model','products');
$this->load->view('products');
}
}
Model: products_model.php
class Products extends CI_Model {
function __construct() {
parent::__construct();
}
function get_products() {
$q = $this->db->get('product');
if($q->num_rows() > 0) {
foreach ($q->result() as $row) {
$data[] = $row;
}
return $data;
}
}
}
A good convention to use is to name your controller ProductsController and then leave the model Products. So all controllers are SomethingController.
Example:
class ProductsController extends CI_Controller
where the filename become products_controller.php
and the model remains:
class Products extends CI_model
and remains products.php
Now your code should work.