Welcome Guest, Not a member yet? Register   Sign In
Upload images along with my news items ?
#1

[eluser]invision[/eluser]
Hi,

Recently I built an incredibly basic news management system for my own portfolio site.

I'd like to extend this to show thumbnails for each news post.

I would love to get a little help in doing this. I've attached my code so far for reference.

Here's my Model:
Code:
function addNews(){
     $now = date("Y-m-d H:i:s");
    $data = array(
        'title' => $this->input->post('title'),
        'slug' => $this->input->post('slug'),
        'status' => $this->input->post('status'),
        'body' => $this->input->post('body'),
        'category_id' => '1',
        'user_id' => $_SESSION['userid'],
        'pubdate' => $now
    );
$this->db->insert('news', $data);
}

My View:
Code:
<h1>&lt;?php echo $title;?&gt;</h1>

&lt;?php
//echo form_open('admin/news/create');
echo form_open_multipart('admin/news/create');
echo "<p><label for='ptitle'>Title</label><br />";
$data = array('name'=>'title','id'=>'ptitle','size'=>25);
echo form_input($data) ."</p>";

echo "<p><label for='ptags'>Tags</label><br />";
$data = array('name'=>'tags','id'=>'ptags','size'=>25);
echo form_input($data) ."</p>";


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


echo "<p><label for='status'>Status</label><br />";
$options = array('draft' => 'draft', 'published' => 'published');
echo form_dropdown('status',$options) ."</p>";

echo "<p><label for='status'>Status</label><br/>";
?&gt;
&lt;input type="file" name="userfile" size="20" /&gt;
&lt;?php

echo form_submit('submit','create post');
echo form_close();

?&gt;

and my Controller:
Code:
function create(){
       if ($this->input->post('title')){
          $this->MNews->addNews();
          $this->session->set_flashdata('message','News created');
          redirect('admin/news/index','refresh');
      } else{
        $data['title'] = "Create News";
        $data['main'] = 'admin_news_create';
        $this->load->vars($data);
        $this->load->view('dashboard');    
     }
  }

I was using this as a resource but I struggled with it.

I basically want to upload an image to be associated with each news entry.


I'd love some help with this.



Many thanks.
#2

[eluser]farinspace[/eluser]
Sorry if i don't get too detailed, but hopefully I can point you in the right direction. Your "addNews" method will need something like this to handle the uploaded file:

Code:
$config = array
(
    'upload_path' => '/path/to/uploads/dir/'
    , 'allowed_types' => 'gif|jpg|jpeg|png'
    , 'max_size' => 500 // 500KB
    , 'overwrite' => FALSE
);

$this->load->library('upload', $config);

if ($this->upload->do_upload('userfile'))
{
    $data = $this->upload->data();

    // save image here
    // echo $data['file_name'];
}
#3

[eluser]invision[/eluser]
Thanks for the reply.

I actually did try something like that before posting here. I'll give it another shot and see how I go.
Will be back shortly Smile
#4

[eluser]Phil Sturgeon[/eluser]
What does the code for your attempt look like.
#5

[eluser]invision[/eluser]
Many thanks for the reply Phil.

Code:
function addNews(){
$now = date("Y-m-d H:i:s");
$data = array(
        'title' => $this->input->post('title'),
        'slug' => $this->input->post('slug'),
        'status' => $this->input->post('status'),
        'body' => $this->input->post('body'),
        'category_id' => '1',
        'user_id' => $_SESSION['userid'],
        'pubdate' => $now
    );
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size']    = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';

$this->load->library('upload', $config);

$data = array('upload_data' => $this->upload->data());
//$this->upload->do_upload()

$this->db->insert('news', $data);
}

I've tried to put this together from the Upload class at CI : http://ellislab.com/codeigniter/user-gui...ading.html
#6

[eluser]Phil Sturgeon[/eluser]
You have commented out the single most important line.

Code:
$this->upload->do_upload()
#7

[eluser]invision[/eluser]
<snip>
#8

[eluser]invision[/eluser]
<snip>
#9

[eluser]invision[/eluser]
<snip>
#10

[eluser]invision[/eluser]
<snip>




Theme © iAndrew 2016 - Forum software by © MyBB