te

Language   :  
Rabu, 30 Maret 2011 - 12:23:59 WIB

Simple CRUD With CodeIgniter


Diposting oleh : Wahyu Ari Kurniawan
Kategori: PHP - Dibaca: 1074 kali

CRUD stands for create, read, update and delete, which are some of the most basic functions that can be done with data storage. In this tutorial we will be applying CRUD to a MySQL database. We will take a look at the database libraries included in CodeIgniter to ease work with databases, and other included libraries to help out with development as well.

If you haven't yet, you may want to read through An Introduction to CodeIgniter first, so that you at least know the basics of CodeIgniter.

Getting Started

The first thing you should do is setup a database on your server. For the purposes of this tutorial, I will be calling mine "ci". Once that is created, execute the following SQL query to create a simple table.

CREATE TABLE posts (
id INTEGER AUTO_INCREMENT PRIMARY KEY,
title TEXT,
content TEXT
);

Next, we need to do a bit of configuration. Open up config/database.php in your favourite text editor and change the following values to match your database configuration.

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "MySQL Username";
$db['default']['password'] = "MySQL Password";
$db['default']['database'] = "ci";

Next open up config/autoload.php. This file contains the systems to be loaded by default. Change the autoloaded libraries (around line 42) to this:

$autoload['libraries'] = array('database');

We are autoloading the database library because we will be using it a lot throughout this tutorial.

Create

Let's get started by creating new records. First, create a new controller called crud.php. Inside of that file, type in the following code.

class Crud extends Controller {
 
function index() {
// Check if form is submitted
if ($this->input->post('submit')) {
$title = $this->input->xss_clean($this->input->post('title'));
$content = $this->input->xss_clean($this->input->post('content'));
 
// Add the post
$this->posts_model->addPost($title, $content);
}
 
$this->load->view('crud_view');
}
 
}

When the index function of our crud controller is called, it first checks if the form, which we will create, has been submitted. Next the input is run through an XSS filter for security purposes, and then added to the database through a function we will create.

Finally, a view will be loaded to display the form, and later, the posts.

The Model

Models are used in the MVC pattern for working with data. In CodeIgniter they are located in the models directory. Create a new model called posts_model.php. Type the following code inside it.

class Posts_model extends Model {
 
function addPost($title, $content) {
$data = array(
'title' => $title,
'content' => $content
);
 
$this->db->insert('posts', $data);
}
 
}

This is a very simple example. Our model class, called Posts_model contains a function called addPost, which can be used with two parameters for the title and the content of the post. An array called $data is created which stores the values. Finally the data is inserted into the database using a function included in the database library, which we automatically loaded.

Creating The Form

For our form will be using a view. Create a new view called crud_view.php.

To build the form, we will use the form helper, which is a library included in CodeIgniter to help make building forms easier. Using the form helper, here is the code to create our form.

 $this->load->helper('form'); ?>
 
 echo form_open('crud'); ?>
 

echo form_input('title'); ?>

 

echo form_textarea('content'); ?>

 

echo form_submit('submit', 'Submit'); ?>

  echo form_close(); ?>

Now you can test it out. Point your browser to index.php/crud/ and try filling out the form. Check the database in phpMyAdmin (or whatever you use). If everything went well, there should be some new rows in the table. If it works, we can continue to the next part!

Read

Reading from the database is still very simple in CodeIgniter, but a bit more complicated than inserting. Inside of our model, add in this new function:

function getPosts() {
$query = $this->db->get('posts');
$posts = array();
 
foreach ($query->result() as $row) {
$posts[] = array(
'id' => $row->id,
'title' => $row->title,
'content' => $row->content
);
}
 
return $posts;
}

This function first creates a query that gets all of the rows from the posts table of the database. Using a foreach statement, it loops through all of the rows to create an array. When that is finished, the function returns an array of all the posts.

Next, inside our controller, inside the index function, delete the line that loads the view, and add in the following lines of code in its place.

$data = array();
 
$data['posts'] = $this->posts_model->getPosts();
$this->load->view('crud_view', $data);

The variable $data is declared as an array. Then an array inside of $data contains all of our posts, using our getPosts function. And then it loads the view again, this time passing in the $data variable, now containing all of our posts.

