1

Controller I have form_ctrl code is below

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class form_ctrl extends CI_Controller {

    public function index()
    {
        //$this->load->view('welcome_message');
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');

                //$this->form_validation->set_rules('name', 'Username', 'required');
              $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('pass', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('table');
                }
                else
                {
                        $this->load->view('results');
                        $name=$this->input->post('name');
                        $pass=$this->input->post('pass');
                        $email=$this->input->post('email');
                        $mobile=$this->input->post('mobile');
            $address=$this->input->post('address');
                             $data = array(
                                           'name' =>$name ,
                                           'pass' => $pass,
                                           'email' => $email,
                                           'mobile' => $mobile,
                       'address' => $address
                                           );
                            $this->db->insert('form', $data);

                                       }
        }
}

View I have result.php code

 <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    <?php echo validation_errors(); ?>
    <?php echo form_open(); ?>
    <table >
    <tr>
        <td colspan=2 align="center"><h3>User Details</h3></td>
    </tr>
    <tr>
        <td>
            <?php echo form_label('Name'); ?>
        </td>
        <td>
            <?php echo form_input(array('id' => 'name', 'name' => 'name')); ?>
        </td>
    </tr>
    <tr>
        <td>
            <?php echo form_label('Pass'); ?>
        </td>
        <td>
            <?php echo form_password(array('id' => 'pass', 'name' => 'pass')); ?>
        </td>
    </tr>
    <tr>
        <td><?php echo form_label('Email'); ?>
    </td>
    <td><?php echo form_input(array('id' => 'email', 'name' => 'email')); ?></td>
    </tr>
    <tr>
        <td><?php echo form_label('Mobile'); ?>
    </td>
        <td><?php echo form_input(array('id' => 'mobile', 'name' => 'mobile')); ?>
    </td>
    </tr>
    <tr>
        <td><?php echo form_label('Address'); ?>
    </td>
        <td><?php echo form_input(array('id' => 'address', 'name' => 'address')); ?>
    </td>
    </tr>
    <tr>
        <td colspan="2" align="center"><?php echo form_submit(array('id' => 'submit', 'value' => 'Submit')); ?>
    </td>
    </tr>
    <?php echo form_close(); ?>
    </table>
    </body>
    </html>

In this code I want to include model to insert the data instead of controller.

The code is working properly for insert data into database but I want this through model not from controller. I tried so many times but I didn't get the desirable result.

1
  • this is not how its gona work you have to load model in controller and then pass the parameters / data to save in DB its MVC pattern and you have to stick with it like that Commented Jun 14, 2017 at 4:58

5 Answers 5

0

commonModel.php

class CommonModel extends CI_Model {

    function __construct() { 
        parent::__construct (); 
    }

    public function insert($tableName,$data){

       return $this->db->insert($tableName, $data);

    }
}

replace your controller code like this

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class form_ctrl extends CI_Controller {

    public function index()
    {
        //$this->load->view('welcome_message');
             $this->load->helper(array('form', 'url'));
             $this->load->library('form_validation');
             $this->load->model('commonModel');

                //$this->form_validation->set_rules('name', 'Username', 'required');
              $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                $this->form_validation->set_rules('pass', 'Password', 'required',
                        array('required' => 'You must provide a %s.')
                );
                $this->form_validation->set_rules('email', 'Email', 'required');
                $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                if ($this->form_validation->run() == FALSE)
                {
                        $this->load->view('table');
                }
                else
                {
                        $this->load->view('results');
                        $name=$this->input->post('name');
                        $pass=$this->input->post('pass');
                        $email=$this->input->post('email');
                        $mobile=$this->input->post('mobile');
            $address=$this->input->post('address');
                             $data = array(
                                           'name' =>$name ,
                                           'pass' => $pass,
                                           'email' => $email,
                                           'mobile' => $mobile,
                       'address' => $address
                                           );
                            $this->commonModel->insert('form', $data);


                                       }
        }
}
Sign up to request clarification or add additional context in comments.

13 Comments

getting this error now: An Error Was Encountered Unable to locate the model you have specified: data_model
did you crated commonModel.php file in application/models/commonModel.php path?
Please provide data_model code if its public. @Gaurav
Data_model and file name Data_model.php give it
my model is here : <?php class Data_model extends CI_Model { public function insert_data($data) { $this->db->insert($data); } } ?>
|
0
   class form_ctrl extends CI_Controller {

        public function index()
        {
            //$this->load->view('welcome_message');
                 $this->load->helper(array('form', 'url'));
                 $this->load->library('form_validation');

                    //$this->form_validation->set_rules('name', 'Username', 'required');
                  $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                    $this->form_validation->set_rules('pass', 'Password', 'required',
                            array('required' => 'You must provide a %s.')
                    );
                    $this->form_validation->set_rules('email', 'Email', 'required');
                    $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                    $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                    if ($this->form_validation->run() == FALSE)
                    {
                            $this->load->view('table');
                    }
                    else
                    {
                            $this->load->view('results');
                            $name=$this->input->post('name');
                            $pass=$this->input->post('pass');
                            $email=$this->input->post('email');
                            $mobile=$this->input->post('mobile');
                $address=$this->input->post('address');
                                 $data = array(
                                               'name' =>$name ,
                                               'pass' => $pass,
                                               'email' => $email,
                                               'mobile' => $mobile,
                           'address' => $address
                                               );
$this->load->model ( 'user_model' );

                                $this->user_model->insert('form', $data);

                                           }
            }
    }

