update() function
updates row based on WHERE criteria
- returns array['rc'] with 'ok' - for success or 'bad' - for failure
- table column lastupdate its timestamped
Parameters - update( tablename, buffer, where, limit )
- SQL table name
- BUFFER - array, containing multiple values to be updated
- 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 updateTable function
function updateTable( $tableid ) {
$UPDATE = $_POST;
unset($UPDATE['tableid']); // no need to update primary key
$status = $this->model->db->update('class', $UPDATE, $where=array('tableid'=>$tableid) ); //
echo json_encode($status['rc']); // signal javascript
return;
}
Example 2: update() MVC Process
- triggers an edit event which loads a modal (Javascript)
- invoked PHP editClass function then renders editClass form page/view
- upon submitting form, it calls function to update editClass form data.
On callback from php, it notifies success or failure (Javascript) - invoked PHP updateClass function updates editClass form data to database
// Example 2 [public/mvcspa/controller] UPDATE PROCESS
// Javascript pops up modal, PHP will fetch edit class form view
$(document).on('click', '#editclass',
function editclass(e) {
e.preventDefault();
var classid = $(this).attr('classid'); //
var args = '/'+classid;
$('#controllerModal').modal({});
$("#controllerContent").load( 'controller/editClass'+args );
});
// Example 2 [controllers/controller] - PHP presents edit class form view
function editClass( $classid ) {
$orgid = (int) Session::get('orgid');
// need class data to show on view
$this->view->rs_class = $this->model->db->getOne('class', $where=array('classid'=>(int) $classid) ); //
$ar = $this->model->db->getOne('org', $where=array('orgid'=>$orgid), $sel='orgname' );
$this->view->orgname = $ar['orgname'];
$this->view->render('controller/editclass', false); // shows edit class form view
}
// Example 2 [public/mvcspa/controller] - edit class form is submitted
$(document).on('submit', '#editClassForm',
function(e) {
e.preventDefault();
// only these fields are being passed to updateClass()
var classid = $('#classid').val(); // form hidden field
var classcode = $('#classcode').val(); // form input field
var desc = $('#desc').val(); // form input field
var args = '/'+classid;
$.ajax({
url: "controller/updateClass"+ args, // pass as function parameter
type: 'post',
dataType: 'json',
data: { classcode: classcode, desc: desc }, // pass arguments via $_POST
async: true,
success:
function( data ) {
// callback: awaiting signal from (updateClass)
if ( data.rc == 'ok' ) {
$('#showclass').trigger('click'); // refresh
Toast.fire('Class Updated!','','info');
}
else if ( data.rc == 'bad' ) {
Toast.fire('Class Update failed!','','warning');
return false;
}
$('#controllerModal').modal('hide');
}
});
});
// Example 2 [controllers/controller] - PHP updates data from edit class form
function updateClass( $classid ) {
// $classid data could also be passed via $_POST variable ...better way
$UPDATE = $_POST; // [classcode, desc] values passed
$status = $this->model->db->update('class', $UPDATE, $where=array('classid'=>$classid) );
echo json_encode($status['rc']); // callback: signals javascript return code status
return;
}