0

I am trying to get confirmation message before deleting the data, and also I would like to maintain my data in database while deleting them in frontend . here is my (project) index.php view

 <div class="main-sec-contant"> <div class="ProjectsDetails"> <h2 class="heading">Projects Details</h2> <div class="row"> <div class="col-md-12"> <table id="table_id" class="display"> <thead> <tr> <th>Project Name</th> <th>Client Name</th> <th>Company</th> <th>Project Manager</th> <th>Support Staff</th> <th>Status</th> <th>Actions</th> </tr> </thead> <tbody> <?php foreach($project as $n) { ?> <tr> <td><?php echo $n->project_name;?></td> <td><?php echo $n->client_name;?></td> <td><?php echo $n->company;?></td> <td><a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>" data-toggle="tooltip" data-placement="bottom" title="<?php echo $n-> project_manager;?>" data-original-title="Click to deactivate the user"></a>&nbsp&nbsp </td> <td><a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>" data-toggle="tooltip" data-placement="bottom" title="<?php echo $n-> support_staff;?>" data-original-title="Click to deactivate the user"></a>&nbsp&nbsp <a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>"></a>&nbsp&nbsp <a class="pro-circle"><img class="img-sm" src="<?php echo site_url('assets/image/man.png');?>"></a></td> <td><span class="icoact"></span> Active</td> <td><a class="edit" href="<?php echo site_url('admin/project/edit/'.$n->id);?>"><i class="fa fa-pencil-square-o" ></i></a>&nbsp&nbsp<a class="delete" href="<?php echo site_url('admin/project/delete/'.$n->id);?>"><i class="fa fa-trash-o"></i></a></td> </tr> <?php } ?> </tbody> </table> </div> </div> </div> 

Here is my Project.php controller

public function index () { $data['company_name'] = $this->project_model->getAllCompanyName(); $data['project'] = $this->project_model->getProjectDetails(); $this->load->view('admin/project/index',$data); } function delete($id) { $this->project_model->delete($id); $this->session->set_flashdata ('success','Project Deleted Sucessfully'); redirect('admin/project/index'); } 

And here is my Project_model.php , delete function

 function getProjectDetails() { //table (projects) $delete_flag=0; $data['project'] = $this->db->get_where('projects',array('delete_flag!='=>$delete_flag))->result_array(); 

}

function getById($id) { return $this->db->get_where('projects',array('id'=>$id))->row(); } function delete($id) { $delete_flag=0; $this->db->where('id',$id)->update('projects',array('delete_flag'=>$delete_flag)); } 

Right now I am able to delete the data successfully from both frontend and database, I would appreciate if you can help me how I can get the confirmation message, and also maintain my data in database while deleting them from frontend ?

Here is the database table

enter image description here

2
  • you want to show confirm message before deleteing the record? Commented Aug 19, 2020 at 8:48
  • please check my answer. Commented Aug 19, 2020 at 10:20

2 Answers 2

0

Delete in Frontend only:

Add a boolean Column "delete_flag" to your project database table and update it to TRUE when delete is clicked in frontend.

Then change your load Method in Project model to only get Projects where delete_flag = 0

Delete Confirmation:

Search for Modal Dialog. You can easily set up a message and confirm button link with a modal Dialog. Maybe you are even using a Template which already provides it.

See an example here: https://www.w3schools.com/howto/howto_css_modals.asp

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

4 Comments

I just updated my code according your recommendation for maintaining data in database,its not working , can you please check
I actually couldent get what you mean by updating to TRUE, maybe thats the problem , would appreciate if you can provide me the code
Just set the value to 1 like this $this->db->where('id',$id)->update('projects',array('delete_flag'=>1));
not working , i am getting errors:Undefined variable: id in project_model.php , and Invalid argument supplied for foreach() in view
0

For Data Delete from Frontend only but not from database:-

You should add a Column name delete_flag int datatype to your project database table and update it to 0, while deleting the record.

Data Delete Confirmation Alert Message:-

<td><a class="delete" href="<?php echo site_url('admin/project/delete/'.$n->id);?>" onclick="return confirm('Are you sure want to Delete this Record?')"><i class="fa fa-trash-o"></i></a></td> 

controller Code Project.php :-

function delete($id) { $this->project_model->delete($id); $this->session->set_flashdata ('success','Project Deleted Sucessfully'); redirect('admin/project/index'); } 

Model Code Project_model.php :-

function getProjectDetails() { $delete_flag=0; $data['project'] = $this->db->get_where('projects',array('delete_flag!='=>$delete_flag))->result_array(); } function delete($id) { $delete_flag=0; $this->db->where('id',$id)->update('projects',array('delete_flag'=>$delete_flag)); } 

In Your View:-

Show Only records which have delete_flag!=0 by this query in your getProjectDetails()

10 Comments

i followed your answer, i am getting confirmation message , but delete function is not working anymore
Severity: Warning Message: Invalid argument supplied for foreach() Filename: project/index.php Line Number: 51 Backtrace: File: /opt/lampp/htdocs/ASGB/application/views/admin/project/index.php Line: 51 Function: _error_handler File: /opt/lampp/htdocs/ASGB/application/controllers/admin/Project.php Line: 16 Function: view File: /opt/lampp/htdocs/ASGB/index.php Line: 315 Function: require_once
I tried to add new project and see what is the delete_flag value in database before deleting and it is 0
Now change your getProjectDetails() in Model with updated answer.
Still getting same errors, please check updated code of mine
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.