Welcome Guest, Not a member yet? Register   Sign In
Updating Multiple Rows Within a Single Form
#1

(This post was last modified: 01-24-2019, 01:56 PM by demyr.)

Hello everybody,

I have got 2 tables on my db: Products and Productimages

I'm trying to update ordering of the images on Productimages table which are related to a product:

My Productimages table:
    imageid  - imagepath - productid - ordering
 
I reach to the update page via product id (on url: update-product-image-order/3). This update page shows only the images related to this product (in this example, it is 3)

On this page I am calling images via a "foreach" and all images have the same 'name'.

And, no matter what I tried, I couldn't make it. 

My View: 

PHP Code:
<form role="form" method="post" action="<?php echo base_url('cp/productimageneworder'); ?>">
<?
php 
                                             
foreach($images as $img) { ?>
                            
 <img class="current-imgs" src="<?php echo $img->imagepath?>" alt="<?php echo $img->imgdata?>">
 <input type="hidden" name="relatedphoto" value="<?php echo $img->imgid?>">
                                
<select id="ordering" name="order">
                                    
<option>Choose the new order for the photo?</option>
<option value="0" <?php if($img->ordering==0){echo 'selected';}?>>0</option>
<option value="1" <?php if($img->ordering==1){echo 'selected';}?>>1</option>
<option value="2" <?php if($img->ordering==2){echo 'selected';}?>>2</option>
<option value="3" <?php if($img->ordering==3){echo 'selected';}?>>3</option>
<option value="4" <?php if($img->ordering==4){echo 'selected';}?>>4</option>
<option value="5" <?php if($img->ordering==5){echo 'selected';}?>>5</option>
                                
</select>
                                                              
<div class="clr"></div>
                                                                
<?php ?>

How should I set up my controller and my model? I would be grateful if you could show me the way.
Reply
#2

Hi, your table has names like:
 Productimage:
    imageid  - imagepath - productid - ordering

but why you use 
src = echo $img->imgpath02;

from where you has that 02 number in imagepath?
this is a example how i used the foreach styntaxis in Codeigniter 3.1.1
https://codeigniter.com/user_guide/gener...ht=foreach
Reply
#3

(01-24-2019, 10:25 AM)Eduardo-Peralta Wrote: Hi, your table has names like:
 Productimage:
    imageid  - imagepath - productid - ordering

but why you use 
src = echo $img->imgpath02;

from where you has that 02 number in imagepath?
this is a example how i used the foreach styntaxis in Codeigniter 3.1.1
https://codeigniter.com/user_guide/gener...ht=foreach

Dear Eduardo-Peralta, thanks for your message. Actually that is not the reason for any mistake. Because I typed here wrong :Smile Correcting my typo.
Reply
#4

Following the tutorial here on YouTube I have made it work! But, I'm ready to hear any other solution or alternatives.

First add brackets to names:

PHP Code:
<input type="hidden" name="relatedphoto[]" value="<?php echo $img->imgid; ?>">
 
                               
<select id="ordering" name="order[]"


The Controller :

PHP Code:
 function productimageneworder(){
 
  
  
       $data 
$this->input->post(); //first create a blank array
 
      
       
for($i=0$i<count($data['relatedphoto']);$i++) // as it is a foreach, we must do increment +1 until the last photo
 
      {
 
          $batch[]=array('imageid'=>$data['relatedphoto'][$i],
 
                       'ordering'=>$data['order'][$i]
 
          
           
);
 
      }
 
      $result $this->db->update_batch('productimages'$batch'imageid');
 
      if($result){
 
              $this->session->set_flashdata('ordering-success''New Ordering Has Been Done With a Great Success');
 
          redirect($_SERVER['HTTP_REFERER']);
 
      }
 
  
       
   
  


As I said, Even though I've found a solution, I'm ready to see your alternatives.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB