I’m adding a search filter in my system but it won’t search.Please someone tell Me the problem with my coding.
Controller
public function facility_reservation_view(){
$name = $this->input->post('name');
$searchtype = $this->input->post('searchtype');
$datefrom = $this->input->post('datefrom');
$dateto = $this->input->post('dateto');
$status = $this->input->post("status");
$query = $this->model_facility->facility_reservation_table($name, $status, $searchtype, $datefrom, $dateto);
echo json_encode($query);
}
Model
public function facility_reservation_table($name, $status, $searchtype, $datefrom, $dateto){
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
global $access;
$columns = array(
// datatable column index => database column name for sorting
0 => 'fr_id',
1 => 'fr_status',
2 => 'CONCAT(fr_fname, fr_mname, fr_lname)',
3 => 'fr_room',
4 => 'fr_purpose',
5 => 'CONCAT(fr_date_from, fr_time_from)',
6 => 'CONCAT(fr_date_to, fr_time_to)'
);
$sql = "SELECT * FROM facility_reservation WHERE fr_enabled = 1";
$query = $this->db->query($sql);
$totalData = $query->num_rows();
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql = "SELECT * FROM facility_reservation WHERE fr_enabled = 1";
if ($searchtype == "divdate") {
$sql .= " AND fr_date_from BETWEEN ? AND ? ";
$data = array($datefrom, $dateto);
}
elseif ($searchtype == "divname"){
$sql .= " AND fr_fname LIKE ? " ;
$data = array($name);
}
elseif ($searchtype == "divstatus") {
$sql .= " AND fr_status LIKE ? ";
$data = array($status);
}
$query = $this->db->query($sql, $data);
$totalData = $query->num_rows();
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
$sql .=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; // adding length
$query = $this->db->query($sql);
$data = array();
$token = en_dec("en", $this->session->userdata('token_session'));
foreach( $query->result_array() as $row ) { // preparing an array for table tbody
$nestedData=array();
$nestedData[] = $row["fr_id"];
$nestedData[] = $row["fr_status"];
$nestedData[] = $row["fr_fname"]." ".$row["fr_mname"]." ".$row["fr_lname"];
$nestedData[] = $row["fr_room"];
$nestedData[] = $row["fr_purpose"];
$nestedData[] = $row["fr_date_from"]." ".$row["fr_time_from"];
$nestedData[] = $row["fr_date_to"]." ".$row["fr_time_to"];
$nestedData[] = '<button data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#viewFRModal" class="btn btn-info btnViewFR btnTable" name="update" data-value="'.$row['fr_id'].'" id="'.$row['fr_id'].'"><i class="fa fa-eye"></i> View</button> <button data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#updateFRModal" class="btn btn-success btnUpdateFR btnTable" name="update" data-value="'.$row['fr_id'].'" id="'.$row['fr_id'].'"><i class="fa fa-edit"></i>Update</button> <button data-toggle="modal" data-backdrop="static" data-keyboard="false" data-target="#deleteFRModal" class="btn btn-primary btnDeleteFR btnTable" name="delete" data-value="'.$row['fr_id'].'" id="'.$row['fr_id'].'"><i class="fa fa-trash"></i> Delete</button>';
$data[] = $nestedData;
}
$json_data = array(
"draw" => intval( $requestData['draw'] ),
"recordsTotal" => intval( $totalData ),
"recordsFiltered" => intval( $totalFiltered ),
"data" => $data
);
return $json_data;
}
view
<div class="row">
<div class="col-lg-2">
<div class="form-group">
<label class="form-control-label col-form-label-sm">Select Filter</label>
<select id="sosearchfilter" class="form-control sosearchfilter">
<option value="divdate">Search by Date</option>
<option value="divname">Search by First Name</option>
<option value="divstatus">Search by Status</option>
</select>
</div>
</div>
<div class="col-lg-4">
<div class="form-group row">
<div class="divdate" id="divdate">
<?php
$dateInTwoWeeks = strtotime('-1 weeks');
$dateInTwoWeeks = date("m/d/Y", $dateInTwoWeeks);
// echo $dateInTwoWeeks;
?>
<label class="form-control-label col-form-label-sm">Date</label>
<div class="input-daterange input-group" id="datepicker">
<input type="text" id="datefrom" class="input-sm form-control material_josh search-input-select1 searchDateTo" value="<?=$dateInTwoWeeks;?>" name="start" readonly/>
<span class="input-group-addon" style="background-color:#fff; border:none;">to</span>
<input type="text" id="dateto" value="<?=today_text();?>" class="input-sm form-control material_josh search-input-select2 searchDateFrom" name="end" readonly/>
</div>
</div>
<div class="divname" id="divname" style="display: none;">
<label class="form-control-label col-form-label-sm">Name</label>
<input type="text" class="input-sm form-control material_josh search-input-text search_name" id="search_name" placeholder="Name.." onkeypress="return (event)" />
</div>
<div class="divstatus" id="divstatus" style="display: none;">
<label class="form-control-label col-form-label-sm">Status</label>
<select id="search_status" class="form-control fr_status" >
<option value="<?php foreach ($get_stat->result() as $gdept) { ?>" ></option>
<option value="<?=$gdept->fr_status_name?>"><?=$gdept->fr_status_name?></option>
<?php } ?>
</select>
</div>
</div>
</div>
<div class="col-lg col-6" style="padding-left: 0">
<div class="pull-right">
<label class="form-control-label col-form-label-sm "></label>
<button type="submit" id="search_order" class="btn blue-grey search_order">Search</button>
<button type="button" data-target="#addFRModal" class="btn btn-primary addsupp"> Add New Room Reservation</button>
</div>
</div>
</div>
js
$(function(){
var base_url = $("body").data('base_url'); //base_url come from php functions base_url();
var token = $("#token").val();
var d = new Date();
var date = d.getFullYear() + "/" + (d.getMonth()+1) + "/" + d.getDate();
var searchDateTo = $("#searchDateTo").val();
function fillDatatable(name, status, searchtype, datefrom, dateto) {
var dataTable = $('#table-grid').DataTable({
destroy : true,
"bDeferRender": true,
"order": [[ 1, "desc" ]],
"serverSide": true,
"columnDefs": [
{ "targets": 1,
"createdCell": function (td, cellData, rowData) {
if (cellData == "Approved"){
$(td).css('background-color', '#DFF0D0')
}else if (cellData == "Disapprove"){
$(td).css('background-color', '#F5DBD9')
}else if (cellData == "Pending"){
$(td).css('background-color', '#FEECB5')
}else if (cellData == "Cancel"){
$(td).css('background-color', '#CFCFC4')
}else if (cellData == "New"){
$(td).css('background-color', '#DAF0F7')
}
}
}],
"ajax":{
url:base_url+"Main_facility/facility_reservation_view", // json datasource
type: "post", // method , by default get
data: { 'name':name, 'status':status, 'searchtype':searchtype, 'datefrom':datefrom, 'dateto':dateto },
beforeSend:function(data) {
$.LoadingOverlay("show");
},
complete: function() {
$.LoadingOverlay("hide");
},
error: function(){ // error handling
$(".table-grid-error").html("");
$("#table-grid").append('<tbody class="table-grid-error"><tr><th colspan="3">No data found in the server</th></tr></tbody>');
$("#table-grid_processing").css("display","none");
}
}
});
}
function toastMessage(heading, text, icon, color) {
$.toast({
heading: heading,
text: text,
icon: icon,
loader: false,
stack: false,
position: 'top-center',
allowToastClose: false,
bgColor: color,
textColor: 'white'
});
}
fillDatatable('divdate', searchDateTo, date, "");
// $(".searchBtn").on("click", function(){
// name = $(".searchName").val();
// status = $(".searchStat").val();
// fillDatatable(name, status);
// });
$("#sosearchfilter").change(function() {
var searchtype = $('#sosearchfilter').val(); // id ng dropdown
var currentdate = new Date();
var dateto = $('#searchDateTo').val();
var datefrom = $('#searchDateFrom').val();
if(searchtype == "divdate") {
$('.divdate').show('slow');
$('.divname').hide('slow');
$('.divstatus').hide('slow');
$(".search_name").val("");
$(".search_status").val("");
$(".searchDateTo").val($.datepicker.formatDate('mm/dd/yy', dateto));
$(".searchDateFrom").val($.datepicker.formatDate('mm/dd/yy', datefrom));
}
if(searchtype == "divname") {
$('.divdate').hide('slow');
$('.divname').show('slow');
$('.divstatus').hide('slow');
$(".search_name").val("");
$(".search_status").val("");
$(".searchDateTo").val("");
$(".searchDateFrom").val("");
}
if(searchtype == "divstatus"){
$('.divdate').hide('slow');
$('.divname').hide('slow');
$('.divstatus').show('slow');
$(".search_name").val("");
$(".search_status").val("");
$(".searchDateTo").val("");
$(".searchDateFrom").val("");
}
});
$("#search_order").click(function() {
searchtype = $('#sosearchfilter').val();
datefrom = formatDate($("#datefrom").val());
dateto = formatDate($("#dateto").val());
name = $('#search_name').val();
status = $('#search_status').val();
checker = 0;
if (searchtype == "divdate") {
if (datefrom == "" || dateto == "") {
toastMessage('Note', 'Please fill in the Date fields.', 'info', '#FFA500');
checker = 0;
}
else {
checker = 1;
}
}else if (searchtype == "divname") {
if (name == "") {
toastMessage('Note', 'Please fill in the Name.', 'info', '#FFA500');
checker = 0;
}
else {
checker = 1;
}
}else if (searchtype == "divstatus") {
if (status == "") {
toastMessage('Note', 'Please fill in the Status Field.', 'info', '#FFA500');
checker = 0;
}
else {
checker = 1;
}
}
if (checker == 1) {
fillDatatable(searchtype, datefrom, dateto, name, status);
}
});
Expecting to get the right code so I can fetch the data I wanted to search
Here’s my MVC coding and wanted to show you all so you can answer me clearly been stuck here for a while and would be happy if someone could help me.