0

I am building a site that let user sell their cars. a user fills in the form with necessary fields with one of the required fields being uploading images of that specific car.everything works fine with my code expect the fact that when a user upload multiple images i will have two columns with similar values expect for the uploaded images and the carID which makes it hard for me to retrieve the images.if anyone knows the better way for me to store these images correctly and retrieving them i would appreciate. Here is my database structure:

CarID | want_to | make | model |year | kms | transmission | location |price |mobile | image

and here is my controller:

 // Upload new  car
    function add()
    {
          $this->load->library('form_validation');
     $this->form_validation->set_rules('list_options', 'Options', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('make', 'Make', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('model', 'Model', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('year', 'Year', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('kms', 'kms', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('transmission', 'Transmissions', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('location', 'Location', 'trim|required|min_length[2]|xss_clean');
      $this->form_validation->set_rules('price', 'Price', 'trim|required|min_length[2]|xss_clean');
     $this->form_validation->set_rules('mobile', 'Mobile', 'trim|required|min_length[2]|xss_clean');

        $upload_conf = array(
            'upload_path'   => realpath('images/'),
            'allowed_types' => 'gif|jpg|png',
            'max_size'      => '30000',
            );

        $this->upload->initialize( $upload_conf );

        // Change $_FILES to new vars and loop them
        foreach($_FILES['userfile'] as $key=>$val)
        {
            $i = 1;
            foreach($val as $v)
            {
                $field_name = "file_".$i;
                $_FILES[$field_name][$key] = $v;
                $i++;   
            }
        }
        // Unset the useless one ;)
        unset($_FILES['userfile']);

        // Put each errors and upload data to an array
        $error = array();
        $success = array();

        // main action to upload each file
        foreach($_FILES as $field_name => $file)
        {
            if ( ! $this->upload->do_upload($field_name))
            {
                // if upload fail, grab error 
                $error['upload'][] = $this->upload->display_errors();
            }
            else
            {
                    // otherwise, put the upload datas here.
                    // if you want to use database, put insert query in this loop

                    $upload_data = $this->upload->data();
                     $data=array(
                                    'want_to'=>$this->input->post('list_options'),
                                    'make'=>$this->input->post('make'),
                                    'model'=>$this->input->post('model'),
                                    'year'=>$this->input->post('year'),
                                    'kms'=>$this->input->post('kms'),
                                    'transmission'=>$this->input->post('transmission'),
                                    'location'=>$this->input->post('location'),
                                    'price'=>str_replace(",", "",$this->input->post('price')),
                                    'mobile'=>$this->input->post('mobile'),
                                    'image'=>$upload_data['orig_name'],

                                );
                     $imagedata=array(
                    'imagePath'=>$upload_data['orig_name'],
                    'imageName'=>$upload_data['file_name'],

                        );

                 $this->car_model->add_car($data); 

                // set the resize config
                $resize_conf = array(
                    // it's something like "/full/path/to/the/image.jpg" maybe
                    'source_image'  => $upload_data['full_path'], 
                    // and it's "/full/path/to/the/" + "thumb_" + "image.jpg
                    // or you can use 'create_thumbs' => true option instead
                    'new_image'     => 'images/thumbs/',
                    'width'         => 200,
                    'height'        => 200
                    );

                $medium_conf = array(
                    // it's something like "/full/path/to/the/image.jpg" maybe
                    'source_image'  => $upload_data['full_path'], 
                    // and it's "/full/path/to/the/" + "thumb_" + "image.jpg
                    // or you can use 'create_thumbs' => true option instead
                    'new_image'     => 'images/medium/',
                    'width'         => 600,
                    'height'        => 600
                    );


                // initializing
                $this->image_lib->initialize($resize_conf);
                 //$this->image_lib->initialize($medium_conf);

                // do it!
                if ( ! $this->image_lib->resize())
                {
                    // if got fail.
                    $error['resize'][] = $this->image_lib->display_errors();
                }
                else
                {
                    // otherwise, put each upload data to an array.
                    $success[] = $upload_data;
                }

            }

        }

        // see what we get
        if(count($error > 0))
        {
            $data['error'] = $error;
        }
        else
        {
            $data['success'] = $upload_data;
        }


        redirect(base_url());
    }

and here is my model

function add_car($data)
{
    $this->db->insert('cars',$data);

}
2
  • if your car has multiple images then why don't you maintain separate table for images? Commented Dec 27, 2013 at 12:25
  • @PraveenReddy i thought so , the problem comes with the carID that is auto incremented.that is why i failed to use that method but if you could suggest a way that i could maintain a unique ID for the multiple images would appreciate a lot :-) Commented Dec 27, 2013 at 12:35

2 Answers 2

1

It's simple to do first thing just create a table for images with carid as a foreign key in images table. I home carid is primary key in cars table right? it should be if not.

in the model where you are inserting car data, you can get the carid after inserting it by using $this->db->insert_id(); function see below code.

function add_car($data)
{
  $this->db->insert('cars',$data);
  return $this->db->insert_id(); // it returns primary key value it should be carid.
}

now in controller after uploading image just use the returned value of add_car() insert it in the images folder.

Hope it helped you!! if not i'm happy to help you!!

Sign up to request clarification or add additional context in comments.

1 Comment

Hello praveen,thank you so much for your help,i am almost there to accomplish wat i wanted but the problem is that the add_images() function is inside a foreach loop and after the foreach loop is completed is where i insert the data the data.please help me will appreciate a lot :-)
0

store images in a different table with carid as foreign key referencing carid in above mentioned table.While retrieving join the two tables. Don't store redundant data in the table.

2 Comments

the challenge is the carid,the car id is generated at the same time as the images, it is auto incremented so you cannot have the same carid in cars table and in images table thts is why i am asking for a way to solve this. appreciate a lot for any suggestions....
first insert car data in cars table and get last_inserted_id from the table then loop the uploaded images and insert in images table using last inserted id (auto increment value you got while you inserting in car data in cars table) as foreign key referencing carid (primarty key in the cars table).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.