delete() function
deletes row(s) based on WHERE criteria
- returns array['rc'] with 'ok' - for success or 'bad' - for failure
Parameters - delete( tablename, where, limit )
- SQL table name
- WHERE condition - array, multiple values treated as AND parameters
e.g. WHERE col1 = value1 AND col2 = value2 - LIMIT - integer, defaults to 1
// Example 1 [controllers/controller] - PHP deleteTable function
function deleteTable( $tableid ) {
$status = $this->model->db->delete('table', $where=array('tableid'=>$tableid)); //
echo json_encode($status['rc']); // signal javascript
return;
}
Example 2: delete() MVC Process
- triggers a delete event which loads a modal (Javascript) with row id to be deleted
- invoked PHP deleteClass function deletes database record based on row id passed
// Example 2 [public/mvcspa/controller] DELETE PROCESS
// Javascript pops up modal for deletion
$(document).on('click', '#deleteclass',
function deleteclass(e) {
e.preventDefault();
var classid = $(this).attr('classid'); // html attribute value
var classcode = $(this).attr('classcode'); // html attribute value
var args = '/'+classid;
deletetitle = 'Are you sure that you want to delete Class ('+classcode+')';
Swal2.fire({
title: 'Delete Request',
html: deletetitle,
//icon: 'warning',
confirmButtonText: 'Delete',
showDenyButton: true,
denyButtonText: `Don't delete`,
showCancelButton: false,
cancelButtonText: 'Cancel'
}).then((result) => {
if (result.isConfirmed) {
// $.post('controller/deleteClass', { classid: classid }, ..a better way, pass in $_POST
$.post('controller/deleteClass/'+ args, // pass as function parameter
function(data) {
if (data == 'ok') {
$('#showclass').trigger('click'); // refresh
Toast.fire('Class Deleted!','','info');
} else if (data == 'bad') {
Toast.fire('Class Deleted failed!','','warning');
return false;
} else {
alert('delete (deleteForm) '+data);
}
});
}
else {
// click Cancel or outside box
}
});
});
// Example 2 [controllers/controller] - PHP deletes data
function deleteClass( $classid ) {
$status = $this->model->db->delete('class', $where=array('classid'=>$classid));
echo json_encode($status['rc']);
return;
}