Codeigniter function undefined error - codeigniter

My custom core Class in application/core/MY_Controller.php
class MY_Controller extends CI_Controller{
protected $data = array();
function __construct(){
parent::__construct();
}
function rander_page($view){
//do this to don't repeate in every controller
$this->load->view('includes/header');
$this->load->view('top_menus');
$this->load->view($view, $this->data);
$this->load->view('includes/footer');
}
}
Index Controller in application/controllers
class Index extends MY_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->render_page('index');
}
}
Error is :Fatal error: Call to undefined method Index::render_page() in D:\wamp\www\ci\application\controllers\index.php on line 10
i am trying to use one controller for all pages help plz

Yes, the error is the clue itself, it has mentioned the function is undefined. You have not defined render_page function. Instead, you misspelled and named it as rander_page().
May be you are trying to write:
function render_page()
as you have called it like :
$this->render_page('index');

You have misspelled the function name. Call $this->rander_page('index');

Related

CI_Template' not found

I want to load model using
$this->load->model('apotek_data');
function __construct()
{
parent::__construct();
$this->load->model('apotek_data');
$this->template->write_view('sidenavs', 'template/default_sidenavs', true);
$this->template->write_view('navs', 'template/default_topnavs.php', true);
$this->load->database();
}
But when I put it ($this->load->model) in my code, the view doesn't show, and display an error message.
Fatal error: Class 'CI_Template' not found in
D:\wamp64\www\apotek\system\core\Common.php on line 196
What should I do?
Load the template library:
$this->load->library('template');
OR:
In config/autoload.php add template in the libraries array.
I don't know the name of your library so I am assuming it is template if it is not - change it with the name of the library.
For any other person experiencing a 'CI_Template not found' error:
In my case, I was loading a library (in the constructor) with the same name as the controller that was being accessed.
class Authld extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
I had to change the name of the controller for the issue to be resolved.
class Authentication extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->library('Authld');
}
}
Your Template.php maybe looks like this:
class Template extends MX_Controller{
If so, change the MX to CI (CI_Controller).

Codeignite 404 error

I ham writing the follow Codeigniter code, but I keep getting a 404 error. Any thoughts?
funding_controller.php
class Funding extends CI_Controller {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function index()
{
$this->load->model('funding_model');
$data['funds'] = $this->funding_model->getAll();
$this->load->view('funding_list', $data);
}
}
funding_model.php
class Funding extends CI_Model {
function __construct()
{
// Call the Model constructor
parent::__construct();
}
function getAll() {
$query = $this->db->get('funds');
return $query->result();
}
}
funding_list.php
foreach($funds as $row) {
echo $row->opportunity_name;
echo "<br />";
}
The default route points to funding_controller. Thoughts?
controller name and class name should be same and file name in lower case
funding.php
And try following url
http://domain.com/funding/index
Model class name different from controller name like funding_model or anything else and no need to suffix _model in ->load->model()
Use first letter in capital for controller n model file name.
For example:
Founding_controller.php
Founding_model.php
And class name should be filename (as it is).

CI Bonfire: Cannot call methods on Model

I am new to BF. I am learning BF by following the tutorial here, but I cant figure out why it keep prompt me error message as below:
Fatal error: Call to a member function where() on a non-object in ...
Here is the code:
class Content extends Admin_Controller {
public function __construct(){
parent::__construct();
Template::set('toolbar_title', 'Manage Your Blog');
}
public function index(){
$posts = $this->post_model->where('deleted', 0)->find_all();
Template::set('posts', $posts);
Template::render();
}
}
Can someone guide me on this? Thanks
bborisovs is right, you need to load the model in the constructor, before you can use the obgject:
$this->load->model('post_model', null, true);
Also make sure your model exists.

HMVC Module Error - Undefined property: model

So I'm getting the error: Undefined property: badge_progress::$bp_model.
I don't understand what's going on. Here is my code:
Controller:
<?php
// Badge Progress Module
class badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
?>
Model:
<?php
class bp_model extends CI_Model {
function dotpoints($badge_id) {
$query = $this->db->query("SELECT * FROM course_topic_dotpoints WHERE badge_id = ".$badge_id);
if ($query->num_rows() > 0) {
return $query->result();
}
}
}
?>
Ah fixed it! Didn't realise that the main controllers (controllers outside of the module directory) also needed to be extending "MX_Controller" instead of "CI_Controller".
Class names must begin with an uppercase letter.
class Badge_progress extends...
class Bp_model extends...
http://codeigniter.com/user_guide/general/controllers.html
http://codeigniter.com/user_guide/general/models.html
update:
You shouldn't have the logic you need as a function in your constructor. Create a separate function to process the dotpoints stuff.
<?php
// Badge Progress Module
class Badge_progress extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->model('bp_model');
}
function dotpoints()
{
$data['dotpoints'] = $this->bp_model->dotpoints('1');
$this->load->view('bp_view', $data);
}
}
Also, you are missing the constructor in your model. Check out those links I posted earlier...

How to Inherit A Model from Another Model in CodeIgniter

i'm using codeigniter for my project and i have this class model which i call Genesis which looks like this:
class Genesis_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get() {
return 'human soul';
}
}
and i have another model, stored in the same directory, which extends Genesis_model
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return $this->get();
}
}
Human_model is used by Human controller
class Human extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('human_model');
}
function get_human() {
$data['human'] = $this->human_model->get_human();
$this->load->view('human/human_interface', $data);
}
}
if i execute the code, it will produce an error which point to return $this->get(). it reads "Fatal error: Class 'Genesis_model' not found in ...\application\models\human_model.php on line 2".
i use this method because nearly all my models shared almost identical structure. I gather the similar functionality in Genesis while the other models serve only as data suppliers unique to the tables they represent. it works well in my asp.net (vb.net) but i don't how to do it in codeigniter.
is there a way for Human_model to inherit Genesis_model. i don't think i'm allowed to use include('genesis_model.php'). i don't know if it works either.
thanks in advance.
core/MY_Model is good if there's only 1 important superclass for your models.
If you want to inherit from more than model superclass, a better option is to change your autoload configuration.
In application/config/autoload.php, add this line:
$autoload['model'] = array('genesis_model');
Put the file genesis_model.php in the core directory
Change your Human_model to this:
include('genesis_model.php');
class Human_model extends Genesis_model {
function __construct() {
parent::__construct();
}
function get_human() {
return parent::get();
}
}
notice the get_human function and the include.
You have to include the Genesis_model on your Human_model.php like this:
include_once( APPPATH . 'folder/file' . EXT );
Or you can autoload it on your config/autoload.php file, what I think is stupid =)
other solution
<?php
$obj = &get_instance();
$obj->load->model('parentModel');
class childModel extends parentModel{
public function __construct(){
parent::__construct();
}
public function get(){
return 'child';
}
}
?>

Resources