Reputation: 19
My table structure is something like below :
============================== Id | Name | Image| ==============================
Now i want to multiple image with name.How can i do this.
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());
}
Is there any alternate way to use loop for inserting and uploading multiple image?
Upvotes: 0
Views: 155
Reputation: 1305
I'm not sure I understand what you're asking but if you meant, to iterate over each 'Breakdown' entity, this is how I would do it:
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');
}
To be able to modify or add multiple records from within your view you need to configure your view a little differently:
// 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();
Upvotes: 1