• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
new with codeigniter

#1
[eluser]kiga[/eluser]
hi, i'm new with codeigniter, recently i installed it on a wamo server, when i try to run a controller i found shows this


( ! ) Fatal error: Call to undefined method CI_Controller::CI_Controller() in C:\wamp\www\CodeIgniter\application\controllers\formulario.php on line 11
Call Stack
# Time Memory Function Location
1 0.0007 385880 {main}( ) ..\index.php:0
2 0.0022 454368 require_once( 'C:\wamp\www\CodeIgniter\system\core\CodeIgniter.php' ) ..\index.php:201
3 0.0670 1350176 Formulario->__construct( )

the code i try is
Code:
<?php

/**
*
*/
class Formulario extends CI_Controller
{
    
    function __construct()
    {
        parent::CI_Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
    }
    
    function index(){
        $this->form_validation->set_rules('nombre', 'nombre', 'required|trim|min_length[5]');
        $this->form_validation->set_rules('email', 'correo electrónico', 'required|valid_email|trim');
        $this->form_validation->set_rules('password', 'contraseña', 'required|trim|md5');
        $this->form_validation->set_rules('repassword', 'reescribir contraseña', 'required|matches[password]|trim|md5');
        
        $this->form_validation->set_message('required', 'Debe introducir el campo %s');
        $this->form_validation->set_message('min_length', 'El campo %s debe ser de al menos %s carácteres');
        $this->form_validation->set_message('valid_email', 'Debe escribir una dirección de email correcta');
        $this->form_validation->set_message('matches', 'Los campos %s y %s no coinciden');
        
        if($this->form_validation->run()==FALSE){
            $this->load->view('form_view');    
        }else{
            $data['nombre'] = $this->input->post('nombre');
            $data['password'] = $this->input->post('password');
            $this->load->view('exito_view', $data);
            
        }
        
    }
}

?>

and the url i use is http://127.0.0.1/CodeIgniter/index.php/formulario

how can i fix this?

#2
[eluser]WanWizard[/eluser]
There is no parent::CI_Controller(). You need to call parent::__construct().

CI_Controller() is PHP4 syntax, used by CodeIgniter up unitl 1.7.3. As of 2.0, it uses PHP5 syntax, __construct.

#3
[eluser]kiga[/eluser]
i try again without "CI", this time nothing happens, the page is blank :S
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;

    &lt;title&gt;form_view&lt;/title&gt;
    &lt;style type="text/css" media="screen"&gt;
        label,input{
            display:block;
            padding:5px;
        }
        div.error{
            background-color:#FF8F8F;
            border: 1px solid #FF1111;
            margin-bottom:5px;
            padding:5px;
            width:400px;
        }
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php echo validation_errors('<div class="error">','</div>') ?&gt;
&lt;?php echo form_open('formulario') ?&gt;
&lt;?php echo form_label('Nombre: ', 'nombre') ?&gt;
&lt;?php echo form_input(array('name' => 'nombre', 'id' => 'nombre', 'size' => '50', 'value' => set_value('nombre'))) ?&gt;
&lt;?php echo form_label('Email:', 'email'); ?&gt;
&lt;?php echo form_input(array('name' => 'email', 'id' => 'email', 'size' => '50', 'value' => set_value('email'))); ?&gt;
&lt;?php echo form_label('Contraseña:', 'password'); ?&gt;
&lt;?php echo form_password(array('name' => 'password', 'id' => 'password', 'size' => '50')); ?&gt;
&lt;?php echo form_label('Reescribe la contraseña:', 'repassword'); ?&gt;
&lt;?php echo form_password(array('name' => 'repassword', 'id' => 'repassword', 'size' => '50')); ?&gt;
&lt;?php echo form_submit('enviar', 'Enviar') ?&gt;
&lt;?php echo form_close() ?&gt;
&lt;/body&gt;
&lt;/html&gt;

//////////////////////
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