model

class User_model extends CI_Model 
{
public function insert($table,$data)
    {

        $this->db->insert ( $table, $data );


    }
}

1 Comment

getting this error now::An Error Was Encountered Unable to locate the model you have specified: data_model
0
class form_ctrl extends CI_Controller {
        public function index(){
                 $this->load->helper(array('form', 'url'));
                 $this->load->library('form_validation');
                 $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                 $this->form_validation->set_rules('pass', 'Password', 'required',
                            array('required' => 'You must provide a %s.')
                    );
                 $this->form_validation->set_rules('email', 'Email', 'required');
                 $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                 $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                    if ($this->form_validation->run() == FALSE)
                    {
                            $this->load->view('table');
                    }
                    else
                    {
                            $data = $this->input->post();
                            $this->load->view('results',$data);
                            $this->load->model ( 'user_model' );
                            $this->user_model->insert('form', $this->input->post());

                    }
            }
    }

and your model looks like below.

 public function insert($table, $data) {
        $param = array(
            'name' => $data['name'],
            'pass' => $data['pass'],
            'email' => $data['email'],
            'mobile' => $data['mobile'],
            'address' => $data['address']
        );
        $this->db->insert($table, $param);
    }

It's always best practices that your controller part will be light weight and have less code.

2 Comments

thanks shyam and if i want to send $data to new view file i have results.php how can i send these data to send on that file
So you can pass this array while loading view in second parameter. $this->load->view('result',$array)
0
**controller**

$postData = $_POST;
$result = $this->batch->addBatch($postData);

**Batch Model**

class Batch_model extends MY_Model {

    public function __construct() {
        parent::__construct();
    }

 function addBatch($postData) {
        $this->_table = TBL_BATCH;
        $result = $this->add($postData);
        return $result;
    }
}



**My Model**
public $_table;
public $_fields;
public $_where;
protected $_except_fields = array();
protected $soft_delete = TRUE;

function add($PostData) {

        $postArray = $this->getDatabseFields($PostData);
        $query = $this->db->insert($this->_table, $postArray);

        if ($this->db->affected_rows() > 0)
            return $this->db->insert_id();
        else
            return '';
    }

protected function getDatabseFields($postData, $tableName = '') {
        if (empty($tableName))
            $tableName = $this->_table;

        $table_fields = $this->getFields($tableName);

        $final = array_intersect_key($postData, $table_fields);

        return $final;
    }

2 Comments

This is not an answer for above question @komal
I have just suggest that he can also implement like this.
0
    Controller I have form_ctrl code is below

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');

    class form_ctrl extends CI_Controller {

        public function index()
        {
            //$this->load->view('welcome_message');
                 $this->load->helper(array('form', 'url'));
                 $this->load->library('form_validation');

                    //$this->form_validation->set_rules('name', 'Username', 'required');
                  $this->form_validation->set_rules('name', 'name','required|min_length[5]|max_length[12]');
                    $this->form_validation->set_rules('pass', 'Password', 'required',
                            array('required' => 'You must provide a %s.')
                    );
                    $this->form_validation->set_rules('email', 'Email', 'required');
                    $this->form_validation->set_rules('mobile', 'Mobile', 'required');
                    $this->form_validation->set_rules('address', 'Address','required|min_length[5]');

                    if ($this->form_validation->run() == FALSE)
                    {
                            $this->load->view('table');
                    }
                    else
                    {
                            $this->load->view('results');
                            $name=$this->input->post('name');
                            $pass=$this->input->post('pass');
                            $email=$this->input->post('email');
                            $mobile=$this->input->post('mobile');
                $address=$this->input->post('address');
                                 $data = array(
                                               'name' =>$name ,
                                               'pass' => $pass,
                                               'email' => $email,
                                               'mobile' => $mobile,
                           'address' => $address
                                               );
                     $this->your_model->insert_data($data)



                                           }
            }
    }

Here is your model your_model..........

class your_model extends CI_Model {

 function insert_data($data) {
       $this->db->insert('your_table', $data);

    }

}

** You must load your_model to controller or in autoload

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.