Welcome to Day 8 Integrating a Blog Section of our journey to build a portfolio website with CodeIgniter! Today, we’ll integrate a blog section into our website. A blog is a great way to share your insights, experiences, and updates with your audience. Let’s get started on implementing this feature using CodeIgniter.
So Start Integrating a Blog Section
Setting Up the Blog Section
Step 1: Create Blog Controller and Views
- Blog Controller: Navigate to
application/controllers/
and create a new file namedBlog.php
. Add the following code:
<?php
class Blog extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('blog_model');
$this->load->library('form_validation');
$this->load->library('session');
}
public function index() {
$data['posts'] = $this->blog_model->get_all_posts();
$this->load->view('header');
$this->load->view('blog/posts', $data);
$this->load->view('footer');
}
public function add_post() {
// Form validation rules for adding a blog post
// Load necessary models and views for adding a blog post
}
public function edit_post($post_id) {
// Form validation rules for editing a blog post
// Load necessary models and views for editing a blog post
}
public function delete_post($post_id) {
// Logic to delete a blog post
}
}
?>
- Blog Views: Create necessary views in
application/views/blog/
directory:posts.php
:
<!-- List of Blog Posts -->
<section>
<h2>Blog Posts</h2>
<?php foreach ($posts as $post): ?>
<div class="post">
<h3><?php echo $post['title']; ?></h3>
<p><?php echo $post['body']; ?></p>
<a href="#">Edit Post</a>
</div>
<?php endforeach; ?>
</section>
add_post.php
:
<!-- Add Blog Post Form -->
<section>
<h2>Add New Post</h2>
<form action="<?php echo base_url('blog/add_post'); ?>" method="post">
<input type="text" name="title" placeholder="Post Title" /><br />
<textarea name="body" placeholder="Post Content"></textarea><br />
<button type="submit">Add Post</button>
</form>
</section>
edit_post.php
:
<!-- Edit Blog Post Form --> <section>
<h2>Edit Post</h2>
<form action="<?php echo base_url('blog/edit_post/'.$post['id']); ?>" method="post">
<input type="text" name="title" value="<?php echo $post['title']; ?>" /><br />
<textarea name="body"><?php echo $post['body']; ?></textarea><br />
<button type="submit">Update Post</button>
</form>
</section>
Step 2: Create Blog Model and Database Operations
- Blog Model: Create
blog_model.php
inapplication/models/
directory. Implement methods to handle blog post CRUD operations:
<?php
class Blog_model extends CI_Model {
public function get_all_posts() {
// Retrieve all blog posts from the database
}
public function add_post() {
// Insert a new blog post into the database
}
public function get_post_by_id($post_id) {
// Retrieve a specific blog post by ID
}
public function update_post($post_id) {
// Update an existing blog post in the database
}
public function delete_post($post_id) {
// Delete a blog post from the database
}
}
?>
Testing the Blog Section
- Access Blog Posts: Open your web browser and go to
http://localhost/your_project_name/index.php/blog
. Ensure you can view all blog posts listed. - Add and Edit Posts: Implement links or buttons in the blog views (
posts.php
,add_post.php
,edit_post.php
) to add new posts, edit existing posts, and delete posts.
Conclusion
Congratulations! You’ve successfully integrated a blog section into your portfolio website using CodeIgniter. Now, you can share your thoughts and updates with your audience through blog posts. In Day 9, we’ll enhance the website with additional features such as search functionality and pagination for blog posts.