Next we are going to display all of the posts to the user. Because we passed them in to the view, we just need to add a few lines to the view, below the form, to get it to display the posts.

 if (isset($posts)): foreach ($posts as $p): ?>
 

echo $p['title']; ?>

  echo $p['content']; ?>   endforeach; else: ?>  

No posts found

  endif; ?>

This first checks if there are any posts to display. If there are, it simply loops through each of them, displaying the title and the content for each of them. If there are no posts to show the user, it shows them a very informative message.

Update

To update data in the database, first add a new function to our model.

function updatePost($id, $title, $content) {
$data = array(
'title' => $title,
'content' => $content
);
 
$this->db->where('id', $id);
$this->db->update('posts', $data);
}

Our new function, updatePost, accepts three parameters, for the id of the post to update, the new title of the post, and the updated content.

$data is an array that stores the title and content. Next a function included in CodeIgniter's database library tells the next SQL query where the row should be updated. In this case it is where the id field of the table is equal to $id. And lastly, the update function is called, which updates the posts database table with the information supplied.

A new form needs to be created to be able to update the post. Create a new view called updateform.php and type in the following code which makes use of CodeIgniter's form helper.

 $this->load->helper('form'); ?>
 

CodeIgniter CRUD Tutorial Example

  echo form_open('crud/update/'.$id); ?>  

echo form_input('title'); ?>

 

echo form_textarea('content'); ?>

 

echo form_submit('submit', 'Submit'); ?>

  echo form_close(); ?>

A new function also needs to be added to the controller for this to work.

function update() {
$id = $this->uri->segment(3);
 
if ($this->input->post('submit')) {
$title = $this->input->xss_clean($this->input->post('title'));
$content = $this->input->xss_clean($this->input->post('content'));
 
$this->posts_model->updatePost($id, $title, $content);
 
$data['posts'] = $this->posts_model->getPosts();
$this->load->view('crud_view', $data);
} else {
$data = array('id' => $id);
$this->load->view('updateform', $data);
}
}

As you can probably tell, this function is very similar to the one to add posts. However this one deals with URI segments. A segment is just a part of a URI, which contains a certain value. Here we use the third segment of the URI, which contains the ID of the post to update.

If the form has been submitted, the title and content are run through an XSS filter again, and the post is updated. It also retrieves all of the posts and passes them into the crud_view view, which is loaded after a post has been updated.

If the form has not been submitted, the form is loaded, with the ID of the post passed in, as it is used in the form.

To test this out, point your browser to index.php/crud/update/YourID, replacing YourID with the ID of the post you would like to update.

Delete

We will finish off this tutorial with Delete, which uses some very simple functions. Create one last function inside of our model.

function deletePost($id) {
$this->db->where('id', $id);
$this->db->delete('posts');
}

That's really all there is to it. The where function is used just like in the "update" function, to indicate which rows will be affected. And then the next statement which simply deletes that row.

Another simple function must be added to our controller, this one called delete.

function delete() {
$id = $this->uri->segment(3);
$this->posts_model->deletePost($id);
 
$data['posts'] = $this->posts_model->getPosts();
$this->load->view('crud_view', $data);
}

This, again, gets the ID of the post to delete from the third segment of the URI. The our deletePost function is called to delete the post with that ID. All of the posts are then retrieved, and displayed to the user through the crud_view view.

To do simple CRUD functions with CodeIgniter, it is really a very simple task. If you have any questions, or if somehow, something doesn't work for you, be sure to leave a comment and I will do my best to help you out.

Also, be sure to subscribe to the RSS feed or for email updates, because now that we know the basics of CodeIgniter, I will start posting a series of posts on something more useful, creating a simple website using CodeIgniter.

 

sumber http://fwebde.com/php/simple-crud-with-codeigniter/


Related Link

Share |
» cara mem-Format komputer, tapi drive D nya ga hilang
2 Comment :

widodo
03 April 2011 - 18:17:19 WIB

kopas dari mana neee??
Puisi Cinta 2012
09 November 2011 - 08:51:56 WIB

Makasih Infonya

Isi Komentar :
Nama :
Email :
Website :
Komentar
  Karakter yang masih tersedia.
 
 (Masukkan 6 kode diatas)