how search between date using post in codeignitier - ajax

Dear Expert need Help first see my view code in codeigniter :
<div class="form-group">
<label for="tglawal" class="col-sm-2 control-label">Periode</label>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglawal" id="tglawal">
</div>
</div>
<div class="col-sm-3">
<div class="input-group date">
<div class="input-group-addon">
<i class="fa fa-calendar"></i>
</div>
<input type="date" class="form-control" name="tglakhir" id="tglawal1">
</div>
</div>
</div>
and this my model code :
private function _get_datatables_query()
{
//add custom filter here
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal', $this->input->post('tglawal'));
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir', $this->input->post('tglakhir'));
}
}
public function get_datatables()
{
$this->_get_datatables_query();
if($_POST['length'] != -1)
$this->db->limit($_POST['length'], $_POST['start']);
$query = $this->db->get();
return $query->result();
}
and my controller if i get the important code is:
public function index()
{
$this->load->helper('url');
$this->load->helper('form');
$this->load->view('infokunjungan_view', $data);
}
else redirect(base_url());
}
public function ajax_list()
{
$list = $this->Infokunjungan->get_datatables();
$data = array();
$no = $_POST['start'];
foreach ($list as $infokunjungan) {
$no++;
$row = array();
$row[] = "<td style='vertical-align:middle'><center>{$no}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglawal}<center></td>";
$row[] = "<td style='font-size:9px; vertical-align:left;'>{$infokunjungan->tglakhir}<center></td>";
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->Infokunjungan->count_all(),
"recordsFiltered" => $this->Infokunjungan->count_filtered(),
"data" => $data,
);
//output to json format
echo json_encode($output);
}
the problem is if searching between two date tglawal and tglakhir
im using between 2016-12-04 and 2016-12-04 output display will empty
but if using between 2016-12-04 and 2016-12-06 output success where is my problem or maybe im using where or i have to use like?

You need to use the >= and <= operator.
In your model try the below.
if($this->input->post('tglawal'))
{
$this->db->where('b.tglawal >=', $this->input->post('tglawal')); //assuming this is your begining (from) date
}
if($this->input->post('tglakhir'))
{
$this->db->where('b.tglakhir <=', $this->input->post('tglakhir')); //assuming this is your end(to) date
}
The above will search for the between dates including the dates selected.
Use the operator depending on the beginning and ending variable.

Related

Custom filter datatables serverside not working in codeigniter 3

