Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

codeigniter view, add, update and delete

I'm newbie in codeigniter and still learning. Anyone can help for sample in basic view, add, update, delete operation and queries in codeigniter will gladly appreciated.

Just a simple one like creating addressbook for newbie.

thanks,

best regards

like image 464
marikudo Avatar asked Aug 18 '10 05:08

marikudo


People also ask

How to add INSERT UPDATE and delete operations in Codeigniter 4?

To begin the insert, update, and delete operations development process, you must first manually download the CodeIgniter 4 framework. Extract the zip file in your root where you want to add the project, If you are using XAMP let’s add in xamp\htdocs. If you are using ubuntu you can add this in /var/www or /var/www/html directory. 2.

What is CodeIgniter and how to use it?

CodeIgniter (CI) is a PHP framework that helps building a full-fledged web application. This article is a continuation of the basic tutorialpresent in the official CodeIgniter site. The tutorial had view and add data part. But, it didn’t contain the update and delete part. I have just added update and delete functionality in it.

How to create a CRUD (add/edit/delete/update) application in CodeIgniter?

Here is the step-by-step guide on creating a CRUD (Add/Edit/Delete/Update) application in CodeIgniter. Create Database and Table First of all, we create database and table. For this article example, we will be creating database named `test`. We will be creating a table named `news` inside the database `test`.

How many views does CodeIgniter have?

Very Simple Add, Edit, Delete, View (CRUD) in PHP & MySQL [Beginner Tutorial]- 367,861 views Magento: How to get attribute name and value?- 185,656 views GPG: Remove keys from your public keyring?- 144,527 views CodeIgniter: Simple Add, Edit, Delete, View – MVC CRUD Application- 127,455 views


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

More information and more ways for CRUD in codeigniter active record documentation More about error number over here

A 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

A sample view - 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>

More about codeigniter views over here

like image 163
vikmalhotra Avatar answered Oct 09 '22 07:10

vikmalhotra


You can use this as an example

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

Some of this are from codeigniter userguide.

To view the records,

If return data is array of records,

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

If the return data is an object (by using $q->row),

   echo $data->title;

This is just a few examples or CRUD in Codeigniter. Visit the codeigniter userguide.

like image 36
Manie Avatar answered Oct 09 '22 08:10

Manie