【问题标题】:Codeigniter Inserting multiple images in to database and retrieving them per unique IDCodeigniter 将多个图像插入数据库并根据唯一 ID 检索它们
【发布时间】:2014-01-15 01:01:53
【问题描述】:

我正在建立一个允许用户出售汽车的网站。用户在表单中填写了必填字段,其中一个必填字段正在上传该特定汽车的图像。我的代码一切正常,期望这样一个事实,即当用户上传多张图像时,我将有两列具有相似值的期望上传的图像和汽车 ID,这使我很难检索图像。如果有人知道我正确存储这些图像并检索它们的更好方法,我将不胜感激。这是我的数据库结构:

汽车ID |想要|制作 |型号 |年份 |公里 |传输 |位置 |价格 |手机 |图片

这是我的控制器:

 // 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());
    }

这是我的模型

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

}

【问题讨论】:

  • 如果您的汽车有多个图像,那么为什么不为图像维护单独的表格?
  • @PraveenReddy 我是这么认为的,问题出在自动递增的 carID 上。这就是我没有使用该方法的原因,但如果您能建议一种方法,我可以为多张图片会很感激:-)

标签: mysql image codeigniter upload


【解决方案1】:

首先要做的事情很简单,只需为图像创建一个表,其中 carid 作为图像表中的外键。我家 carid 是汽车表中的主键,对吗?如果没有,应该是。

在你插入汽车数据的模型中,你可以在插入后使用 $this->db->insert_id();功能见以下代码。

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

现在在控制器中上传图片后只需使用 add_car() 的返回值将其插入图片文件夹中。

希望对你有帮助!!如果没有,我很乐意为您提供帮助!

【讨论】:

  • 您好 praveen,非常感谢您的帮助,我几乎可以完成我想要的任务,但问题是 add_images() 函数位于 foreach 循环内,并且在 foreach 循环完成后是我插入数据数据的地方。请帮助我将不胜感激:-)
【解决方案2】:

将图像存储在不同的表中,carid 作为外键引用上述表中的carid。检索时连接两个表。不要在表中存储冗余数据。

【讨论】:

  • 挑战是 carid,carid 与图像同时生成,它会自动递增,因此您不能在汽车表和图像表中拥有相同的 carid,这就是我的原因寻求解决此问题的方法。非常感谢任何建议....
  • 首先在汽车表中插入汽车数据并从表中获取 last_inserted_id 然后循环上传的图像并使用最后插入的 id 插入图像表(在汽车表中插入汽车数据时获得的自动增量值) 作为外键引用 carid(汽车表中的主键)。
猜你喜欢
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 2013-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
相关资源
最近更新 更多