• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Store Image path into DB

#1
[eluser]Shella[/eluser]
Hi,
should be a pretty easy thing to do but is couple of hours I'm trying invane.
In PHP it's pretty easy when you upload a file you have to catch the $_FILES data but here...?

The point is that I have no error back in the select I just have an empty value.

I guess is the model to understand.

VIEW
Code:
[...]
echo form_open_multipart('backend/write_article');
[...]
echo form_upload('article_file');
[...]
echo form_submit('submit', 'Publish', $attributes['class']);
echo form_submit('submit', 'Reset');
echo form_close();
[...]

CONTROLLER
Code:
[...]
$this->load->model('articles_model');
  
   if($query = $this->articles_model->create_article())
   {
    //some stuff...
   }
[...]

MODEL
Code:
function create_article()
{
  $created_on = date ("Y-m-d H:i:s", time());
  
  $new_article_insert_data = array(
  'title' => $this->input->post('title'),
  'content' => $this->input->post('content'),
  'image' => $this->input->post('article_file'), //THIS IS THE POINT
  'id_category' => $this->input->post('category'),
  'created_on' =>  $created_on,
  );
  
  $insert = $this->db->insert('articles', $new_article_insert_data);
  return $insert;
}

Thanks for help

#2
[eluser]NotDior[/eluser]
Check out the documentation for the file uploader class (assuming that's what your using).

$this->upload->data() - will give you an array of file information your looking for and you can pass to your model.

http://ellislab.com/codeigniter/user-gui...ading.html

#3
[eluser]Shella[/eluser]
Thanks for reply.
Unfortunately this is exactly my drama.

If I use this code:

Code:
$new_article_insert_data = array(
  'title' => $this->input->post('title'),
  'content' => $this->input->post('content'),
  'image' => $this->upload->data['file_name'], //THIS IS A TRY
  'id_category' => $this->input->post('category'),
  'created_on' =>  $created_on,
  );

I get this error:

Severity: Notice

Message: Undefined property: CI_Upload::$data

Filename: models/articles_model.php

...and if I use this:

Code:
$new_article_insert_data = array(
  'title' => $this->input->post('title'),
  'content' => $this->input->post('content'),
  'image' => $this->upload->data('file_name'), //THIS IS ANOTHER TRY
  'id_category' => $this->input->post('category'),
  'created_on' =>  $created_on,
  );



I get this error:

Severity: Notice

Message: Array to string conversion

Filename: mysql/mysql_driver.php

This last is trying to write a general "array" into the database:

Code:
INSERT INTO `articles` (`title`, `content`, `image`, `id_category`, `created_on`) VALUES ('Post Title', '\r\n Example data', Array, '44', '2012-06-14 15:19:28')

...I feel so dumb.. but I can't understand this logic...



#4
[eluser]NotDior[/eluser]
Shella,

Sorry for the delay on getting back to you. It appears to me that part of the problem might be that you are trying to grab your upload data directly in the model rather than throwing that data into an array() in your controller and then passing that array to your model. See if something like this gets you down the road.

CONTROLLER
Code:
[...]
$this->load->model('articles_model');
  $aData['title'] = $this->input->post('title');
  $aData['content'] = $this->input->post('content');
  $aData['id_category'] = $this->input->post('category');
  $aData['created_on'] =  date ("Y-m-d H:i:s", time());

   $aData['fileinfo'] = $this->upload->data();
   if($query = $this->articles_model->create_article($aData))
   {
    //some stuff...
   }
[...]

MODEL
Code:
function create_article($aData)
{

  // you'll have to play around with the $aData to get it formatted exactly correct...
  return($this->db->insert('articles', $aData));
  
}

Lastly in your last post/example it looks like $this->upload->data('file_name') is an array for some reason.

#5
[eluser]Shella[/eluser]
Hi! So sorry I'm just back to my desk today and I'm bit late too!
I solved the issue. My problem was about a misunderstanding using this MVC way. I'm pretty new with this programming technic.

I thank you very much for your help.

Have a good day!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.