Steve Bals
Steve Bals

Reputation: 1979

Delete the image codeigniter

My controller is:

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class pickoftheday_display extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        //$this->load->library('upload');
        //$this->load->library('form_validation');
        $this->load->helper('url');
        $this->load->model('pickoftheday_display_model','',TRUE);
        $this->pickoftheday_display_model->get_pickoftheday_image();
        $this->load->database();
    }
    public function index()
    {
                $this->load->view('pickoftheday_display');


    }

    public function get_pickoftheday_image()
    {
        $data['get_pick_picture']=$this->pickoftheday_display_model->get_pickoftheday_image();
    }
}

Model is:

class pickoftheday_display_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('encrypt');
        $this->load->helper('security');
        $this->load->helper('string');
        $this->load->library('email');
    }

    public function get_pickoftheday_image()
    {
        $query=$this->db->query("SELECT * FROM pick_of_the_day order by id desc ");
        if($query->num_rows>0)
        {
            return $query->result();

        }
        else
        {

            return false;
        }
    }
}

View code:

<?php
                        $i=1;
                        foreach($get_pick_picture as $key)
                        {
                        $val= $key->image;
                        $link= $key->link;

                        ?>
                        <tr>
                            <td>
                                 <?php echo $i; ?>
                            </td>
                            <td>
                                <img src="<?php echo base_url(); ?>uploads/pickofday_images/<?php echo $val;?>" style="width:200px;"/>
                            </td>
                            <td>
                                 <a href="<?php echo $link;?>"><?php echo $link;?></a>
                            </td>
                            <td>
                                <a href="" class="label label-success">
                                     Delete</a>
                                </span>
                            </td>
                        </tr>
                        <?php
                        $i++;
                        }
                        ?>

How delete the particular image from database on clicking delete option in my view file?

Upvotes: 1

Views: 547

Answers (3)

Steve Bals
Steve Bals

Reputation: 1979

itried this wrkng fine , use tis in controller

public function delete_pickoftheday_image($id)
    {
        $data['del_pick_picture']=$this->pickoftheday_display_model->delete_pickoftheday_image($id);
    }

use tis in model

public function delete_pickoftheday_image($id)
    {
        $query=$this->db->query("DELETE FROM pick_of_the_day where id='$id' ");
        if($query)
        {
            return true;

        }
        else
        {

            return false;
        }
    }

use tis code in view

<a href="<?php echo base_url();?>pickoftheday_display/delete_pickoftheday_image/<?php echo $id;?>" class="label label-success">
                                         Delete
                                    </a>

Upvotes: 0

Maulik patel
Maulik patel

Reputation: 1601

your view should be,

<a href="example.com/pickoftheday_display/delete_image/<?php echo $your_img_id; ?>" class="label label-success">Delete</a>

where pickoftheday_display is your controller name and delete_image is function under this controller.

Please add below function in your controller,

function delete_image($image_id = "") {
   $result = $this->user_model->delete_image_from_db($image_id);
   if($result) {
     unlink($file_path); //Replace your full file path with $file_path.
 $this->session->set_flashdata('message', 'Docs deleted successfully.');
    }
   // rest of code
   // call to your view
}

Please add below function in your model,

function delete_image_from_db($image_id = "") {
   $sql = "DELETE FROM TABLE WHERE COLUMN_NAME = '".$image_id."'";
   $rs1 = $this->db->query($sql); 
   return $rs1;
}

Upvotes: 2

vignesh.D
vignesh.D

Reputation: 776

CODEIGNITER

You can delete image through javascript by passing img name ie.,$val And then in your model u can get its id by using image name Then delete that image

Upvotes: 0

Related Questions