Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, 24 November 2013

Simple PHP Code in Codeigniter

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.

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 :

  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. class Posts extends CI_Model
  3. {
  4.        
  5.         function __construct()
  6.         {
  7.                 parent::__construct();
  8.         }
  9.         // If $postId is NULL, gets all posts, otherwise get single post from db
  10.         // returns $post[]
  11.         public function get_posts($postId)
  12.         {
  13.                 $post=array();
  14.                 if ($postId !== null){         
  15.                         $query = $this->db->get_where('posts', array('id' =>$postId));
  16.                         if ($query->num_rows() == 1) { 
  17.                                 foreach($query->result() as $row){
  18.                                 $post['id'] = $row->id;
  19.                                 $post['title'] = $row->title;
  20.                                 $post['summary'] = $row->summary;
  21.                                 $post['content'] = $row->content;
  22.                                 }
  23.                                 return $post;
  24.                         }      
  25.                 } else {
  26.                         $query = $this->db->get('posts');
  27.                         if ($query->num_rows() !== 0 ){
  28.                                 foreach($query->result() as $row){
  29.                                 $post['id'][] = $row->id;
  30.                                 $post['title'][] = $row->title;
  31.                                 $post['summary'][] = $row->summary;
  32.                                 $post['content'][] = $row->content;
  33.                                 }
  34.                                 return $post;
  35.                         }
  36.                        
  37.                 }              
  38.         }
  39.         function insert_post($data){
  40.                 $this->db->insert('posts', $data);
  41.                 return;
  42.         }
  43.         function update_post($postId, $data){
  44.                 $id = $postId;         
  45.                 $this->db->where('id',$id);
  46.                 $this->db->update('posts', $data);
  47.                 return;
  48.         }
  49.         function delete_post($postId){
  50.                 $id = $postId;         
  51.                 $this->db->delete('posts',array('id' => $id));
  52.                 return;
  53.         }
  54. }

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 :
  1. <!DOCTYPE html>
  2.         <head>
  3.                 <link rel="stylesheet" type="text/css" href="<?php echobase_url('style.css');?>">
  4.         </head>
  5.         <body>
  6.                 <h1><?php echo anchor('','My Blog'); ?></h1>
  7.                 <div id="loginDiv"><?php echo anchor('admin/logout','Logout');?></div>
  8.                 <hr/>

Create index.php at this location :

/application/views/admin/index.php

Copy the code below :
  1. <?php
  2.         echo '<p>Welcome To The Admin Page '.$username.'! All posts available for edit or deletion is listed below.</p><br/>';
  3.         echo anchor('admin/create','Create New Post');
  4.         $count = count($post['id']);
  5.         for ($i=0;$i<$count;$i++)
  6.         {
  7.                 echo '<div class="postDiv">';
  8.                 echo '<h4>'.$post['title'][$i];
  9.                 echo anchor('blog/view/'.$post['id'][$i],' [view]');
  10.                 echo anchor('admin/edit/'.$post['id'][$i],' [edit]');
  11.                 echo anchor('admin/delete/'.$post['id'][$i],' [delete]</h4>');
  12.                 echo '<p>'.$post['summary'][$i].'</p>';
  13.                 echo '</div>';
  14.         }
  15. ?>

reate create.php at this location :

/application/views/admin/create.php

Copy the code below :
  1. <?php
  2.         echo validation_errors();
  3. ?>
  4.         <h4>Create A New Post Below</h4>
  5.         <form action="" method="post" >
  6.         <p>Title:</p>
  7.         <input type="text" name="title" size="50"/><br/>       
  8.         <p>Summary:</p>
  9.         <textarea name="summary" rows="2" cols="50"></textarea><br/>
  10.         <p>Post Content:</p>
  11.         <textarea name="content" rows="6" cols="50"></textarea><br/>
  12.         <input type="submit" value="Save" />
  13. <?php
  14.         echo anchor('admin','Cancel');
  15. ?>
  16.         </form>

Create edit.php at this location :

/application/views/admin/edit.php

Copy the code below :
  1. <?php
  2.         echo validation_errors();
  3. ?>
  4.         <h4>Edit "<?php echo $post['title']; ?>" Below</h4>
  5.         <form action="" method="post" >
  6.                 <p>Title:</p>
  7.                 <input type="text" name="title" size="50" value="<?php echo$post['title']; ?>"/><br/> 
  8.                 <p>Summary:</p>
  9.                 <textarea name="summary" rows="2" cols="50"><?php echo$post['summary']; ?>
  10.                 </textarea><br/>
  11.                 <p>Post Content:</p>
  12.                 <textarea name="content" rows="6" cols="50"><?php echo$post['content']; ?>
  13.                 </textarea><br/>
  14.                 <input type="submit" value="Save" />
  15. <?php
  16.         echo anchor('admin','Cancel');
  17. ?>
  18.         </form>

Simple CRUD in Laravel Framework

Creating, reading, updating, and deleting resources is used in pretty much every application. Laravel helps make the process easy using re...