Convert Datatables array value from Integer to String - php

My database has a column called status which store only integer 0,1,2... Currently im listing all my database data to a datatable but how can i display my status column as string instead of the integer 0,1,2 ? I'm displaying the int value straight from database to table (which are 0,1,2), but i want to process the (0,1,2) and display (0 =Pending, 1 = Running , 2 = Completed.)
My database column :
Column Status
Image: Datatable
Expected result : Expected
PHP:
// DB table to use
$table = 'projects';
// Table's primary key
$primaryKey = 'projectID';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database.
// The `dt` parameter represents the DataTables column identifier.
$columns = array(
array('db' => 'projectName', 'dt' => 0),
array('db' => 'projectDescription', 'dt' => 1),
array('db' => 'destinationName', 'dt' => 2),
array('db' => 'projectStatus', 'dt' => 3), //Here is the status column
array('db' => 'startDate', 'dt' => 4),
array('db' => 'endDate', 'dt' => 5)
);
// Include SQL query processing class
require 'ssp.class.php';
// Output data as json format
echo json_encode(
SSP::simple($_GET, $dbDetails, $table, $primaryKey, $columns)
);
My logic:
If (data) of projectStatus == 0 then display "Pending";
If (data) of projectStatus == 1 then display "Running";
If (data) of projectStatus == 2 then display "Completed";

Related

How to retrieve row data that has null value and one specific value on one column

I want to retrieve the row data from table course where program_vss has null and 2 value.
return $this->db->get_where('course', array('is_top_course' => 1, 'status' => 'active', 'program_vss'=> null , ));
any suggestions on how to retrieve data with two conditions
you can use an array and pass the array:-
Associative array method:-
$array = array('name' => $name, 'title' => $title, 'status' => $status);
$this->db->where($array);
// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

Bulk insert or update for the given where

I have a function that gets $homes and $member_id as parameters. $homes is an array, $member_id is an integer.
$homes = array( 'home13', 'home21', 'home34', 'home44' );
$member_id = 5;
How do I insert/update into the following DB?
in the format:
id home_name member_id
1 home13 5
2 home21 5
2 home34 5
2 home44 5
The function I used:
public function store($homes, $member_id) {
foreach( $homes as $home ) {
$data[] = [ 'member_id' => $member_id, 'home_name' => $home ];
}
Home::insert( $data );
}
which works fine while inserting. However, I need to update all records for a given $member_id if the member_id already exists.
For example, if the $member_id is 5 and $homes is now array( 'home54' ), all other home_name that are previously stored for the $member_id 5 should be deleted. so that the following would only remain.
id home_name member_id
1 home54 5
You can use Laravel's upsert() method
Example:
Home::upsert([
['home_name' => 'home54', 'member_id' => 5, 'id' => 1],
['home_name' => 'home13', 'member_id' => 5, 'id' => 2],
['home_name' => 'home21', 'member_id' => 5, 'id' => 3],
], ['member_id', 'id'], ['home_name']);
This method consists of three arguments.
Values to insert/update
Columns that uniquely identify the values
The values that need to be updated
Please note that you need to use a unique composite key to use upsert() method.
Example from Seeder class:
$table->unique(['member_id','id']);
Reference from Laravel docs: https://laravel.com/docs/9.x/eloquent#upserts
You can use updateOrCreate method to achieve this
public function store($homes, $member_id) {
foreach( $homes as $home ) {
Home::updateOrCreate( 'member_id' => $member_id, 'home_name' => $home );
}
}

breaking a row of datatable into multiple row with serverside method

I have a mysql table that store all info about customer supports like smsPanel,website,vmodule,.....
customerName|smspanel|SMSstartDay|SMSexpDay |websit|WebStartDay|webExpDay|
------------------------------------------------------------
john |yes |2019/01/15 |2020/01/15|yes |2019/02/12 |2020/02/12
I am using datatable serverside method to show informations. Its ok when I show every single row of database in single row of data table. but i want to show every support package in different row lik this
customerName | package | StartDay | EndDate
----------------------------------------------
jhon | SMS |2019/01/15 |2020/01/15
jhon | website |2019/02/12 |2020/02/12
this is my sql query for datatable serverSide
if($ssaction == "customerSupportlist"){
$table = <<<EOT
(
SELECT
*
FROM pdsupport
where customer_smsPack >0
) temp
EOT;
}
and i use this cod to format datatable but its useful for one row display
if($ssaction == "customerSupportlist"){
$primaryKey='id';
$columns = array(
array( 'db' => 'customer_fname','dt' => 0),
array( 'db' => 'customer_lname', 'dt' => 1 ),
array('db' => 'customer_ncode','dt' => 2),
array('db' => 'customer_mobile','dt' => 3),
array( 'db' => 'id',
'dt' => 4,
'formatter' =>function( $d, $row ) {
$customerPacks=getCustomerPacks($d);
return $customerPacks;
}
),
array( 'db' => 'id',
'dt' => 5,
'formatter' =>function( $d, $row ) {
$startDate=getCustomerPackStartDate($d);
return $startDate;
}
),
array( 'db' => 'id',
'dt' => 6,
'formatter' =>function( $d, $row ) {
$endDate=getCustomerPackEndDate($d);
return $endDate;
}
),
array( 'db' => 'id','dt' => 7 ),
);
}
I dont know how to modify this part to split the rows. OR by changing mysql query make a table that has my favorite structure