Custom filter datatables serverside not working in codeigniter 3 but no error, with join table database ... anyone can help me ? thx
MODEL
Rekap_m.php
var $column_order = array(null, 'bidang', 'skpd', 'kode', 'masalah', 'nomor', 'rincian_masalah', 'tahun_cipta', 'rak', 'dos', 'retensi', 'ket');
var $column_search = array('bidang', 'kode', 'nomor', 'skpd');
var $order = array('id_arsip' => 'asc');
private function _get_datatables_query()
{
if ($this->input->post('bidang')) {
$this->db->where('id_bidang', $this->input->post('bidang'));
}
if ($this->input->post('skpd')) {
$this->db->like('skpd', $this->input->post('skpd'));
}
if ($this->input->post('kode')) {
$this->db->like('kode', $this->input->post('kode'));
}
if ($this->input->post('masalah')) {
$this->db->like('masalah', $this->input->post('masalah'));
}
$this->db->select('*');
$this->db->from('arsip');
$this->db->join('bidang', 'bidang.id_bidang = arsip.id_bidang');
$i = 0;
foreach ($this->column_search as $item) {
if (#$_POST['search']['value']) {
if ($i === 0) {
$this->db->group_start();
$this->db->like($item, $_POST['search']['value']);
} else {
$this->db->or_like($item, $_POST['search']['value']);
}
if (count($this->column_search) - 1 == $i)
$this->db->group_end();
}
$i++;
}
if (isset($_POST['order'])) {
$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
} else if (isset($this->order)) {
$order = $this->order;
$this->db->order_by(key($order), $order[key($order)]);
}
}
function get_datatables()
{
$this->_get_datatables_query();
if (#$_POST['length'] != -1)
$this->db->limit(#$_POST['length'], #$_POST['start']);
$query = $this->db->get();
return $query->result();
}
function count_filtered()
{
$this->_get_datatables_query();
$query = $this->db->get();
return $query->num_rows();
}
function count_all()
{
$this->db->from('arsip');
return $this->db->count_all_results();
}
public function get_list_bidangs()
{
$this->db->select('*');
$this->db->from('arsip');
$this->db->join('bidang', 'bidang.id_bidang = arsip.id_bidang');
$this->db->order_by('id_arsip', 'asc');
$query = $this->db->get();
$result = $query->result();
$bidangs = array();
foreach ($result as $row) {
$bidangs[] = $row->bidang;
}
return $bidangs;
}
CONTROLLER
Rekap.php
function __construct()
{
parent::__construct();
check_not_login();
$this->load->model('rekap_m');
}
public function index()
{
$bidangs = $this->rekap_m->get_list_bidangs();
$opt = array('' => 'Semua Bidang');
foreach ($bidangs as $id_bidang) {
$opt[$id_bidang] = $id_bidang;
}
$data['form_bidang'] = form_dropdown('', $opt, '', 'id="bidang" class="form-control"');
$this->template->load('template', 'rekap/rekap_data', $data);
}
function ajax_list()
{
$list = $this->rekap_m->get_datatables();
$data = array();
$no = #$_POST['start'];
foreach ($list as $arsip) {
$no++;
$row = array();
$row[] = $no . ".";
$row[] = $arsip->bidang;
$row[] = $arsip->skpd;
$row[] = $arsip->kode;
$row[] = $arsip->masalah;
$row[] = $arsip->nomor;
$row[] = $arsip->rincian_masalah;
$row[] = $arsip->tahun_cipta;
$row[] = $arsip->rak;
$row[] = $arsip->dos;
$row[] = $arsip->retensi;
$row[] = $arsip->ket;
// add html for action
$data[] = $row;
}
$output = array(
"draw" => #$_POST['draw'],
"recordsTotal" => $this->rekap_m->count_all(),
"recordsFiltered" => $this->rekap_m->count_filtered(),
"data" => $data,
);
// output to json format
echo json_encode($output);
}
public function rekap()
{
$this->load->model('bidang_m');
$data['bidang'] = $this->bidang_m->get()->result();
$this->template->load('template', 'rekap/rekap_data', $data);
}
public function del($id)
{
$this->arsip_m->del($id);
if ($this->db->affected_rows() > 0) {
echo "<script>alert('Data Berhasil dihapus'); </script>";
}
echo "<script>window.location='" . site_url('rekap') . "'; </script>";
}
VIEW
rekap_data.php
<div class="row">
<!-- DataTable with Hover -->
<div class="col-lg-12">
<div class="card mb-4">
<div class="card-header py-3 d-flex flex-row align-items-center justify-content-between">
<div class="col-md-4 offset-md-4">
<form id="form-filter">
<div class="form-horizontal">
<div class="form-group">
<div class="col-sm-9">
<div class="form-group">
<label for="bidang" class="col-sm-4 control-label">Bidang</label>
<?php echo $form_bidang; ?>
</div>
<div class="form-group">
<label for="skpd" class="col-sm-4 control-label">SKPD</label>
<input type="text" class="form-control" id="skpd">
</div>
<div class="form-group">
<label for="kode" class="col-sm-4 control-label">Kode</label>
<input type="text" class="form-control" id="kode">
</div>
<div class="form-group">
<label for="masalah" class="col-sm-4 control-label">Masalah</label>
<textarea class="form-control" id="masalah"></textarea>
</div>
<div class="form-group">
<button type="button" id="btn-filter" class="btn btn-primary">Filter</button>
<button type="button" id="btn-reset" class="btn btn-default">Reset</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
<div class="table-responsive p-3">
<table class="table align-items-center table-flush table-hover" id="dataTableHover">
<thead class="thead-light">
<tr>
<th>#</th>
<th>Bidang</th>
<th>SKPD</th>
<th>Kode</th>
<th>Masalah</th>
<th>Nomor</th>
<th>Rincian</th>
<th>Tahun</th>
<th>Rak</th>
<th>Dos</th>
<th>Retensi</th>
<th>Keterangan</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script type="text/javascript">
var table;
$(document).ready(function() {
table = $('#dataTableHover').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "<?php echo site_url('rekap/ajax_list') ?>",
"type": "POST",
"data": function(data) {
data.id_bidang = $('#bidang').val();
data.skpd = $('#skpd').val();
data.kode = $('#kode').val();
data.masalah = $('#masalah').val();
}
},
"columnDefs": [{
"targets": [0],
"orderable": false,
}, ],
});
$('#btn-filter').click(function() {
table.ajax.reload();
});
$('#btn-reset').click(function() {
$('#form-filter')[0].reset();
table.ajax.reload();
});
});
</script>
the code above is the code that I use, there is no error but not working ... is there something wrong with my code, I ask for advice on what corrections if there are errors

