表名:stud | |
roll_no | int(11) |
name | varchar(30) |
$autoload['libraries'] = array(‘database’);
$this->load->database();
语法
|
insert([$table = ''[, $set = NULL[, $escape = NULL]]]) |
Parameters
|
$table (string)-表名 $set (array)-字段/值对的关联数组 $escape (bool)-是否转义值和标识符 |
Returns
|
成功为真,失败为假 |
Return Type
|
布尔 |
$data = array( 'roll_no' => ‘1’, 'name' => ‘Virat’ ); $this->db->insert("stud", $data);
语法
|
set($key[, $value = ''[, $escape = NULL]]) |
Parameters
|
$key (mixed)-字段名称,或字段/值对数组 $value (string)-字段值,如果 $key 是单个字段 $escape (bool)-是否转义值和标识符 |
Returns
|
CI_DB_query_builder 实例(方法链) |
Return Type
|
CI_DB_query_builder |
语法
|
where($key[, $value = NULL[, $escape = NULL]]) |
Parameters
|
$key (mixed)-要比较的字段名称或关联数组 $value (mixed)-如果是单个键,则与此值进行比较 $escape (bool)-是否转义值和标识符 |
Returns
|
DB_query_builder 实例 |
Return Type
|
对象 |
语法
|
update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL ]]]]) |
Parameters
|
$table (string)-表名 $set (array)-字段/值对的关联数组 $where (string)-WHERE 子句 $limit (int)-LIMIT 子句 |
Returns
|
成功为真,失败为假 |
Return Type
|
布尔 |
$data = array( 'roll_no' => ‘1’, 'name' => ‘Virat’ ); $this->db->set($data); $this->db->where("roll_no", ‘1’); $this->db->update("stud", $data);
语法
|
delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]]]) |
Parameters
|
$table (mixed)-要从中删除的表;字符串或数组 $where (string)-WHERE 子句 $limit (int)-LIMIT 子句 $reset_data (bool) − TRUE 重置查询"write"子句 |
Returns
|
CI_DB_query_builder 实例(方法链)或失败时 FALSE |
Return Type
|
mixed |
$this->db->delete("stud", "roll_no = 1");
语法
|
get([$table = ''[, $limit = NULL[, $offset = NULL]]]) |
Parameters
|
$table (string)-查询数组的表 $limit (int)-LIMIT 子句 $offset (int)-OFFSET 子句 |
Returns
|
CI_DB_result 实例(方法链) |
Return Type
|
CI_DB_result |
$query = $this->db->get("stud"); $data['records'] = $query->result();
$this->db->close();
<?php class Stud_controller extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper('url'); $this->load->database(); } public function index() { $query = $this->db->get("stud"); $data['records'] = $query->result(); $this->load->helper('url'); $this->load->view('Stud_view',$data); } public function add_student_view() { $this->load->helper('form'); $this->load->view('Stud_add'); } public function add_student() { $this->load->model('Stud_Model'); $data = array( 'roll_no' => $this->input->post('roll_no'), 'name' => $this->input->post('name') ); $this->Stud_Model->insert($data); $query = $this->db->get("stud"); $data['records'] = $query->result(); $this->load->view('Stud_view',$data); } public function update_student_view() { $this->load->helper('form'); $roll_no = $this->uri->segment('3'); $query = $this->db->get_where("stud",array("roll_no"=>$roll_no)); $data['records'] = $query->result(); $data['old_roll_no'] = $roll_no; $this->load->view('Stud_edit',$data); } public function update_student(){ $this->load->model('Stud_Model'); $data = array( 'roll_no' => $this->input->post('roll_no'), 'name' => $this->input->post('name') ); $old_roll_no = $this->input->post('old_roll_no'); $this->Stud_Model->update($data,$old_roll_no); $query = $this->db->get("stud"); $data['records'] = $query->result(); $this->load->view('Stud_view',$data); } public function delete_student() { $this->load->model('Stud_Model'); $roll_no = $this->uri->segment('3'); $this->Stud_Model->delete($roll_no); $query = $this->db->get("stud"); $data['records'] = $query->result(); $this->load->view('Stud_view',$data); } } ?>
<?php class Stud_Model extends CI_Model { function __construct() { parent::__construct(); } public function insert($data) { if ($this->db->insert("stud", $data)) { return true; } } public function delete($roll_no) { if ($this->db->delete("stud", "roll_no = ".$roll_no)) { return true; } } public function update($data,$old_roll_no) { $this->db->set($data); $this->db->where("roll_no", $old_roll_no); $this->db->update("stud", $data); } } ?>
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>Students Example</title> </head> <body> <?php echo form_open('Stud_controller/add_student'); echo form_label('Roll No.'); echo form_input(array('id'=>'roll_no','name'=>'roll_no')); echo "<br/>"; echo form_label('Name'); echo form_input(array('id'=>'name','name'=>'name')); echo "<br/>"; echo form_submit(array('id'=>'submit','value'=>'Add')); echo form_close(); ?> </body> </html>
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>Students Example</title> </head> <body> <form method = "" action = ""> <?php echo form_open('Stud_controller/update_student'); echo form_hidden('old_roll_no',$old_roll_no); echo form_label('Roll No.'); echo form_input(array('id'⇒'roll_no', 'name'⇒'roll_no','value'⇒$records[0]→roll_no)); echo " "; echo form_label('Name'); echo form_input(array('id'⇒'name','name'⇒'name', 'value'⇒$records[0]→name)); echo " "; echo form_submit(array('id'⇒'sub mit','value'⇒'Edit')); echo form_close(); ?> </form> </body> </html>
<!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>Students Example</title> </head> <body> <a href = "<?php echo base_url(); ?> index.php/stud/add_view">Add</a> <table border = "1"> <?php $i = 1; echo "<tr>"; echo "<td>Sr#</td>"; echo "<td>Roll No.</td>"; echo "<td>Name</td>"; echo "<td>Edit</td>"; echo "<td>Delete</td>"; echo "<tr>"; foreach($records as $r) { echo "<tr>"; echo "<td>".$i++."</td>"; echo "<td>".$r->roll_no."</td>"; echo "<td>".$r->name."</td>"; echo "<td><a href = '".base_url()."index.php/stud/edit/" .$r->roll_no."'>Edit</a></td>"; echo "<td><a href = '".base_url()."index.php/stud/delete/" .$r->roll_no."'>Delete</a></td>"; echo "<tr>"; } ?> </table> </body> </html>
$route['stud'] = "Stud_controller"; $route['stud/add'] = 'Stud_controller/add_student'; $route['stud/add_view'] = 'Stud_controller/add_student_view'; $route['stud/edit/(\d+)'] = 'Stud_controller/update_student_view/$1'; $route['stud/delete/(\d+)'] = 'Stud_controller/delete_student/$1';
http://yoursite.com/index.php/stud