2010-08-18 27 views
5

Tôi là người mới tham gia viết mã và vẫn đang học. Bất cứ ai cũng có thể giúp cho mẫu trong xem cơ bản, thêm, cập nhật, xóa hoạt động và truy vấn trong codeigniter sẽ sẵn sàng đánh giá cao.xem mã vạch, thêm, cập nhật và xóa

Chỉ đơn giản như tạo sổ địa chỉ cho người mới sử dụng.

cảm ơn,

Trân

Trả lời

5

Một số truy vấn mẫu trong CodeIgniter

class Names extends Model { 
    function addRecord($yourname) { 
    $this->db->set("name", $yourname); 
    $this->db->insert("names"); 
    return $this->db->_error_number(); // return the error occurred in last query 
    } 
    function updateRecord($yourname) { 
    $this->db->set("name", $yourname); 
    $this->db->update("names"); 
    } 
    function deleteRecord($yourname) { 
    $this->db->where("name", $yourname); 
    $this->db->delete("names"); 
    } 
    function selectRecord($yourname) { 
    $this->db->select("name, name_id"); 
    $this->db->from("names"); 
    $this->db->where("name", $yourname); 
    $query = $this->db->get(); 
    return $this->db->result(); 
    } 
    function selectAll() { 
    $this->db->select("name"); 
    $this->db->from("names"); 
    return $this->db->get(); 
    } 
} 

biết thêm thông tin và cách thức hơn cho CRUD trong codeigniter active record documentation Thông tin thêm về số lượng lỗi trên here

Một mẫu bộ điều khiển

class names_controller extends Controller { 
    function addPerson() { 
     $this->load->Model("Names"); 
     $name = $this->input->post("name"); // get the data from a form submit 
     $name = $this->xss->clean(); 
     $error = $this->Names->addRecord($name); 
     if(!$error) { 
     $results = $this->Names->selectAll(); 
     $data['names'] = $results->result(); 
     $this->load->view("show_names", $data); 
     } else { 
     $this->load->view("error"); 
     } 
    } 
} 

Thông tin thêm về bộ điều khiển trên here

Một quan điểm mẫu - show_names.php

<table> 
    <tr> 
    <td>Name</td> 
    </tr> 
    <?php foreach($names as $row): ?> 
    <tr><td><?ph echo $row->name; ?></td></tr> 
    <?php endforeach; ?> 
</table> 

Thông tin thêm về quan điểm CodeIgniter qua here

+1

trên hàm deleteRecord(), tại sao bạn sử dụng $ this-> db-> set(), theo như tôi biết nó được sử dụng để cập nhật .. – Manie

+0

@Manie - cảm ơn vì đã chỉ ra điều đó. Đã sửa chữa nó. – vikmalhotra

+0

cho Manie và ShiVik cảm ơn sự giúp đỡ của bạn, tôi rất biết ơn ... – marikudo

3

Bạn có thể sử dụng như là một ví dụ

class Crud extends Model { 
    // selecting records by specifying the column field 
    function select() 
    { 
     // use $this->db->select('*') if you want to select all the records 
     $this->db->select('title, content, date'); 
     // use $this->db->where('id', 1) if you want to specify what row to be fetched 
     $q = $this->db->get('mytable'); 

     // to get the result 
     $data = array(); 
     // for me its better to check if there are records that are fetched 
     if($q->num_rows() > 0) { 
      // by doing this it means you are returning array of records 
      foreach($q->result_array() as $row) { 
       $data[] = $row; 
      } 
      // if your expecting only one record will be fetched from the table 
      // use $row = $q->row(); 
      // then return $row; 
     } 
     return $data; 
    } 

    // to add record 
    function add() 
    { 
     $data = array(
      'title' => 'My title' , 
      'name' => 'My Name' , 
      'date' => 'My date' 
     ); 

     $this->db->insert('mytable', $data); 
    } 

    // to update record 
    function update() 
    { 
     $data = array(
      'title' => $title, 
      'name' => $name, 
      'date' => $date 
     ); 

     $this->db->where('id', 1); 
     $this->db->update('mytable', $data); 
    } 

    // to delete a record 
    function delete() 
    { 
     $this->db->where('id', 1); 
     $this->db->delete('mytable'); 
    } 
} 

Một số trong số này là từ codeigniter userguide.

Để xem các hồ sơ,

Nếu trở lại dữ liệu là mảng các bản ghi,

foreach($data as $row) 
    { 
     echo $row['title'] . "<br />"; 
    } 

Nếu dữ liệu trở lại là một đối tượng (bằng cách sử dụng $q->row),

echo $data->title; 

Đây là chỉ là một vài ví dụ hoặc CRUD trong Codeigniter. Truy cập trình viết mã userguide.

+0

* foreach ($ q-> result_array () là $ row) {$ data [] = $ row;} * = ** $ data = $ q-> result_array(); ** – treeface

+0

@treeface .. cảm ơn .. đó là một tùy chọn .. – Manie

+0

nhờ Manie Hữu ích... – marikudo

Các vấn đề liên quan