Welcome Guest, Not a member yet? Register   Sign In
Urgent help with multiple updates in DB, please!
#1

[eluser]murphy2006[/eluser]
Hello Guys,

I have a simple problem. I am developing an online photo album and I want the users to be able to update ALL image descriptions on one page.

How can this be done? I have tried with some kind of foreach loop but fail.

I have namned each description field desc[] and added an hidden ID field for each image by the name of the_id[] with the database ID of the image.

The controller looks like the below code, what is wrong, who can it be fixed?

Code:
///////////////////////////////////////////////////////////
        //
        //  Gather the incoming data into variables
        //
        ///////////////////////////////////////////////////////////

        $owner     = $this->session->userdata('id');
      
    foreach ($_POST['desc'] as $desc) {

        foreach ($_POST['id'] as $id) {

        $the_id = $id;
        
        }
              
        ///////////////////////////////////////////////////////////
        //
        //  Put the incoming date in variables
        //
        ///////////////////////////////////////////////////////////

        $data = array(
                           'aco_desc'     => $desc
                );
                
        ///////////////////////////////////////////////////////////
        //
        //  Delete the selected feed
        //
        ///////////////////////////////////////////////////////////
          
         $this->db->where('aco_owner', $owner);  
         $this->db->where_in('aco_id', $the_id);  
           $this->db->update('fe_albums_content', $data);
          
           }

The problem is that it isnĀ“t updating each image only the last one.

Thankful for a quick reply.
#2

[eluser]Sumon[/eluser]
echoing is the best method to debug these sort of code. In short i feel, you might have problem in assigning $_POST['id']. i mean the view file where you assign id[] array. please print id values on top of this function to view whether your have assigned all ids correctly.

Lets have a look later what's come up
#3

[eluser]murphy2006[/eluser]
Hi Sumon,

The form items look like this in the view file:

Code:
<input type="text" class="textbox" name="desc[]" value="" />
<input type="hidden" name="id[]" value="6" /></div>

<input type="text" class="textbox" name="desc[]" value="" />
<input type="hidden" name="id[]" value="7" /></div>

<input type="text" class="textbox" name="desc[]" value="" />
<input type="hidden" name="id[]" value="8" /></div>

Thanks!

/Daniel
#4

[eluser]Sumon[/eluser]
Ops!! the my former thought is totally wrong. because you loop $_POST['id'] inside outer loop. So without any doubt $the_id should be the last value of the hidden field.
Change your code as
Code:
$i=0;
foreach ($_POST['desc'] as $desc) {

        $the_id = $_POST['id'][$i++];
              
        ///////////////////////////////////////////////////////////
        //
        //  Put the incoming date in variables
        //
        ///////////////////////////////////////////////////////////

        $data = array(
                           'aco_desc'     => $desc
                );
                
        ///////////////////////////////////////////////////////////
        //
        //  Delete the selected feed
        //
        ///////////////////////////////////////////////////////////
          
         $this->db->where('aco_owner', $owner);  
         $this->db->where_in('aco_id', $the_id);  
           $this->db->update('fe_albums_content', $data);
          
           }
now lets see what's happening.
#5

[eluser]murphy2006[/eluser]
Fantastic Sumon!

It works like a charm.

Thank you and have a nice weekend.

Kind Regards,
Daniel
#6

[eluser]murphy2006[/eluser]
Hi again,

Another small problem has arisen. I am using SWFupload for the images and when I upload an image it works perfectly fine but the database get 12 extra posts + plus the correct post. Why is that, I think it has to be something with the looping process.

