Controller doesn't load, no errors from Apache or CodeIgniter - php

I'm sure this is a simple mistake on my part. I'm trying to demo ci to a friend and we created a simple controller that looks like this:
<?php
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}
And here's what the model looks like:
<?php
class Customerlookup_model extends CI_Model
{
public function __construct()
{
parent::Model();
$this->load->database();
}
public function get_customers()
{
$query = $this->db->get('customer');
return $query->result_array();
}
}
If I try to test this out by doing either:
localhost/myapp/index.php/customerlookup/loadcustomers
or
localhost/myapp/index.php/customerlookup/
nothing happens and no errors appear and no data or messages either. We are using the latest CodeIgniter (2.1.3).
Any suggestion?

i found the problem. php errors were not turned on. there were some syntax problems. checked the apache error log and figured out that the errors were just not being displayed
thanks for the help guys

you are missing with your url helper
$this->load->helper('url');
into your
class Customerlookup extends CI_Controller {
//constructor for this class
public function __construct()
{
parent::__construct();
$this->load->model('customerlookup_model');
$this->load->helper('url');
}
public function index()
{
echo 'test';
}
public function loadcustomers()
{
$data['cust'] = this->customerlookup_model->get_customers();
$this->load->view('customerlookup',$data);
}
}

Related

Unable to locate the specified class: Session.php, When call a function from another controller in Codeigniter

contrller:News.php
This is my controller News
<?php class News extends CI_Controller {
public function __construct()
{
}
public function getShowIN_News()
{
return $result;
} } ?>
contrller:Category.php
This is my controller Category
<?php class Category extends CI_Controller {
public function __construct()
{
}
public function category()
{
require('news.php');
$test = new News();
$data["headlines"] = $test->getShowIN_News();
} }?>
By using an empty constructor, you're making it so that CI_Controller::__construct() isn't called, and that's where everything in the framework is initialized.
I know you've put it there to hack it so you can call one controller from another, but it is very intentionally made that way, exactly so you don't do this.

Extending models in codeigniter 3

What I'm trying to do on the surface seems simple, basic OOP PHP but I just can't get it working. I have a controller class which is calling a model, that model extends another model of mine but it throws an error saying it can't find it:
Controller (Welcome.php):
public function __construct()
{
parent::__construct();
$this->load->model('users_model');
}
public function index()
{
$this->users_model->getAll();
}
Users Model (User_model.php):
class Users_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Base Model (Base_model.php):
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table)
{
$query = $this->db->query('Query here');
return $query;
}
}
This gives me the error Fatal error: Class 'base_model' not found in /ci/application/models/Users_model.php on line 3
Save your Base_model in application/core named as Base_model.php with following code.
class Base_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
public function getAll($table=FALSE)
{
$query = $this->db->query('Query here');
return $query;
}
}
Save User_model in application/models named as User_model.php having following code
class User_model extends Base_model
{
public function __construct()
{
parent::__construct();
}
}
Then make a controller Welcome.php in appliation/controllers having following code with extending CI_Controller
public function __construct()
{
parent::__construct();
$this->load->model('user_model');//loads user_model
}
public function index()
{
$data = $this->user_model->getAll(); //need a variable to hold return data
}
You just have to locate the Base_model in your Users_model like below code and you can access the functions of base model easily.
require('application/models/base_model.php');
class User_model extends Base_model
{
function __construct()
{
parent::__construct();
}
}

Can anyone tell me why there is an error?

I am having error in my code, this is not working properly can anyone tell me where is the problem?
<?php
class Blog extends CI_controller {
public function jay()
{
$this->load->model('abc');
$this->load->getdata();
print_r($data);
$this->load->view("page1.php");
}
/*public function wel()
{
$this->load->view('Welcome_message');
} */
}
?>
If the method getdata(); is inside your model you can not access it by using
$this->load function.
you can access it by $this->abc->getdata();

CodeIgniter load database in Controller or Model?

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');
}
}

CodeIgniter - constructors in models

So here is my controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->search_model->search_result = $_POST;
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
And here is my model:
class Search_model extends CI_Model {
protected $search_query;
function __construct($search_query)
{
parent::__construct();
$this->load->database();
$this->search_query = $search_query;
}
But this doesn't seem to work. What I want to do is pass the posted form ($_POST) to my model, then do stuff with it. But it seems messy to pass $_POST to each method of my model. My plan is to extract the variables sent with $_POST and construct these as properties such as $website_url, $text_query etc..., then call these in methods with $this->website_url;
I'm relatively new to CodeIgniter so just getting to grips with the basics
for your special purpose you can try this code
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
$this->init();
}
private function init()
{
$this->search_model->init( $this->input->post() );
}
public function index()
{
$data['results'] = $this->search_model->get_results();
$this->load->view('search_results', $data);
}
model:
class Search_model extends CI_Model {
protected $search_query;
function __construct()
{
parent::__construct();
$this->load->database();
}
public function init( $search_query )
{
$this->search_query = $search_query;
}
you have protected $search_query; which you can't access it from your controller.
You either have to change it to public or create getter and setter for it. or just getter depending on your domain/business logic.
And it should have been obvious as you should get an error saying
Fatal error: Cannot access protected property in file some/path/to/file!
Don't put the 'search query' in your model constructor.
Controller:
class Search extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('search_model');
}
public function index()
{
if ($this->input->server('REQUEST_METHOD') == 'POST')
{
// you should probably validate/clean the post data instead
// of sending it straight to the model
$results = $this->search_model->get_where($_POST);
}
else
{
// if it's not a post, you probably need something...
// either here, or somewhere in your view to handle empty data
$results = array();
}
$data['results'] = $results
$this->load->view('search_results', $data);
}
Your Model:
class Search_model extends CI_Model {
function __construct()
{
parent::__construct();
$this->load->database(); // <--- you may want to autoload 'database' library
}
function get_where($where)
{
$this->db->where($where);
// add select, order, joins, etc here
return $this->db->get('mytable'); // whatever your table name is
}

Categories