Run MySQL query in DataTables server-side processing script

I am using DataTables server-side processing to pull data into a DataTables table from a MySQL table.
This is the working MySQL query I would like to run and display in my DataTables table:
$sql = "SELECT Client,EstimateNumber,Status,TotalEstimatedTime,CostToDateRoleTotal,ROUND((CostToDateRoleTotal/TotalEstimatedTime)*100) as PercentComplete FROM Estimates WHERE " . ($studioName != null ? "Studio = '" . $studioName. "' AND" : '') . " Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 AND CostToDateRoleTotal > 0 ORDER BY PercentComplete DESC";
I have adjusted the DataTables server-side processing script to be:
<?php
// connection configuration
require_once 'config.php';
// db table to use
$table = 'Estimates';
// table's primary key
$primaryKey = 'EstimateNumber';
$percent_complete = "('CostToDateRoleTotal'/'TotalEstimatedTime')*100";
// array of database columns which should be read and sent back to DataTables.
// the 'db' parameter represents the column name in the database, while the 'dt'
// parameter represents the DataTables column identifier.
$columns = array(
array('db' => 'Client', 'dt' => 0),
array('db' => 'EstimateNumber', 'dt' => 1),
array('db' => 'Status', 'dt' => 2),
array('db' => 'TotalEstimatedTime', 'dt' => 3),
array('db' => 'CostToDateRoleTotal', 'dt' => 4),
array('db' => $percent_complete, 'dt' => 4),
); // end columns array
// sql server connection information
$sql_details = array(
'user' => $currentConfig['user'],
'pass' => $currentConfig['pass'],
'db' => $currentConfig['name'],
'host' => $currentConfig['host'],
);
// DataTables helper class
require 'ssp.class.php';
function utf8ize($d) {
if (is_array($d)) {
foreach ($d as $k => $v) {
$d[$k] = utf8ize($v);
}
} else if (is_string ($d)) {
return utf8_encode($d);
}
return $d;
}
$data = SSP::complex($_GET, $sql_details, $table, $primaryKey, $columns, null, "Status != 'Invoiced' AND Status != 'Cancelled' AND TotalEstimatedTime > 0 AND CostToDateRoleTotal > 0");
echo json_encode(utf8ize($data));
This line is throwing an error:
$percent_complete = "('CostToDateRoleTotal'/'TotalEstimatedTime')*100";
The error is: {"error":"An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column '('CostToDateRoleTotal'/'TotalEstimatedTime')*100' in 'field list'"}
In my original $sql query above, I ran this calculation and displayed the outcome as a new column, $percent_complete. I am trying to display those same results in my DataTables table. How can I alter my server-side processing script to perform this calculation and display it in a new column?
CAUSE
Class SSP defined in ssp.class.php cannot handle column aliases, expressions or JOINs.
SOLUTION
You need to alter ssp.class.php and remove all ` (backtick) characters that escape column and table names. You would be responsible for escaping column/table names yourself if the name is a reserved word.
Then replace
array('db' => $percent_complete, 'dt' => 4)
with
array('db' => 'ROUND((CostToDateRoleTotal/TotalEstimatedTime)*100)', 'dt' => 4)
I don't know how the SSP class formats the query but you might want to try to add "AS percent_complete".
$percent_complete = "(CostToDateRoleTotal/TotalEstimatedTime)*100 AS percent_complete";
When you SELECT a column it needs a name.

fetch values in listbox from two table using joins

User Table -
column name - location_id
values = 4,5
Location table
column name - id , location_name
values = 4 nagpur
5, Akola
i want to display location names in listbox by joining location_id of user table,
Suppose in location_id column of user table 4,5 values are present then in my select box this only displays nagpur, Akola from location table.
i cannot understand how to explode values of location_id table before select and display only those values which are available in location_id column from location table.
public function getUserLocations() {
$this->loadModel('User');
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id','Location.location_name'),
'joins' => array(
array(
'table' => 'locations',
'alias' => 'Location',
'type' => 'LEFT',
'conditions' => array('FIND_IN_SET(User.location_id,Location.id)')
)
),
));
$this->set('getlocations', $getlocations);
}
below is my code ..
Explode the variable like this :
$getlocations = $this->User->find('list', array(
'fields' => array('Location.id'));
$location_ids[] = explode (',', $getlocations);
Now you will get the data in array format. Modify it according to your needs

Categories