Ввод данных кодогенератора через модели и контроллер

Контроллер У меня есть form_ctrl код ниже

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

                                       }
        }
}

У меня есть 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>

В этом коде я хочу включить модель для вставки данных вместо контроллера.

Код работает правильно для вставки данных в базу данных, но я хочу, чтобы это через модель не из контроллера. Я пробовал столько раз, но я не получил желаемого результата.

Ответ 1

commonModel.php

class CommonModel extends CI_Model {

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

    public function insert($tableName,$data){

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

    }
}

замените свой код контроллера следующим образом

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


                                       }
        }
}

Ответ 2

   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);

                                           }
            }
    }

модель

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

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


    }
}

Ответ 3

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());

                    }
            }
    }

и ваша модель выглядит ниже.

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

Всегда лучше, чтобы ваша часть контроллера была легкой и имела меньше кода.

Ответ 4

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

Ответ 5

    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)



                                           }
            }
    }

Вот ваша модель your_model..........

класс your_model расширяет CI_Model {

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

    }

}

** Вы должны загрузить your_model в контроллер или в автозагрузку