Welcome Guest, Not a member yet? Register   Sign In
Help needed with multi upload, extra text fields and database
#1

[eluser]StefanAlexandru[/eluser]
Hi again everyone,

I do know that this question have been asked million times before, but I just can't find way to make this thing work.

So this is the scenario :
I am building a classified ad site, and I need to be able to make a form with 3 images and a few other fields with information and than to store the information in to the database. After searching more than 3 hours on google I've finally decided to ask you guys for a good workaround.
The below controller code has been taken also from this forum, but I need somehow to make this code to also insert the other fields into the database, not only the images.

Controller :

Code:
<?php
$config['upload_path'] = './uploads';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']    = '2048'; //2 meg
            $config['encrypt_name']    = TRUE;
            $this->load->library('upload', $config);
            $this->load->library('image_lib');
            //Upload error flag
            $error = FALSE;

            foreach($_FILES as $key => $value)
            {
                if( !empty($value['name']))
                {
                    if ( $this->upload->do_upload($key) )
                    {
                        $uploaded = $this->upload->data();

                        //Creat Thumbnail
                        $config['image_library'] = 'GD2';
                        $config['source_image'] = $uploaded['full_path'];
                        $config['create_thumb'] = TRUE;
                        $config['thumb_marker'] = '_tn';
                        $config['master_dim'] = 'width';
                        $config['quality'] = 75;
                        $config['maintain_ratio'] = TRUE;
                        $config['width'] = 175;
                        $config['height'] = 175;

                        $this->image_lib->clear();
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();

                        $imagename = $uploaded['file_name'].'_tn'.$uploaded['file_ext'];
                        $timestamp = time();

                        
                        $this->db->set('file', $imagename);
                        $this->db->insert('photos');
                    }
                    else
                    {
                        $error = TRUE;
                    }
                }
            }
//If we have some error...
if($error) $this->session->set_flashdata('notice', '<div class="error icon"><ol>'.$this->upload->display_errors('<li>','</li>').'</ol></div>');

else $this->session->set_flashdata('notice', '<p class="success icon">¡Success!</p>');

//Call the view
$this->load->view('upload_photos');
}
}


The real problem with this controller is that if I am trying to insert also the text field information when I am inserting the images, because it's in the foreach loop, it will insert the information 3 times.
I just can't find the logic to this operation correct.

My ads table is looking like this :

id title content someotherfield1 someotherfield2 image1 image2 image3

If there is any tutorial already posted somewhere please point me to it, because as I said above, I've searched for a workaround for more than 3 hours, and I was not successful.


Thanks in advance and sorry for my bad english.




Theme © iAndrew 2016 - Forum software by © MyBB