
Controller: Handle the Blog Creation
We’ll modify your create_blog
method to correctly handle the creation of a blog post with multiple tags. You’ll need to receive the tags[]
as an array from the form, convert it into a comma-separated string, and store it in the database.
Updated create_blog
Method: php Copy code public function create_blog($id = 0)
{
is_test();
$id = $this->input->post('id');
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('content', 'Content', 'required');
if ($this->form_validation->run() == TRUE) {
// Fetch category and tags from the form input
$category = $this->input->post('category');
if (empty($category)) {
$category = 6; // Default category (Uncategorized)
}
// Get the tags selected by the user (tags[] as an array)
$tags = $this->input->post('tags');
// Convert array of tags to a comma-separated string
$tags_string = implode(',', $tags);
// Generate the slug for the blog post
$slug = url_title($this->input->post('title'), '-', TRUE);
// Prepare blog data to be inserted or updated
$blog_data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'category'=> $category,
'tags' => $tags_string, // Store tags as a comma-separated string
'image' => $this->input->post('image'),
'content' => $this->input->post('content'),
'status' => 1, // Assuming blog is published
'created' => date('Y-m-d H:i:s'),
);
// Handle image upload if provided
if (!empty($_FILES['image']['name'])) {
$config = array(