I am not sure if my questions is worded correctly so please feel free to tell me to change it.
I am allowing for multiple images to be uploaded at once and then inserting text into the database as an html img.
Here is my code:
if ( ! $this->upload->do_upload('post_image'))
{
$this->session->set_flashdata('post_message', $this->upload->display_errors());
$this->session->set_flashdata('post_message_class', 'alert-danger');
redirect('/user/profile/'.$identity, 'refresh');
}
else
{
$uploaded = $this->upload->data();
// insert pos
if(isset($uploaded['file_name']) && $uploaded['file_name'] == 0){
$post_text = '<img src="/uploads/images/'.$uploaded['file_name'].'" width="251px" alt="'.$uploaded['raw_name'].'" /><br>'.$this->input->post('post_text');
}
else
{
foreach ($uploaded as $images){
$post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');
}
}
$query = $this->user_model->insert_user_posts($this->input->post('poster_id'), $this->input->post('profile_id'), $this->input->post('post_type'), $post_text);
$this->session->set_flashdata('post_message', 'Image has been posted!');
$this->session->set_flashdata('post_message_class', 'alert-success');
redirect('/user/profile/'.$identity, 'refresh');
}
EDIT: Here is the code to the model:
public function insert_user_posts($poster_id, $profile_id, $post_type, $post_text)
{
// Users table.
$data = array(
'poster_id' => $poster_id,
'profile_id' => $profile_id,
'post_type' => $post_type,
'post_text' => $post_text,
'datetime' => time()
);
$this->db->insert($this->tables['users_posts'], $data);
if($this->db->affected_rows() > 0)
{
$this->set_message('upload_successful');
return TRUE;
}
else
{
return FALSE;
}
}
Also, I am using this: https://github.com/avenirer/MY_Upload
If I echo or var_dump the $post_text it will show the data for both images, but it is only inserting the first image. What am I doing wrong here?
foreach ($uploaded as $images){you are overwriting$post_textwith every image. I imagine you will only insert the last image because of this. In addition you should post the code atuser_model->insert_user_postsas that could be the issue as well.$uploadedvariable has multiple values in it that we can assume are images. What are you planning to do with those? Do you want to append them together in the same$post_textvariable to be inserted or do an insert for each one?$post_textvariable.