I use this Library :
https://github.com/m1ome/phalcon-datatables
This is how i use it :
$resultset = $this->modelsManager
->createQuery("SELECT protId, protUsmsName, protUsmsId, protComsName, protAddress, protQty, protPrice, protTotalPrice,
protShpmName, protDateOrder , protPthdId
FROM ProductOrderTransaction WHERE AuditDelete = 'N' AND protMomsId = '4'
ORDER BY protId DESC")
->execute();
$dataTables = new DataTable();
$dataTables->fromResultSet($resultset)->sendResponse();
But, while i search "company" in the Data tables, it can't be found because it has value "Company". Case sensitive case.
Anyone had the modify Lib? thx
$check = (strpos(strtoupper( $item->$column ), strtoupper( $search ) ) !== false);
Related
I am tired to calculate date with start & end date in my Laravel & Vue js project?
I have also installed moment js.
Code as below,
{{item.start_date | MyDate}}
{{item.end_date | MyDate}}
{{Here will be calculate between above dates}}
Please help me.
You can create new method with this logic:
methods: {
parseDate (start, end) {
return moment(start, 'YYYY.MM.DD HH:mm').diff(moment(end, 'YYYY.MM.DD HH:mm'), "days")
}
}
And in your template you can simply do that: {{ parseDate(item.start_date, item.end_date) }}
I think is that what you need. Here you can check some examples of using diff in moment.
Good luck!
If you are getting the data from Laravel you can use Carbon to calculate the difference like this:
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$diff_in_hours = $end_date->diffInHours($start_date);
$diff_in_days = $end_date->diffInDays($start_date)
$diff_in_minutes = $end_date->diffInMinutes($start_date);
$diff_in_seconds = $end_date->diffInSeconds($start_date);
So if you are getting the data from a query you can map the query like this:
$schedules = DB::table('schedules')
->get()
->map(function($item){
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$item->diff = $end_date->diffInMinutes($start_date);
return $item;
});
The same for models
Schedule::where('status',1)->get()
->map(function($item){
$start_date = Carbon::parse($item->start_date);
$end_date = Carbon::parse($item->end_date);
$item->diff = $end_date->diffInMinutes($start_date);
return $item;
});
I have javascript function that populates datatable using Ajax. My javascript code looks like :
$('#results').dataTable({
// Ajax load data
"ajax": {
"url": "get_intl_tickets",
"type": "POST",
"data": {
"user_id": 451,
"csrfmiddlewaretoken" : csrftoken,
}
}
})
My server side script in django has a function that loads around 500 data rows. Now the problem is that I don't want to load whole data at a time. Instead I want to have first 10 data rows. Then with pagination, another 10 rows like that.
I read the page server side processing documentation of datatables. I tried with "serverSide": true option as well. I am not understanding server side script. There is given an example of PHP. It seems that they are not using any parameters like draw, recordsFiltered, recordsTotal there. There they have used php SSP class. And it is unknown what does it do. I am trying to implement it in django.
But I am not finding proper good documentation to implement. Any help will be appreciated.
Old question but one I also had a surprisingly difficult time finding an answer to, so in case anyone else ends up here... :P
I found this 2020 article very helpful, specifically part 6 showing the "complete code" that includes getting the correct variables, building the SQL query, and how to build/structure the data object that it responds with:
https://makitweb.com/datatables-ajax-pagination-with-search-and-sort-php/
Their example posted below:
<?php
## Database configuration
include 'config.php';
## Read value
$draw = $_POST['draw'];
$row = $_POST['start'];
$rowperpage = $_POST['length']; // Rows display per page
$columnIndex = $_POST['order'][0]['column']; // Column index
$columnName = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue = mysqli_real_escape_string($con,$_POST['search']['value']); // Search value
## Search
$searchQuery = " ";
if($searchValue != ''){
$searchQuery = " and (emp_name like '%".$searchValue."%' or
email like '%".$searchValue."%' or
city like'%".$searchValue."%' ) ";
}
## Total number of records without filtering
$sel = mysqli_query($con,"select count(*) as allcount from employee");
$records = mysqli_fetch_assoc($sel);
$totalRecords = $records['allcount'];
## Total number of record with filtering
$sel = mysqli_query($con,"select count(*) as allcount from employee WHERE 1 ".$searchQuery);
$records = mysqli_fetch_assoc($sel);
$totalRecordwithFilter = $records['allcount'];
## Fetch records
$empQuery = "select * from employee WHERE 1 ".$searchQuery." order by ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage;
$empRecords = mysqli_query($con, $empQuery);
$data = array();
while ($row = mysqli_fetch_assoc($empRecords)) {
$data[] = array(
"emp_name"=>$row['emp_name'],
"email"=>$row['email'],
"gender"=>$row['gender'],
"salary"=>$row['salary'],
"city"=>$row['city']
);
}
## Response
$response = array(
"draw" => intval($draw),
"iTotalRecords" => $totalRecords,
"iTotalDisplayRecords" => $totalRecordwithFilter,
"aaData" => $data
);
echo json_encode($response);
Nice exemple:
https://datatables.net/examples/server_side/defer_loading.html
But you need edit server side.
Response demo
{
draw:2,
recordsFiltered:57,
recordsTotal:57
}
How to prevent auto fix in the select statement using CodeIgniter 3?
Query that I want: SELECT REPLACE(item, ',', '') AS item (without space)
I tried : $this->db->select("replace(item, ',', '') AS item", FALSE);
Result : SELECT REPLACE(item, ', ', '') AS item (CI added a space in my query)
I read this documentation but it didn't work.
This my real query:
$this->db->select("a.unit_id");
$this->db->select("replace(c.topik, ',', '') AS topik", FALSE); //this my problem (added space which shouldnt)
$this->db->select("COUNT(*) AS jumlah");
$this->db->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
$this->db->join('opub_tag c','a.id = c.kliping_id');
if(!empty($data['keyword']))$this->db->where("(a.judul LIKE '%".$data['keyword']."%' OR a.text LIKE '%".$data['keyword']."')");
if(!empty($data['tgl1']))$this->db->where("a.tanggal >=",$data['tgl1']);
if(!empty($data['tgl2']))$this->db->where("a.tanggal <=",$data['tgl2']);
if(!empty($data['unit']))$this->db->where("a.unit_id ",$data['unit']);
if(!empty($data['media']))$this->db->where("a.media_id ",$data['media']);
if(!empty($data['jenis']))$this->db->where("a.jenis_id ",$data['jenis']);
if(!empty($data['tone']))$this->db->where("a.karakter ",$data['tone']);
if(!empty($data['topik']))
{
$qTopik = array();
$expTopik = explode(',', $data['topik']);
foreach($expTopik as $t)
{
if(!empty($t))
{
$qTopik[] = "c.topik LIKE '%$t%'";
}
}
if(!empty($qTopik))
{
$qTopiks = implode(",", $qTopik);
$this->db->where($qTopiks);
}
}
$this->db->where('a.unit_id !=', 0);
$this->db->where('c.topik !=', NULL);
$this->db->where('c.topik !=', '');
$this->db->group_by("a.unit_id");
$this->db->group_by("replace(c.topik, ',', '')", FALSE); //this produce correct query (without adding space)
$this->db->order_by('a.unit_id', 'ASC');
$this->db->order_by('jumlah', 'DESC');
$q= $this->db->get();
i found this after tried some experiment
$this->db->select(array('REPLACE(c.topik', "','", "'') AS topik"), false);
seems little hack maybe. but its work for me
hope will help others
change 4 lines of your query
$this->db->select("a.unit_id,replace(c.topik, ',', '') AS topik,COUNT(*) AS jumlah")->from('opub_kliping a');
$this->db->join('opub_media b','a.media_id = b.organisasi_id');
I try to delete entries in the database with the active records of Yii. But I think it works really weird.
I want to delete all records of my table where vehicle_id = the given id and plug_id NOT IN (given string)
I tried a lot of ways and nothing worked but this
$query = "delete from `vehicle_details` where `vehicle_id`= ".$vehicle->id." AND `plug_id` NOT IN (".implode(',', array_map(function($item) {
return $item->type;
}, $vehicleDetails->plug)).")";
$command = Yii::app()->db->createCommand($query);
$command->execute();
But why isn't this working???
VehicleDetail::model()->DeleteAllByAttributes(
array('vehicle_id' => $vehicle->id),
array('condition' => 'plug_id NOT IN (:ids)',
'params' => array('ids' => implode(',', array_map(function($item) {
return $item->type;
}, $vehicleDetails->plug)))));
Or this:
VehicleDetail::model()->deleteAll(' vehicle_id = :vehicleId AND plug_id NOT IN (:ids)', array('vehicleId' => $vehicle->id, 'ids' => implode(',', array_map(function($item) {
return $item->type;
}, $vehicleDetails->plug))));
But if I make and Find by attributes out of this query it works well and returns the correct data.
I hope you can explain it to me.
Your code does not work because of wrong arguments passed in the methods. Problems occur when YII tries to build CDbCriteria object using your array arguments. Fortunetly, you can build a CDbCriteria by yourself and pass it into methods directly. Guess in this particular case it will be easier to use a CDbCriteria object to solve the issue.
$dbc = new CDbcriteria();
$dbc->condition = 'vehicle_id = :vehicleId';
$dbc->params = array(':vehicleId'=>$vehicle->id);
$dbc->addNotInCondition(
'plug_id',
array_map(
function($item) {
return $item->type;
},
$vehicleDetails->plug)
);
VehicleDetail::model()->deleteAll($dbc);
That is all you need.
In sql server I create views where I can check for null values and replace them with a default if I want (i.e. ISNULL(PrimaryPhone, 'No Primary #') AS PrimaryPhone. I used a lot of views in my asp.net web forms application, but I am now learning MVC 4 and want to start out right.
I have a model, controller and view set up for my client table. I would like to be able to replace null/empty values with ("Not Entered") or something like that.
I believe this needs to go in the controller, please correct me if I'm wrong.
I saw an example that uses hasvalue, but it is not available through intellisense.
How would I replace empty/null values without using DefaultValue in my model?
Thanks
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = x.emailadd.DefaultIfEmpty("No Email"),....
You can use the ?? operator to set a default value when something is null:
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = x.emailadd ?? "No Email", ...
If you need more control, for example checking for both null and empty, you can also use the ?: operator (also known as the "conditional operator" or "ternary operator"):
var result = db.Clients.Select(x => new
{
CustomerID = x.CustomerID,
UserID = x.UserID,
FullName = x.FullName,
EmailAdd = string.IsNullOrEmpty(x.emailadd) ? "No Email" : x.emailadd, ...