我有一个技能,用户可以从中添加更多技巧。
这是我的student_skill
表:
+------+--------------+--------------------------------------+
| id | student_id | skill |
+------+--------------+--------------------------------------+
| 1 | 1 | 10 |
| 2 | 1 | 3 |
| 3 | 2 | 2 |
| 4 | 2 | 6 |
+------+--------------+--------------------------------------+
我的Html表格:
<form action="<?= base_url('skill/add_new_skill') ?>" method="post">
Select Skills:
<select name="skill[]" id="skill" multiple>
<option value="1">Physics</option>
<option value="2">Accounting </option>
<option value="3">Business Activity Monitoring</option>
<option value="4">Redhat Linux </option>
// More Skill Options Here
</select>
<input type="submit" name="submit">
</form>
问题:
我不知道如何插入和更新多行。
我想使用insert_batch
&amp; update_batch
添加&amp;更新技能。
到目前为止我做了什么?
这是我的控制器代码:
//Controller Functions
public function insert_skill(){
$this->load->model ('skill_model');
$skill_data = $this->input->post();
$update_status = $this->skill_model->insert_student_skill($skill_data);
}
public function update_skills(){
$this->load->model ('skill_model');
$skill_data = $this->input->post();
$update_status = $this->skill_model->update_student_skills($skill_data);
}
//Model Functions
//Update Skill Model
public function update_student_skills($skill_data){
//What should i do to update student data
$this->db->update_batch('student_skill', $skill_data);
}
//Insert Skill Model
public function insert_student_skill($skill_data){
//What should i do to Insert student data
$this->db->insert_batch('student_skill', $skill_data);
}
问题案例场景1 :如果用户选择
'Physics','Accounting'
First和In 如果用户根据更改所选选项,则更新过程'Physics','Redhat Linux'
如何更新此类技能 方案
答案 0 :(得分:1)
请通过以下内容了解CI查询构建器中的批处理过程。
<强> insert_batch 强>
使用insert_batch
命令时,First参数为table_name
,第二个参数为insert array。
$data = array(
array(
'title' => 'My title',
'name' => 'My Name',
'date' => 'My date'
),
array(
'title' => 'Another title',
'name' => 'Another Name',
'date' => 'Another date'
)
);
$this->db->insert_batch('table_name', $data);
<强> update_batch 强> 当您使用
update_batch
命令时,您的第一个参数将是table_name
,第二个参数将是值数组,其中condition和第三个参数将包含field_name
where where condition。
$data = array(
array(
'title' => 'My title' ,
'name' => 'My Name 2' ,
'date' => 'My date 2'
),
array(
'title' => 'Another title' ,
'name' => 'Another Name 2' ,
'date' => 'Another date 2'
)
);
$this->db->update_batch('mytable', $data, 'title');
在您的情况下,您需要实现如下所述的登录。
IN控制器
在IN控制器方法中,您需要传递user_id
字段。请查看以下内容。
// Insert Function
$update_status = $this->skill_model->insert_student_skill($skill_data,$user_id);
// Update Function
$update_status = $this->skill_model->update_student_skills($skill_data,$user_id);
IN模型
// Update Data
public function update_student_skills($skill_data,$user_id){
$this->db->where('user_id',$user_id);
$this->db->delete('student_skill');
$ins_data = array();
foreach($skill_data as $i => $skills):
$ins_data[$i]['user_id'] = $user_id;
$ins_data[$i]['skill_id'] = $skills['skill_id'];
endforeach;
$this->db->insert_batch('student_skill', $ins_data);
}
//Insert Skill Model
public function insert_student_skill($skill_data,$user_id){
$ins_data = array();
foreach($skill_data as $i => $skills):
$ins_data[$i]['user_id'] = $user_id;
$ins_data[$i]['skill_id'] = $skills['skill_id'];
endforeach;
$this->db->insert_batch('student_skill', $ins_data);
}
请修改以上代码,具体取决于您的要求。
答案 1 :(得分:0)
首先,您应该按会话拥有当前用户的ID。因此,您可以按ID进行定位以进行更改。
你的观点。
<form action="<?= base_url('skill/update_skill') ?>" method="post">
Select Skills:
<select name="skill[]" id="skill" multiple>
<option value="1">Physics</option>
<option value="2">Accounting </option>
<option value="3">Business Activity Monitoring</option>
<option value="4">Redhat Linux </option>
// More Skill Options Here
</select>
<input type="submit" name="submit">
</form>
控制器
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Skill extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('skill_model');
}
public function index(){
$this->load->view('/your_view');
} // end index function
function update_skill(){
$value = $this->input->post('skill');
$data['skill'] = $value; //it's mean value or rows skill will be inserted/updated
$current_user_id = '1'; // example: current user id is 1 (set it by session). this is shuold in new Stackoverflow question
// Now send data and id to model
$this->skill_model->update_data($data, $current_user_id);
}
} // end Class
模型
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Skill_model extends CI_Model{
function update_data($data, $current_user_id){
$this->db->where('id', '$current_user_id');
$this->db->update('student_skill', $data);
//after done, if want do something put codes below.
redirect(site_url('skill')); //example, after done update, redirect user to mysite.com/skill
} // end update_date()
/* you can add another function in this model. maybe will used from another controller
function another($fromanother){
}
*/
} // end Class