1

On my view I need to be able to get the hidden banner_image_id and use that in my $this->db->where('banner_image_id', $id) on my model function

When I update the image. It does not update to the correct row. Instead it update all rows with the same filename.

Question: On my model how can I make sure that my $this->db->where('banner_image_id', $id) updates correct rows.

I have done a vardump($id) and result is string(2) "63" which is correct but still updates every row the same. I tried update_batch also no luck.

Model Function

public function edit_banner_image($file_name) {
    $banner_image_id = $this->input->post('banner_image_id');

    if (isset($banner_image_id)) {
        foreach ($banner_image_id as $id) { 
            //var_dump($id);
            //exit;
            $data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);

            $this->db->where('banner_image_id', $id);

            //$this->db->where_in('banner_image_id', $id); // Tried No Luck
            //$this->db->or_where_in('banner_image_id', $id); // Tried No Luck

            $this->db->update($this->db->dbprefix . 'banner_img', $data);
        }
    }
}   

View Table

<table>
    <tbody>
        <?php foreach ($banner_images as $img) { ?>
        <tr>
            <td>
                <?php echo $img['banner_image_id'];?>
                <input type="hidden" name="banner_image_id[]" value="<?php echo $img['banner_image_id'];?>">
           </td>
           <td>
                <input type="file" name="banner_image[]" multiple size="20">
           </td>
           <td>
               <img src="<?php echo base_url() . 'uploads/' . $img['banner_image'];?>" />
               <input type="hidden" name="banner_image" value="<?php echo $img['banner_image'];?>">
           </td>
       </tr>
       <?php }?>
    <tbody>
</table>

1 Answer 1

1

Try to concatenate the id numbers in foreach loop and pass it where function at once.

Assuming that your id numbers are array(1,2,5,6).

public function edit_banner_image($file_name) {
    $banner_image_id = $this->input->post('banner_image_id');

    if (isset($banner_image_id)) {

        $bid = 'IN(';
        foreach ($banner_image_id as $id) { 
            $bid .= $id . ',';
        }

        $bid = substr($bid, 0, -1) . ')'; //remove last comma and add closing paranthesis.
        //The content of the variable $bid would be like
        //IN(1,2,5,6)

        $data = array('banner_id' => $this->uri->segment(4),'banner_image' => $file_name);

        $this->db->where('banner_image_id', $id);
        $this->db->update($this->db->dbprefix . 'banner_img', $data);

    }
}   

Another Suggestion:

Convert $banner_image_id to a normal array in case of it is an associative array.

public function edit_banner_image($file_name) {
    $banner_image_id = $this->input->post('banner_image_id'); 
    $bid = array();

    if (isset($banner_image_id)) {
        foreach ($banner_image_id as $id) { 
            $bid[] = $id;
        }

        $data = array('banner_id' => $this->uri->segment(4),
                      'banner_image' => $file_name);

        $this->db->where_in('banner_image_id', $bid);
        $this->db->update($this->db->dbprefix . 'banner_img', $data);    
    }
}  

I suggest you to make convertion in the controller section, not in model. The lines ending with // should be in the controller. You can easily pass $bid to edit_banner_image($file_name, $bid) function then.

public function edit_banner_image($file_name, $bid) {
    $banner_image_id = $this->input->post('banner_image_id'); //
    $bid = array();//

    if (isset($banner_image_id)) {//
        foreach ($banner_image_id as $id) { //
            $bid[] = $id;//
        }

        $data = array('banner_id' => $this->uri->segment(4),
                      'banner_image' => $file_name);

        $this->db->where_in('banner_image_id', $bid);
        $this->db->update($this->db->dbprefix . 'banner_img', $data);    
    }
}  

Thus, you will have more readable code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.