0

我的表结构如下所示:

===============================
  身份证 | 姓名 | 图片|
===============================

现在我想要多个带有名称的图像。我该怎么做。

if($this->request->is('post'))
        {
            if($this->Breakdown->saveAll($this->request->data['Breakdown']))
            {
            //Image insertion Successfully
            $image=$this->request->data['Breakdown'][0]['image1']['name'];
            if(!empty($this->request->data['Breakdown'][0]['image1']['name'])) 
                {
                    $image_path = $this->Image->upload_image_and_thumbnail($image,$this->data['Breakdown'][0]['image1'],600,450,180,120, "Breakdown");
                    //echo "This".$image_path;
                    if(isset($image_path)) 
                    {
                        $this->Breakdown->saveField('image',$image_path);
                        $uploaded = true;
                    }
                }
               $this->Session->setFlash('Quantity Breakdown has been added Successfully.', 'default', array('class' => 'oMsg1 oMsgError1'));
               $this->redirect('qty_breakdown');
            }
            else
            {
                 $this->set('errors', $this->Breakdown->invalidFields());   
            }

有没有其他方法可以使用循环插入和上传多个图像?

4

1 回答 1

1

我不确定我是否理解您的要求,但如果您的意思是遍历每个“故障”实体,我会这样做:

foreach($this->request->data['Breakdown'] as $breakdown){
    //Image insertion Successfully
    $image = $breakdown['image1']['name'];
    if(!empty($breakdown['image1']['name'])){
        $image_path = $this->Image->upload_image_and_thumbnail($image,$this->data['Breakdown'][0]['image1'],600,450,180,120, "Breakdown");
            //echo "This".$image_path;
            if(isset($image_path)){
                $this->Breakdown->saveField('image', $image_path);
                $uploaded = true;
            }
        }
        $this->Session->setFlash('Quantity Breakdown has been added Successfully.', 'default', array('class' => 'oMsg1 oMsgError1'));
        $this->redirect('qty_breakdown');
}

更新:29/04/13

为了能够在视图中修改或添加多条记录,您需要稍微不同地配置视图:

// open the form
echo $this->Form->create('Breakdown');
// set the upper-bound limit of $i to whatever you like
for($i = 0; $i < 5; $i++){
    echo $this->Form->input(sprintf("Breakdown.%d.image1", $i), array('type' => 'file'));
    // add other fields using similar syntax as above:
    // Example: "Breakdown.1.name"
}
// close the form
echo $this->Form->end();

资源

于 2013-04-28T15:37:44.993 回答