开发者

Delete Images -> codeigniter

I have reworded my question to help gain my specific answer, sorry if my last question was confusing:

I have an issue with trying to delete a row from the database but also incorporate how to unlink the to images from different locations. I cannot get the row to delete or the images to unlink.

Here is my -> db structure

My image locations are:

fullpath = /includes/uploads/gallery/

thumbpath = /includes/uploads/gallery/thumbs/

Ideal Situation:

What my ideal situation would be is when the delete link is clicked that it will run the function within the index function and then reload the same page with a success message -> I have not successfully been able to get them to work.

My code:

Model:

function deleteImage($id,$path){

        $this->db->delete('images', arra开发者_如何学Goy('id' => $id));

        if($this->db->affected_rows() >= 1) {
            if(unlink($path)
                return TRUE;
            } else{
                return FALSE;
            }
        }    

View:

    <?php if(is_array($get_images)): ?>

            <?php foreach($get_images as $image): ?>
            <img src ="<?=base_url()?>includes/uploads/gallery/thumbs/<?=$image->thumbpath?>" alt="<?= $image->description?>"> <a href="deleteimage/delete/<?=$image->id?>">Delete</a>
            <?php endforeach; ?>
    <?php endif; ?>

Controller:

   function index() { 
    if(!$this->session->userdata('logged_in')) { 
        redirect('admin/home'); 
    } 
    // Main Page Data 
    $page['get_images'] = $this->image_model->getImages();
    $data['cms_pages'] = $this->navigation_model->getCMSPages();
    $data['title'] = 'Delete Gallery Image'; 
    $data['content'] = $this->load->view('admin/deleteimage',$page,TRUE); 

    $this->load->view('admintemplate', $data);

}


function delete_data($record_id)
{
    $query =  $this->db->get_where('projukti_committee',array('record_id' => $record_id));
    if( $query->num_rows() > 0 )
    {
        $row = $query->row();
        $picture = $row->picture;
        unlink(realpath('assets/photo/'.$picture));
        $this->db->delete('projukti_committee', array('record_id' => $record_id));
        return true;
    }
    return false;   
} 


What you're looking for if you're using active record is:

$this->db->delete();

Now from memory this will only return false if an error has occurred, so will constantly return true. But it's ok we can use another function to check whether the row was deleted.

Oh and you will also want to pass into the function the path of the file you want deleting.


function deleteImage($id,$path) {

$this->db->delete('table', array('id' => $id));

    if($this->db->affected_rows() >= 1){
        if(unlink($path))
        return TRUE;
    } else {
        return FALSE;
    }

}


Presumably you have an image ID of some sort.

Append the ID to the deleteimage/delete URI.

Create a method that deletes the ID from your table, and use PHP's unlink($path) function to actually delete the image.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