&lt;html &gt;
&lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"/&gt;

    &lt;title&gt;exito_view&lt;/title&gt;
    &lt;style type="text/css" media="screen"&gt;
        div.exito{
            background-color:#C2FFAF;
            border:1px solid #2A7F0F;
            padding:5px;
            margin-bottom:15px;
            width:400px;
        }
    &lt;/style&gt;
&lt;/head&gt;

&lt;body&gt;
<div class="exito">
    El formulario se ha enviado correctamente<br />
    El nombre es: &lt;?php echo $nombre ?&gt;<br />
    La contraseña en md5 es: &lt;?php echo $password ?&gt;
</div>
&lt;?php echo anchor('formulario', 'Volver al formulario') ?&gt;
&lt;/body&gt;
&lt;/html&gt;

this is the complete code :S

#4
[eluser]kiga[/eluser]
alredy try it, and this was the result!

Fatal error: Class 'Controller' not found in C:\wamp\www\CodeIgniter\application\controllers\formulario.php on line 7
Call Stack
# Time Memory Function Location
1 0.0007 386248 {main}( ) ..\index.php:0
2 0.0026 454736 require_once( 'C:\wamp\www\CodeIgniter\system\core\CodeIgniter.php' ) ..\index.php:201
3 0.0344 1350552 include( 'C:\wamp\www\CodeIgniter\application\controllers\formulario.php' ) ..\CodeIgniter.php:248
Code:
&lt;?php

/**
*
*/
class Formulario extends Controller
{
    
    function __construct()
    {
        parent::Controller();
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
    }
    
    function index(){
        $this->form_validation->set_rules('nombre', 'nombre', 'required|trim|min_length[5]');
        $this->form_validation->set_rules('email', 'correo electrónico', 'required|valid_email|trim');
        $this->form_validation->set_rules('password', 'contraseña', 'required|trim|md5');
        $this->form_validation->set_rules('repassword', 'reescribir contraseña', 'required|matches[password]|trim|md5');
        
        $this->form_validation->set_message('required', 'Debe introducir el campo %s');
        $this->form_validation->set_message('min_length', 'El campo %s debe ser de al menos %s carácteres');
        $this->form_validation->set_message('valid_email', 'Debe escribir una dirección de email correcta');
        $this->form_validation->set_message('matches', 'Los campos %s y %s no coinciden');
        
        if($this->form_validation->run()==FALSE){
            $this->load->view('form_view');    
        }else{
            $data['nombre'] = $this->input->post('nombre');
            $data['password'] = $this->input->post('password');
            $this->load->view('exito_view', $data);
            
        }
        
    }
}

?&gt;


if i try something easier like
Code:
&lt;?php
class Blog extends Controller {

    public function index()
    {
        echo 'Hello World!';
    }
}
?&gt;

is the same thing


( ! ) Fatal error: Class 'Controller' not found in C:\wamp\www\CodeIgniter\application\controllers\Blog.php on line 2
Call Stack
# Time Memory Function Location
1 0.0210 385832 {main}( ) ..\index.php:0
2 0.0590 454320 require_once( 'C:\wamp\www\CodeIgniter\system\core\CodeIgniter.php' ) ..\index.php:201
3 0.1911 1335168 include( 'C:\wamp\www\CodeIgniter\application\controllers\Blog.php' ) ..\CodeIgniter.php:248

#5
[eluser]InsiteFX[/eluser]
Code:
class Formulario extends CI_Controller
{
    
    function __construct()
    {
        parent::__construct();

        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
    }

Code:
class Blog extends CI_Controller {

