This section will show you how to create the admin page for our blog, with pages to create and edit blog posts. By the end of this part you should have a functioning admin page (that requires login!) displaying a list of posts from the database, with links to view, edit, or delete the post.
Create edit.php at this location :
/application/views/admin/edit.php
Copy the code below :
We’re gonna want to add the methods to insert, update, and delete posts. With CodeIgniter manipulating database data is simplified.
Go back to the Posts model located at :
/application/models/posts.php
Copy the extra methods to our Post model from the code below :
- <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
- class Posts extends CI_Model
- {
- function __construct()
- {
- parent::__construct();
- }
- // If $postId is NULL, gets all posts, otherwise get single post from db
- // returns $post[]
- public function get_posts($postId)
- {
- $post=array();
- if ($postId !== null){
- $query = $this->db->get_where('posts', array('id' =>$postId));
- if ($query->num_rows() == 1) {
- foreach($query->result() as $row){
- $post['id'] = $row->id;
- $post['title'] = $row->title;
- $post['summary'] = $row->summary;
- $post['content'] = $row->content;
- }
- return $post;
- }
- } else {
- $query = $this->db->get('posts');
- if ($query->num_rows() !== 0 ){
- foreach($query->result() as $row){
- $post['id'][] = $row->id;
- $post['title'][] = $row->title;
- $post['summary'][] = $row->summary;
- $post['content'][] = $row->content;
- }
- return $post;
- }
- }
- }
- function insert_post($data){
- $this->db->insert('posts', $data);
- return;
- }
- function update_post($postId, $data){
- $id = $postId;
- $this->db->where('id',$id);
- $this->db->update('posts', $data);
- return;
- }
- function delete_post($postId){
- $id = $postId;
- $this->db->delete('posts',array('id' => $id));
- return;
- }
- }
Views
Once again we’re going to need to create a few simple view files for our admin index page, a new html head wrapper file that will have a ‘Logout’ link instead of an ‘Admin’ link, and forms for creating/editing posts.
Create admin_html_head.php at this location :
/application/views/template/admin_html_head.php
Copy the code below :
- <!DOCTYPE html>
- <head>
- <link rel="stylesheet" type="text/css" href="<?php echobase_url('style.css');?>">
- </head>
- <body>
- <h1><?php echo anchor('','My Blog'); ?></h1>
- <div id="loginDiv"><?php echo anchor('admin/logout','Logout');?></div>
- <hr/>
Create index.php at this location :
/application/views/admin/index.php
Copy the code below :
- <?php
- echo '<p>Welcome To The Admin Page '.$username.'! All posts available for edit or deletion is listed below.</p><br/>';
- echo anchor('admin/create','Create New Post');
- $count = count($post['id']);
- for ($i=0;$i<$count;$i++)
- {
- echo '<div class="postDiv">';
- echo '<h4>'.$post['title'][$i];
- echo anchor('blog/view/'.$post['id'][$i],' [view]');
- echo anchor('admin/edit/'.$post['id'][$i],' [edit]');
- echo anchor('admin/delete/'.$post['id'][$i],' [delete]</h4>');
- echo '<p>'.$post['summary'][$i].'</p>';
- echo '</div>';
- }
- ?>
reate create.php at this location :
/application/views/admin/create.php
Copy the code below :
- <?php
- echo validation_errors();
- ?>
- <h4>Create A New Post Below</h4>
- <form action="" method="post" >
- <p>Title:</p>
- <input type="text" name="title" size="50"/><br/>
- <p>Summary:</p>
- <textarea name="summary" rows="2" cols="50"></textarea><br/>
- <p>Post Content:</p>
- <textarea name="content" rows="6" cols="50"></textarea><br/>
- <input type="submit" value="Save" />
- <?php
- echo anchor('admin','Cancel');
- ?>
- </form>
Create edit.php at this location :
/application/views/admin/edit.php
Copy the code below :
- <?php
- echo validation_errors();
- ?>
- <h4>Edit "<?php echo $post['title']; ?>" Below</h4>
- <form action="" method="post" >
- <p>Title:</p>
- <input type="text" name="title" size="50" value="<?php echo$post['title']; ?>"/><br/>
- <p>Summary:</p>
- <textarea name="summary" rows="2" cols="50"><?php echo$post['summary']; ?>
- </textarea><br/>
- <p>Post Content:</p>
- <textarea name="content" rows="6" cols="50"><?php echo$post['content']; ?>
- </textarea><br/>
- <input type="submit" value="Save" />
- <?php
- echo anchor('admin','Cancel');
- ?>
- </form>