Codeigniter view, add, update and delete

I am new to programming and have still studied. Anyone can help the sample in the basic representation, add, update, delete operations and queries in codeigniter will be happy to appreciate.

Just as simple as creating an address book for beginners.

thank,

Regards

+5
source share
2 answers

Some sample queries 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();
  }
}

Additional information and other methods for CRUD in the documentation for recording with codeignign Read more about the error number here

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

More about controllers over here.

View example - 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>

codeigniter

+5

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

codeigniter userguide.

,

,

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

( $q->row),

   echo $data->title;

CRUD Codeigniter. codeigniter userguide.

+3

All Articles