codeigniter method error - php

I'm using the following from the controller to call a method from the model but receiving and error:
//from the controller:(main.php)
<?php
class Main extends CI_Controller {
public function __construct() {
parent::__construct();
}
function index() {
.....
$this->load->view('view_form');
}//END Fn index()
function get_th() {
//$the=$this->input->post('th', TRUE);
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}//END Fn get_th()
}//END Cls Main
?>
//from the model:(model_data.php)
<?php
class Model_data extends CI_Model {
function slider() {
...
}//END Fn slider()
function check_input($data) {
...
}//END Fn check_input()
function tst() {
$tsts= "hellos";
return $this->tsts;
}
}//END Cls model_data
?>
$autoload['model'] = array('model_data');
The error:
Fatal error: Call to undefined method Model_data::tst() in ... application\controllers\main.php...

i think you forgot to load the model in the controller.
$this->load->model('Model_name');
function get_th() {
$this->load->model('model_data');
$d['shit']=$this->model_data->tst();
$this->load->view('view_form',$d);
}

FIXED :/ can t believe I had an additional bracket at the the end of a long file :( wtf
"}"<-- this was the problem.
btw as I said #pramodhkumar use autoload.php.. I had the model autoload so no need for $this->load->...

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.

Codeigniter, problems with extending classes

I have the following simplistic code:
// FILE: controllers/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function Top() {
echo 'Hello';
}
}
// FILE: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
// FILE: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
And I get the following the following error:
Fatal error: Class 'MY_Public_Controller' not found in
/var/www/example.com/public_html/application/controllers/Top.php on line 5
A PHP Error was encountered
Severity: Error
Message: Class 'MY_Public_Controller' not found
Filename: controllers/Top.php
Line Number: 5
Backtrace:
Any help would be much appreciated!
Instead of you create a new file (MY_Public_Controller.php) to create the class My_Public_Controller.
Insert this class inside the My_Controller.php file.
In that way the My_Controller.php file will be like:
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
}
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
After I see another answer
Or you can make something like #Hikmat Sijapati said, but instead of you put the require_once, inside the My_Controller.php. Try to put it in the My_Public_Controller.php using 'My_Controller.php' as parameter. Something like that:
My_Public_Controller.php:
include_once('My_Controller.php');
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
I have not tried it that way, but I think it will work.
Try like this...
You can create any number of controller but create controller's must be included in the controller that extends CI_Controller.As Below:
Controller's Name and function Name Keep different (Good Way)
MY_Controller:application/core/MY_Controller.php
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
}
include_once('MY_Public_Controller.php');// include here
}
MY_Public_Controller: application/core/MY_Public_Controller.php
class MY_Public_Controller extends MY_Controller {
function __construct() {
parent::__construct();
}
}
And Top: application/Top.php
class Top extends MY_Public_Controller {
function __construct() {
}
public function index() { //function name must be different than controller's name
echo 'Hello';
}
}

codeigniter- Call to undefined function getNombre()

can someone help me with this?. It gives me following Fatal Error:
Fatal error: Call to undefined function getNombre() in C:\xampp\htdocs\CodeIgniter\application\views\vista.php on line 6
this is my controller(c1.php)
<?php
class c1 extends CI_Controller{
function _construct() {
parent::__construct();
$this->load->helper('mihelper');
}
function index(){
$this->load->view('headers');
$this->load->view('vista');
}
}
?>
this is in my helper(mihelper_helper.php)
<?php
function getNombre(){
return "<h1>Arturo</h1>";
}
?>
this is my view(vista.php)
<body>
<h1>Llamado desde controlador</h1>
<?php getNombre() ?>
</body>
</html>
You missed a underscore in the controller's construct function. So the helper is not able to load and the function is undefined. Updated code as below,
<?php
class c1 extends CI_Controller{
function __construct() {
parent::__construct();
$this->load->helper('mihelper');
}
function index(){
$this->load->view('headers');
$this->load->view('vista');
}
}
?>

Codeigniter calling model method from another model causes error

I have a controller:
class Blah extends Controller
{
function Blah()
{
$this->load->model('baga_model');
}
}
then comes baga_model:
class Baga_model extends Model
{
function do_it()
{
echo "BOOM!";
}
}
..and
class Blah_model extends Model
{
function some_action()
{
$this->baga_model->do_it();
}
}
So .. when in blah_model I call $this->baga_model->do_it() I get an error :
Call to a member function do_it() on a non-object
I just can't understand why.... I know it must work, I did something similar before..
Thanks
Got it! I had to load baga_model in blah_model constructor. This way it works.
Thanks everyone.
public function test()
{
$this->load->model('baga_model');
$this->baga_model->do_it();
}
Model
class baga_model extends CI_Model
{
public function do_it()
{
echo $this->bar("BOOM!");
}
Your not loading your required model inside your model:
class Blah_model extends CI_Model
{
$this->baga_model = $this->load->model('baga_model', true);
public function some_action()
{
$this->baga_model->do_it();
}
}

why I'm getting function get() on a non-object in error when using codeigniter?

index_model.php is below:
<?php
class index_model extends CI_Model {
function __construct() {
parent::__construct();
}
function getVideo()
{
$query = $this->db->get('videolar');
return $query->result_array();
}
}
?>
And index controller is below, too
<?php
class index extends CI_Controller {
function __construct() {
parent::__construct();
}
function index()
{
$this->load->model('index_model');
$data['video'] = $this->index_model->getVideo();
$this->load->view('index',$data);
}
}
?>
When I call index controller it returns this error
Fatal error: Call to a member function get() on a non-object in
/var/www/atlet/application/models/index_model.php on line 10
I set database in autoload.php.
$autoload['packages'] = array('database');
You need to load the database in the libraries array, not the packages array:
$autoload['libraries'] = array('database');

Categories