20099

Codeigniniter insert data through models and controller

Question:

<strong>Controller</strong> I have <strong>form_ctrl</strong> 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 <strong>result.php</strong> 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.

Answer1:

<strong>commonModel.php</strong>

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

Answer2:

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

Answer3:

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.

Answer4:

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

Answer5:

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

Recommend

  • Print a Form at higher dpi than screen resolution
  • How to use CoreFoundation in QuickTime SDK for Windows?
  • Insert statement not working using execute(array()) of PDO Extension
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • the IBM_JAVA error for running jobs in Hadoop 2.2.0
  • iOS App crash issue `[UIWindow warpPoint:]`
  • Slick: How can I combine a SQL LIKE statement with a SQL IN statement
  • Unable to connect to AWS RDS through PDO
  • How to get a time and Date Separately?
  • In metro, get all inherited classes of an (abstract) class?
  • Implementation of timeout in LDAP
  • Facebook Error (#200) The user hasn't authorized the application to perform this action (PHP)
  • How to change the host IP sent in emails to new GitLab users to a publicly visible IP, not the local
  • What are advantages/disadvantages of using Selenium for Java vs .NET applications?
  • Spotify cocoalibspotify offline status set to 1 but all tracks stuck at waiting
  • How to use array in autohotkey?
  • How to use Flask's render_template from an ajax POST form submit
  • How to check if a database and tables exist in sql server in a vb .net project?
  • Rotating Towards Path in OpenGL
  • Floating parent div grows to hypothetical width of floating child div
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • Google Spreadsheet Script to Blink a range of Cells
  • C++ STL stack pop operation giving segmentation fault
  • How to specify generic type when the type is only known at runtime?
  • reshape/remould data frame to create normalized bar chart and pie chart
  • How to change user identity when git pushing via ssh?
  • read part of h5 dataset python
  • Did not understand process of initialize in swift programming
  • php “page caching” solution suggestions for CMS Applications
  • Functions by reference or by variable, which to use when?
  • How to encrypt Connectionstring written in web.config from codebehind?
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Capture SIGFPE from SIMD instruction
  • Jersey serializes character value to ASCII equivalent numeric string
  • How to use FirstOrDefault inside Include
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • Time Complexity of Fibonacci Algorithm [duplicate]
  • How to check if object is null in Java?