2010-08-18 3 views
5

Sono novizio in codeigniter e sto ancora imparando. Chiunque può aiutare per l'esempio nella visualizzazione di base, aggiungere, aggiornare, eliminare l'operazione e le query in codeigniter saranno apprezzate.vista codice, aggiungere, aggiornare e cancellare

Semplicemente uno come creare una rubrica per principianti.

grazie,

migliori saluti

risposta

5

Alcune query di esempio in 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(); 
    } 
} 

Più informazioni e più modi per CRUD a codeigniter active record documentation Ulteriori informazioni su numero di errore oltre here

Un campione controller

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

Ulteriori informazioni sui controller sopra here

Una vista del campione - 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> 

più su CodeIgniter vista sopra here

+1

sulla funzione deleteRecord(), perché hai usato $ this-> db-> set(), per quanto ne so è l'uso per l'aggiornamento .. – Manie

+0

@Manie - grazie per averlo indicato. L'ho corretto. – vikmalhotra

+0

per Manie e ShiVik grazie per il vostro aiuto, io sono molto grato ... – marikudo

3

È possibile utilizzare questo come un esempio

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

Alcuni di questi sono dal codeigniter userguide.

Per visualizzare i record,

Se i dati di ritorno è matrice di record,

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

Se i dati di ritorno è un oggetto (utilizzando $q->row),

echo $data->title; 

Questo solo alcuni esempi o CRUD in Codeigniter. Visita il codeigniter userguide.

+0

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

+0

@treeface .. grazie .. questa è una possibilità .. – Manie

+0

grazie Manie sua stessa utile... – marikudo