Code:
if ( ! $this->upload->do_upload('Filedata'))
            
        {
                $error = array('error' => $this->upload->display_errors());
                
                $this->load->view('albums_upload', $error);
                
        } else {
              
                $data = array('upload_data' => $this->upload->data());
            
                $res = $this->upload->data();

        $i=0;

        foreach ($res as $rese) {

        $test =$i+1;

        ///////////////////////////////////////////////////////////
        //
        //  Create the mini image
        //
        ///////////////////////////////////////////////////////////

        $imgfile = $res['full_path'];
        
        ///////////////////////////////////////////////////////////
        //
        //  Load the Image Library
        //
        ///////////////////////////////////////////////////////////
            
        $this->load->library('image_lib');

        ///////////////////////////////////////////////////////////
        //
        //  Set the configuration for the mini image
        //
        ///////////////////////////////////////////////////////////

        $config['image_library']     = 'GD2';
        $config['source_image']     = $imgfile;
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']     = FALSE;
        $config['quality']             = '95%';
        $config['width']             = 51;
        $config['height']             = 50;
        $config['master_dim']         = 'width';
    
        $this->image_lib->initialize($config);
        $this->image_lib->resize();

        ///////////////////////////////////////////////////////////
        //
        //  Set image size
        //
        ///////////////////////////////////////////////////////////
            
        $width     = $res['image_width'];
        $height = $res['image_height'];
                    
        ///////////////////////////////////////////////////////////
        //
        //  Set the configuration for the master image
        //
        ///////////////////////////////////////////////////////////
            
        $config2['image_library']     = 'GD2';
        $config2['source_image']     = $imgfile;
        $config2['create_thumb']     = FALSE;
        $config2['maintain_ratio']     = TRUE;
        $config2['quality']         = '95%';
        
        if ($width > 500) {
        
        $config2['width']            = 500;
        
        } else {
        
        $config2['width']            = $width;
        
        }
  
        if ($height > 300) {
        
        $config2['height']           = 300;
        
        } else {
        
        $config2['height']            = $height;
        
        }      
    
        $this->image_lib->initialize($config2);
        $this->image_lib->resize();
        
        ///////////////////////////////////////////////////////////
        //
        //  Gather the incoming data into variables
        //
        ///////////////////////////////////////////////////////////
        
        $owner             =         $_POST["owner"];
        $album             =         $_POST["album"];
                        
        ///////////////////////////////////////////////////////////
        //
        //  Copy the uploaded files with new name
        //
        ///////////////////////////////////////////////////////////
        
        $random = rand(0,999999999);
        $thumb = "_thumb";
                
        copy($res['file_path'].$res['raw_name'].$res['file_ext'],$res['file_path'].$owner.'_'.$random.$res['file_ext']);
        copy($res['file_path'].$res['raw_name'].'_thumb'.$res['file_ext'],$res['file_path'].$owner.'_'.$random.$thumb.$res['file_ext']);

        ///////////////////////////////////////////////////////////
        //
        //  Delete the old images
        //
        ///////////////////////////////////////////////////////////
            
        $delete = $res['file_path'].$res['raw_name'].$res['file_ext'];
        $delete2 = $res['file_path'].$res['raw_name'].'_thumb'.$res['file_ext'];
        
        unlink($delete);
        unlink($delete2);
                
        ///////////////////////////////////////////////////////////
        //
        //  Put the incoming date in variables
        //
        ///////////////////////////////////////////////////////////
        
        $new_name = $owner.'_'.$random;

        $date     = now();

        $dbentry = array(
                           'aco_owner'             => $owner,
                           'aco_title'             => $new_name,
                           'aco_album'             => $album,
                           'aco_ext'                 => $res['file_ext'],
                           'aco_created'             => $date
                          
                );
        
        ///////////////////////////////////////////////////////////
        //
        //  Save the image to the database
        //
        ///////////////////////////////////////////////////////////
        
        $this->db->insert('fe_albums_content', $dbentry);
        
        }
                
        ///////////////////////////////////////////////////////////
        //
        //  Display the target page
        //
        ///////////////////////////////////////////////////////////
                      
        $this->load->view('albums_upload', $data);
                
        }
}

Thankful for a quick reply,

/Daniel
#7

[eluser]Sumon[/eluser]
You have used
Code:
$owner  =  $_POST["owner"];
which mean same data for every elements of
Code:
foreach ($res as $rese)
so would you please have a try to change at here.
#8

[eluser]murphy2006[/eluser]
Hi again Sumon,

I replace the $_POST["owner"]; with "" and now the ownership goes to user 0, but the problem remains, there are still 12 extra records for each uploaded image everytime I upload.

What can be wrong, seems to be some kind of looping that goes out of hand.

Thanks,
Daniel
#9

[eluser]Sumon[/eluser]
In a second i can tell you that, you have to set a condition by using base file name. Loop operation will proceed only if the image field is non empty. If not works for you please paste your view (must contain file field) at here.




Theme © iAndrew 2016 - Forum software by © MyBB