    public function index()
    {
        echo 'Hello World!';
    }
}

All controllers are now CI_Controller
All models are now CI_Model

Prototypes:

Controller:
Code:
Class Controller_name extends CI_Controller {

    // Not needed if your not using it to setup things!
    public function __construct()
    {
        parent::__construct();
    }
}

Model:
Code:
Class Model_name extends CI_Model {

}

InsiteFX

#6
[eluser]kiga[/eluser]
ey man thanks!!! that was really helpfull!! ... now i pass this and, i'm trying a crud example this is the code for model and controller (both attached) ..

well i follow the instructions for my sql
Quote:Database schema. run this script.
01
CREATE DATABASE crud;
02

03
USE crud;
04
CREATE TABLE tbl_person
05
(
06
id bigint auto_increment PRIMARY KEY,
07
name varchar(50),
08
gender char(1),
09
dob date
10
);
11

12
INSERT tbl_person (name,gender,dob) VALUES('henrihnr', 'm', '1985-09-09');
13
INSERT tbl_person (name,gender,dob) VALUES('name_001', 'm', '1990-01-01');
14
INSERT tbl_person (name,gender,dob) VALUES('name_002', 'f', '2000-01-01');
15
INSERT tbl_person (name,gender,dob) VALUES('name_003', 'm', '2000-02-02');
16
INSERT tbl_person (name,gender,dob) VALUES('name_005', 'f', '2000-04-04');
Database configuration. file: CodeIgniter\system\application\config\database.php
1
$active_group = "default";
2
$active_record = TRUE;
3

4
$db['default']['hostname'] = "localhost";
5
$db['default']['username'] = "root";
6
$db['default']['password'] = "";
7
$db['default']['database'] = "crud";
8
$db['default']['dbdriver'] = "mysql";
Base url configuration. file: CodeIgniter\system\application\config\config.php
1
$config['base_url'] = "http://127.0.0.1/CodeIgniter/";
Default controller configuration. file: CodeIgniter\system\application\config\routes.php
1
$route['default_controller'] = "person";

after all this i try to run mi application and this was te result
Quote:An Error Was Encountered

Unable to load the requested class: validation

what i'm doing wrong?

thanks for everything

#7
[eluser]danmontgomery[/eluser]
You're trying to load the validation library somewhere, you should only be loading form_validation

#8
[eluser]kiga[/eluser]
this?
Code:
function Person(){
        parent::__construct();
        
        // load library
        $this->load->library(array('table','validation'));
        
        // load helper
        $this->load->helper('url');
        
        // load model
        $this->load->model('personModel','',TRUE);
    }

#9
[eluser]InsiteFX[/eluser]
Code:
function Person(){
        parent::__construct();
        
        // load library
        $this->load->library(array('table','form_validation'));
        
        // load helper
        $this->load->helper('url');
        
        // load model
        $this->load->model('personModel','',TRUE);
    }

InsiteFX

#10
[eluser]kiga[/eluser]
thanks!!! finally it works!!! .... now, when i try to add data sow this

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Person::$validation

Filename: controllers/person.php

Line Number: 177

( ! ) Fatal error: Call to a member function set_fields() on a non-object in C:\wamp\www\CodeIgniter\application\controllers\person.php on line 177
Call Stack
# Time Memory Function Location
1 0.1225 386296 {main}( ) ..\index.php:0
2 0.1454 454784 require_once( 'C:\wamp\www\CodeIgniter\system\core\CodeIgniter.php' ) ..\index.php:201
3 0.3011 3229144 call_user_func_array ( ) ..\CodeIgniter.php:339
4 0.3011 3229192 Person->add( ) ..\CodeIgniter.php:0
5 0.3011 3229192 Person->_set_fields( ) ..\person.php:57

and the same thing using update

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Person::$validation

Filename: controllers/person.php

Line Number: 177

( ! ) Fatal error: Call to a member function set_fields() on a non-object in C:\wamp\www\CodeIgniter\application\controllers\person.php on line 177
Call Stack
# Time Memory Function Location
1 0.0009 386328 {main}( ) ..\index.php:0
2 0.0026 454816 require_once( 'C:\wamp\www\CodeIgniter\system\core\CodeIgniter.php' ) ..\index.php:201
3 0.0725 3229376 call_user_func_array ( ) ..\CodeIgniter.php:339
4 0.0725 3229424 Person->update( ) ..\CodeIgniter.php:0
5 0.0725 3229424 Person->_set_fields( ) ..\person.php:114

delete and view works!! Smile


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.