控制器我的 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 代码
<!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>
在这段代码中,我想包含模型来插入数据而不是控制器。
代码正常工作,可以将数据插入到数据库中,但我希望通过模型而不是来自控制器。我尝试了很多次,但我没有得到理想的结果。
答案 0 :(得分: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);
}
}
}
模型
class User_model extends CI_Model
{
public function insert($table,$data)
{
$this->db->insert ( $table, $data );
}
}
答案 1 :(得分:0)
<强> 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 :(得分:0)
field_stats
,您的模型如下所示。
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());
}
}
}
最好的做法是控制器部件重量轻,代码少。
答案 3 :(得分: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;
}
答案 4 :(得分:0)
install.packages("XML", repos = "https://cran.r-project.org/")
这是你的模型 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扩展了CI_Model {
your_model
}
**您必须将 function insert_data($data) {
$this->db->insert('your_table', $data);
}
加载到控制器或自动加载