Codeigniter CRUD: Day 3 Read Operation: In Day 2 Create Operation we created view & controller for create operation to work. which is responsible for showing form for new post entry and saving it to database via post model. Now we have our create operation ready let’s move on to Read Operation of Codeigniter CRUD. So that we can list already created posts.
Controller code for Codeigniter CRUD’s Read Operation
Create a method in post controller named index and add following code in it.
1 2 3 4 5 6 7 |
function index() { $data['posts'] = $this->post->getAll(); $this->load->view("post/index",$data); } |
Controller part for Codeigniter CRUD Read operation is fairly simple. We just have to call model method getAll(). It will return array containing all posts available in database. We just stored in a variable and pass it to index view inside views/post directory. Remember we pass whole $data variable which is an array again. so every key of that array will become a variable in view. so we already created a $posts variable for view.
Index View code for Codeigniter CRUD’s Read Operation
Create a new file inside views/post and name it index.php. Add following code in it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
<style type="text/css"> #container { width:900px; background-color: #d8d8d8; margin: auto; padding:20px; font-size:14px; font-family:tahoma; } .crud { font-size: 12px; width: 100%; } .crud th { background-color: #242424; color:#efefef; padding:10px; } .crud td { background-color: #efefef; padding:10px; color:#242424; } .crud tr.even td { background-color: #fefefe; } </style> <div id="container"> <h1>Post Listing</h1> <?php if($msg = $this->session->flashdata("message")): ?> <p class="success"> <?=$msg?> </p> <?php endif; ?> <p><a href="<?=site_url('post/create')?>">New Post</a></p> <table class="crud" cellspacing="0"> <thead> <tr> <th>ID</th> <th>Post Title</th> <th>Post Content</th> <th>Post Status</th> <th>Post Date</th> <th>Actions</th> </tr> </thead> <tbody> <?php $i=1; foreach($posts as $post): ?> <tr <?=($i % 2 == 0) ? 'class="even"' : '' ?>> <td><?=$post->id?></td> <td><?=$post->post_title?></td> <td><?=$post->post_content?></td> <td><?=$post->post_status?></td> <td><?=$post->post_date?></td> <td><a href="<?=site_url("post/edit/".$post->id)?>">edit</a> <a href="<?=site_url("post/delete/".$post->id)?>">delete</a></td> </tr> <?php $i++; endforeach; ?> </tbody> </table> </div> |
First we added few stylesheet. Basically that I do after setting HTML. After that we have a table for posts listing. inside tbody element I ran a loop of foreach to fetch all posts one by one and output in HTML. As you can see we have a $post object and we can access every field of that using stdobject method. Variable $i is just for setting even class for alternate row color in table.
Now open browser and navigate to http://localhost:8888/ci/index.php/post and you will see similar to below screenshot.
As you can see we also add link to create, update & delete operation. We also want to redirect to index method when new post is added instead on create method itself. Let’s fix that.
Open post controller and modify create method function with highlighted line of code shows. Full Controller code is as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Post extends CI_Controller { function __construct() { parent::__construct(); $this->load->model("postmodel","post"); } function index() { $data['posts'] = $this->post->getAll(); $this->load->view("post/index",$data); } function create() { if(@$_POST['create_post']) { $data = $_POST['post']; $data['post_date'] = date('Y-m-d H:i:s'); $this->post->add($data); $this->session->set_flashdata('message',"Post created successfully"); redirect("post"); } $this->load->view("post/create"); } } |
So we have our Read operation of Codeigniter CRUD working perfectly. So let’s move forward to Update operation for Codeigniter CRUD.
Please like our Facebook Fan page for instant updates on Tutorials.
Pingback: Codeigniter CRUD: Day 2 Create Operation » CodeRiddles
Pingback: Codeigniter CRUD: Day 4 Update Operation » CodeRiddles
Pingback: Codeigniter CRUD: Day 1 Create Model » CodeRiddles