iamthestreets
iamthestreets

Reputation: 773

Insert data into database using codeigniter 3

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?

Upvotes: 2

Views: 965

Answers (1)

Will Sampson
Will Sampson

Reputation: 514

If you want to append the images together in the same $post_text variable you have to change this line: $post_text = '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" /><br>'.$this->input->post('post_text');

by changing the assignment operator = to the concatenation operator .= like so:

$post_text .= ...

Instead of overwriting the $post_text variable each time you will instead add each image to the end of it. I can make a guess that your final foreach loop might look something like this:

$post_text = ""; foreach ($uploaded as $images){ $post_text .= '<img src="/uploads/images/'.$images['file_name'].'" width="251px" alt="'.$images['raw_name'].'" />'; } $post_text .= '<br>'.$this->input->post('post_text');

This would create a string that contains all the images in a row followed at the end by '<br>'.$this->input->post('post_text');. Make note that you must first instantiate the $post_text variable to something (here I have set it to an empty string) in order to use the concatenation operator.

Upvotes: 1

Related Questions