How to use condition in laravel query

Hello I am new to laravel. I am getting an error saying 'Property [pickupdate] does not exist on this collection instance.', after fowarding to 'overdue_pickup' view.
My Controller code
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
if($overdue_pickup->pickupdate < $curr_date){
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}else{
return "No Overdue Pickup";
}
}
My overdue_pickup view
<?php $i = 1; ?>
#foreach($overdue_pickup as $overdue_pickup)
<div class="col-md-offset-1 col-md-10">
<div class="box box-success">
<div class="box-header with-border">
<h3 class="box-title" style="padding:5px">Order No: <b><?php echo $i;?> {{$overdue_pickup->status}}</b></h3>
</div>
<div class="box-body">
<div class="col-md-offset-1 col-md-10">
<div style="overflow-x:auto;">
<p><i class="fa fa-dashboard"></i> <b>Pickup Time:</b> {{$overdue_pickup->pickuptime}}</p>
<p><i class="fa fa-dashboard"></i> <b>Delivery Time:</b> {{$overdue_pickup->deliverytime}}</p>
</div>
</div>
</div>
</div>
</div>
<?php $i++; ?>
#endforeach
Considering that your record for $id=2 is one,
Change
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->get();
to first()
$assign_pickup = DB::table('archive_pickup')
->where('ridder_id',$id)
->first();
Then use
if($assign_pickup->pickdate > $curr_date){
return view('Ridder.assign',['assigned_pickup' => $assigned_pickup]);
}else{
return "No Overdue Pickup";
}
If not one then use foreach & no need to change get() statement,
foreach($assign_pickup as $assign_pick){
if($assign_pick->pickdate > $curr_date){
// ... your logic
}else{
// ..your logic
}
}
EDIT:
public function overdue_pickup(){
$id = "2";
$curr_date = date('m/d/yy');
$overdue_pickup = DB::table('archive_pickup')
->where('ridder_id_',$id)
->get();
return view('overdue_pickup',['overdue_pickup' =>
$overdue_pickup]);
}
In your view, you can just foreach loop through it,
#foreach($overdue_pickup as $overdue_pick)
#if($overdue_pick->pickdate > $curr_date)
{{-- show your view --}}
#else
<p class="text-warning">No Overdue Pickup</p>
#endif
#endforeach

Laravel Maatwebsite excel

I need your help. I don't know how to import the excel file. I mean I don't understand where to put this users.xlsx and how to get its directory
public function import()
{
Excel::import(new UsersImport, 'users.xlsx');
return redirect('/')->with('success', 'All good!');
}
its simple on mattwebsite you need a controller like below :
public function importExcel(Request $request)
{
if ($request->hasFile('import_file')) {
Excel::load($request->file('import_file')->getRealPath(), function ($reader) {
foreach ($reader->toArray() as $key => $row) {
// note that these fields are completely different for you as your database fields and excel fields so replace them with your own database fields
$data['title'] = $row['title'];
$data['description'] = $row['description'];
$data['fax'] = $row['fax'];
$data['adrress1'] = $row['adrress1'];
$data['telephone1'] = $row['telephone1'];
$data['client_type'] = $row['client_type'];
if (!empty($data)) {
DB::table('clients')->insert($data);
}
}
});
}
Session::put('success on import');
return back();
}
and a view like this :
<form
action="{{ URL::to('admin/client/importExcel') }}" class="form-horizontal" method="post"
enctype="multipart/form-data">
{{ csrf_field() }}
<div class="form-group">
<label class="control-label col-lg-2">excel import</label>
<div class="col-lg-10">
<div class="uploader"><input type="file" name="import_file" class="file-styled"><span class="action btn btn-default legitRipple" style="user-select: none;">choose file</span></div>
</div>
</div>
<button class="btn btn-primary">submit</button>
</form>
and finally a route like below :
Route::post('admin/client/importExcel', 'ClientController#importExcel');

search with ajax laravel

