Welcome Guest, Not a member yet? Register   Sign In
Image Upload help
#1

[eluser]mikegeorgeff[/eluser]
I am trying to create a blog thread with a preview image to go along with the title and the preview text.
I understand how to create an image upload form by itself but I am having a hard time figuring out how to integrate it into the blog post form. I know I have to add it to the add function in the controller but I not sure how to go about doing that. Also the file name will be stored in the database table and the image will upload to a folder called uploads

here is my code

Model MBlog.php
Code:
function add_article()
    {
        $now = date("Y-m-d");
        $data = array(
                    'title' => $this->input->post('title'),
                    'body' => $this->input->post('body'),
                    'author' => $this->input->post('author'),
                    'category_id' => $this->input->post('category_id'),
                    'preview_img' => $this->input->post('preview_img'),  
                    'date' => $now
                    );
                    
        $this->db->insert('articles', $data);
    }

controller blog.php
Code:
function add()
    {
        if ($this->input->post('title'))
        {
            $this->MBlog->add_article();
            $this->session->set_flashdata('message', 'Article Inserted');
            redirect('admin/blog', 'refresh');
        } else {
            $data['title'] = "New Article";
            $data['cats'] = $this->MCats->getCategoriesDropDown();
            $this->load->view('admin/new_article', $data);
        }
    }

view new_article.php
Code:
<?php
echo form_open_multipart('admin/blog/new_article');

echo "<p><label>Author</label><br />";
$data = array('name' => 'author', 'id' => 'author', 'size' => 25);
echo form_input($data) ."</p>";

echo "<p><label>Title</label><br />";
$data = array('name' => 'title', 'id' => 'title', 'size' => 25);
echo form_input($data) ."</p>";

echo "<p><label>Preview Image (max size: 434x240)</label><br />";
$data = array('name' => 'preview_img', 'id' => 'preview_img', 'size' => 25);
echo form_upload($data) ."</p>";

echo "<p><label>Category</label><br />";
echo form_dropdown('category_id', $cats) ."</p>";

echo "<p><label>Content</label><br />";
$data = array('name' => 'body', 'id' =>'body', 'rows' => 5, 'cols' => 50);
echo form_textarea($data) ."</p>";

echo form_submit('submit', 'Post It');

echo form_close();
?&gt;
#2

[eluser]leonardteo[/eluser]
Heya,

Well, I won't post the full solution but kinda point you in the right direction.

In your controller function here:
Code:
function add_article()
    {

//Add file upload handling code here. Look here on how to do that:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

//Get the path to the image that is uploaded. Use that for the 'preview_img' string.

        $now = date("Y-m-d");
        $data = array(
                    'title' => $this->input->post('title'),
                    'body' => $this->input->post('body'),
                    'author' => $this->input->post('author'),
                    'category_id' => $this->input->post('category_id'),
                    'preview_img' => $this->input->post('preview_img'),  
                    'date' => $now
                    );
                    
        $this->db->insert('articles', $data);
    }




Theme © iAndrew 2016 - Forum software by © MyBB