codeigniter- Call to undefined function getNombre() - php

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

Related

CodeIgniter: undefined function [duplicate]

This question already has answers here:
Fatal error: Call to undefined function form_open() in c
(5 answers)
Closed 5 years ago.
Hello I get this error when I try to run my code, I'm just trying to test it out and I got this error, I'm trying to create a account registration and I'm required to use Codeigniter and I saw a similar code in the net and I try to follow it but somehow I got error with this and I don't know what I'm missing sorry I'm new to Codeigniter
Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\codeigniter_project\application\views\welcome_message.php on line 9
A PHP Error was encountered
Severity: Error
Message: Call to undefined function form_open()
Filename: views/welcome_message.php
Line Number: 9
Backtrace:
Here is my code for view
<html>
<head>
<title>Create Account</title>
</head>
<body>
<h3>Create Account</h3>
<?php
echo form_open(CreateAccount_Controller,submit_data);
echo form_label('User Name:', 'Vu_name' );
$data= array(
'name' => 'u_name',
'placeholder' => 'Please Enter User Name',
'class' => 'input_box'
);
echo form_input($data);
?>
</body>
</html>
Here is my code for controller
<?php
class CreateAccount_Controller extends CI_Controller
{
public function _construct ()
{
parent::_construct();
}
public function create_account ()
{
$this->load->view(CreateAccount_View);
}
public function submit_data ()
{
// return hi or something;
}
}
?>
Load the helper in your controller constructor and Your code will be as follows:
<?php
class CreateAccount_Controller extends CI_Controller
{
public function _construct ()
{
parent::_construct();
$this->load->helper('form');
}
public function create_account ()
{
$this->load->view(CreateAccount_View);
}
public function submit_data ()
{
// return hi or something;
}
}
?>
You have forgotten to load form helpers in your code.
Please load the helper in autoload.php under application/config
$autoload['helper'] = array('helper');
OR
Load the helper in your controller constructor.
<?php
class CreateAccount_Controller extends CI_Controller
{
public function _construct ()
{
parent::_construct();
$this->load->helper('form');
}
public function create_account ()
{
$this->load->view(CreateAccount_View);
}
public function submit_data ()
{
// return hi or something;
}
}
?>
Hope this helps you.

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

How do I do $this->model->getList() in php

I am working on i MVC and i've gone through some project to see how works but, when i do something like $this->model->getList() to get a list of items from my DB in my controller i get this error message
Notice: Undefined property: Managemenu::$model in C:\wamp\www\mymvc\Admin\controllers\managemenu.php on line 10
Fatal error: Call to a member function getList() on a non-object in C:\wamp\www\mymvc\Admin\controllers\managemenu.php on line 10
Actually this is my code on my managemenu model
<?php class Managemenu_Model extends Model{
function __construct(){
parent::__construct();
}
public function getList(){
$menus = Menu::find_all();
return $menus;
}
public function edit(){
}
public function update(){
}
public function save(){
}
public function delete(){
}
}?>
and this is what my controller is like
<?php class Managemenu extends Controller{
public $mymenus = null;
function __construct(){
parent::__construct();
$this->index();
}
public function index(){
#$this->loadModel("managemenu");
$this->mymenus = $this->model->getList();
$this->view->render("managemenu/index");
}
public function doList(){
}}?>
Also, I want to get the list of my menus on the front end by looping through the $mymenus into a table if i do foreach($this->mymenus as....) i keep on having the same error in my view template too
can anybody give me a hand. i don't know why that error comes up

codeigniter method error

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->...

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