I need my project to have a search form, I tried with this code, but when I execute console it says:
POST http://localhost:8000/buscar 500 (Internal Server Error)
then here is my view
<div id="qnimate" class="off">
<div id="search" class="open">
<button data-widget="remove" id="removeClass" class="closeSearch" type="button">×</button>
<input type="text" placeholder="Buscar Noticias - Articulos - Reviews" id="buscar" onkeydown="keydownFunction()" onkeyup="keyupFunction()">
<button class="btn btn-lg btn-site" type="submit"><span class="glyphicon glyphicon-search"></span> Buscar</button>
<div id="resultadoBusqueda" class="col-md-12"></div>
</div>
</div>
here is my controller
public function buscar()
{
$keywords = Input::get('keywords');
$reviews = Reviews::where('estado','1')->take();
$buscarReviews = new \Illuminate\Database\Eloquent\Collection();
foreach ($reviews as $review) {
if(Str::contains(Str::lover($review->nombre), Str::lover($keywords)))
$buscarReviews->add($review);
}
return view::make('busqueda')->with('buscarReviews', $buscarReviews);
}
here is the view which shows the information
#foreach($buscarReviews as $review)
<div id="reviews" class="col-md-12">
<div class="col-md-4">
<img src="Imagenes/{{$review->logo}}" width="50">
</div>
<div class="col-md-8">
<h2>{{$review->nombre}}</h2>
<div>{!! str_limit($review->descripcion, $limit = 150, $end = '...') !!}</div>
</div>
</div>
#endforeach
here is my js
var timer;
function keydownFunction(){
timer = setTimeout(function(){
var keywords = $('#buscar').val();
if(keywords.length > 0){
$.post('/buscar', {keywords: keywords}, function(markup){
$('#resultadoBusqueda').html(markup);
});
}
}, 500);
}
function keyupFunction(){
clearTimeout(timer);
}
I don't know what is the reason that it doesn't print the information required.
you are sending post request from ajax but in your controller you are trying to get that value using get, try this code
public function buscar(Request $request)
{
$keywords = $request->keywords;
$reviews = Reviews::where('estado','1')->take();
$buscarReviews = new \Illuminate\Database\Eloquent\Collection();
foreach ($reviews as $review) {
if(Str::contains(Str::lover($review->nombre), Str::lover($keywords)))
$buscarReviews->add($review);
}
return view::make('busqueda')->with('buscarReviews', $buscarReviews);
}
also remember to change in route like this.
Route::post('buscar', 'yourController#buscar');

Form dropdown is giving me index and not the text

Can you help out a new learner of Codeigniter? for some reason my form dropdown is giving me the index as input->post. I just need the text selected.
Model
function get_items(){
$this->db->select('item_name');
$this->db->from('commissary_items');
$query=$this->db->get();
$result=$query->result();
$item_names=array('-SELECT-');
for($i=0;$i<count($result);$i++){
array_push($item_names,$result[$i]->item_name);
}
return $item_names;
}
View
<div class="form-group">
<div class="row colbox">
<div class="col-lg-4 col-sm-4">
<label for="item_name" class="control-label">Item</label>
</div>
<div class="col-lg-8 col-sm-8">
<?php
$attributes = 'class = "form-control" id = "item_name"';
echo form_dropdown('item_name',$item_name,set_value('item_name'),$attributes);?>
<span class="text-danger"><?php echo form_error('item_name'); ?></span>
</div>
</div>
</div>
Controller
public function new_inventory(){
$data['item_name']=$this->commissary_model->get_items();
$this->form_validation->set_rules('date_added','Date Added','required');
$this->form_validation->set_rules('item_name','Item Name','callback_combo_check');
$this->form_validation->set_rules('quantity','Quantity','required');
$this->form_validation->set_rules('amount','Amount','required|numeric');
$this->form_validation->set_rules('username','User Name');
if($this->form_validation->run()==FALSE){
// $data="";
$this->load->view('new_inventory_view',$data);
}else{
$data=array(
'date_added'=>#date('Y-m-d',#strtotime($this->input->post('date_added'))),
'item_name'=>$this->input->post('item_name'),
'quantity'=>$this->input->post('quantity'),
'amount'=>$this->input->post('amount'),
'username'=>$this->session->userdata('username')
);
$this->db->insert('add_inventory',$data);
$this->session->set_flashdata('msg','<div class="alert alert-success text-center">Item added to inventory.</div>');
redirect('commissary/added_to_inventory');
}
}
Instead of the "text value" inside the form dropdown, I get the index 1 or 2 or 3 or 4 or 5 etc.... Thank you.
That is how <select> options work - the value of the select option is returned.
If you want the text you will have to setup $item_names differently and have the index be the same as the text. Easily accomplished with a little restructuring in the model.
function get_items()
{
$this->db->select('item_name');
$this->db->from('commissary_items');
$query = $this->db->get();
$result = $query->result();
$item_names = array();
foreach($result as $item)
{
$item_names[$item->item_name] = $item->item_name;
}
return $item_names;
}

Resources