breaking a row of datatable into multiple row with serverside method - php

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

Related

Convert Datatables array value from Integer to String

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";

MongoDb + PHP: Cannot specify more than one positional proj. per query

I have a "users" collection containing 2 arrays of objects (cards and prizes) each of these 2 can contain data from different companies marked with the "id_company" field.
$list = $users->find(
array(
'cards.id_company' => '... id company ...',
'cards.is_active' => true,
),
array(
'projection' => array(
'_id' => 1,
'full_name' => 1,
'cards.$.id_company' => '...id company...',
'prizes.$.id_company' => '...id company...',
),
'limit' => 5,
)
);
I'm trying to select only the data relating to one company, consequently excluding the others, but I get the following error: Cannot specify more than one positional proj. per query.
Any solutions?

Pull count of records in a table that have the same value with Codeigniter Active Record

I am working on a site where I have a table with "catches" (people have caught fish with a particular lure). The lure_used category is an integer.
I want to find the users "best lure". So i want to loop through all the user_catches rows and return an array that has each lure and how many times it was used.
So for example we might have
catch_1 | lure_used = 5,
catch_2 | lure_used = 3,
catch_3 | lure_used = 6,
catch_4 | lure_used = 3,
so from that i would like to know that 5 and 6 occur 1 time while 3 occurs twice. Therefore 3 is the most successful lure.
I know that I could probably use "MAX" in mysql queries, but i'm not really farmiliar with it and attempts have failed.
I have been able to create an array of lures used like:
$lures = array(5, 3, 6, 3);
So maybe i could just loop through that and output something like...
5 = 1, 3 = 2, 6 = 1.
I guess i'm grasping at straws haha, anyone have a better idea on how to get this done?
Here are all the fields in the "user_catches" table:
'id' => $row->id,
'user_id' => $row->user_id,
'species' => $row->species,
'season' => $row->season,
'lure_used' => $row->lure_used,
'depth' => $row->depth,
'exact_depth' => $row->exact_depth,
'presentation' => $row->presentation,
'catch_weight' => $row->catch_weight,
'length' => $row->length,
'fishing_from' => $row->fishing_from,
'moon_phase' => $row->moon_phase,
'water_temp' => $row->water_temp,
'water_quality' => $row->water_quality,
'notes' => $row->notes,
'rod' => $row->rod,
'reel' => $row->reel,
'line' => $row->line,
'rig' => $row->rig,
'catch_image' => $row->catch_image,
'weather' => $row->weather,
'temp' => $row->temp,
'humidity' => $row->humidity,
'wind' => $row->wind,
'pressure' => $row->pressure,
'visibility' => $row->visibility,
'location' => $row->location,
'weather_icon' => $row->weather_icon,
'catch_date' => $row->catch_date,
SELECT lure, count (*) as count
from user_catches
GROUP BY lures
ORDER BY count;

cakephp: filtering fields from child table in find('all')

My question extends one posted previously CakePHP: Limit Fields associated with a model. I used this solution effectively for limiting the returned fields for the parent table with this call
$data = $this->SOP10100->find('all',
array('fields' => $this->SOP10100->defaultFields));
However, this method returns the filtered parent and unfiltered child fields. I have 131 child fields of which I only need 7. I have the same defaultFields array construct in the child table. How do I modify this call ( or create a new one) that will return the filtered fields for both parent and child models in the same array?
Here is the structure for the array for the parent table:
public $defaultFields = array(
'SOP10100.SOPNUMBE',
'SOP10100.INVODATE',
'SOP10100.DOCDATE',
'SOP10100.DOCAMNT',
'SOP10100.SUBTOTAL');
Your help is appreciated.
SCORE! Wow, solved two big problems in one day. I finally figured it out with loads of help from many resources:
$this->InvoiceHeader->Behaviors->attach('Containable');
$data = $this->InvoiceHeader->find('all', array(
'fields' => $this->InvoiceHeader->defaultFields,
'contain' => array(
'InvoiceDetail' => array(
'fields' => $this->InvoiceDetail->defaultFields))
)
);
returns my array data just like I want it:
array(
(int) 0 => array(
'InvoiceHeader' => array(
'SOPNUMBE' => 'SVC0202088 ',
'INVODATE' => '2012-04-17 00:00:00',
'DOCDATE' => '2012-04-17 00:00:00',
'DOCAMNT' => '.00000',
'SUBTOTAL' => '.00000'
),
'InvoiceDetail' => array(
(int) 0 => array(
'ITEMNMBR' => 'SERVICE ',
'QUANTITY' => '1.00000',
'UOFM' => 'EA ',
'UNITPRCE' => '.00000',
'TAXAMNT' => '.00000',
'CONTSTARTDTE' => '2012-04-17 00:00:00',
'CONTENDDTE' => '2012-04-30 00:00:00',
'SOPNUMBE' => 'SVC0202088 '
),

cakePHP complex find query

how do I build a find() query in cakePHP using these conditions:
Find where
MyModel.x = 1 and MyModel.y = 2 OR
MyModel.x = 1 and MyModel.y value does not exist (or is equal to empty string)
Can somebody tell me how I can go about building such find query?
I'm gonna give you some pointers, but you need to try to do this as it's very basic and it's always good to practice.
A basic find in cake is in the form of
$this->ModelName->find('all');
This in its default form does a SELECT * from model_names (convention is to have singular ModelName for plural table name - model_names)
To add conditions:
$this->ModelName->find('all', array('conditions' => array('ModelName.x' => 1));
To add AND conditions
$this->ModelName->find('all', array('conditions' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
));
To add OR conditions
$this->ModelName->find('all', array('conditions' => array(
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
To combine both
$this->ModelName->find('all', array('conditions' => array(
'ModelName.y is not' => null,
'OR' => array(
'ModelName.x' => 1, 'ModelName.y' => 2
)
));
// where y is not null and (x = 1 or y = 2)
http://book.cakephp.org/1.3/view/1030/Complex-Find-Conditions
(btw I'm sure there will be users giving you the exact answers, so just take my answer for your reference :) )
$this->MyModel->find('all', array('conditions' => array(
'OR' => array(
array(
'MyModel.x' => 1,
'MyModel.y' => 1
),
array(
'MyModle.x' => 1,
'OR' => array(
array('MyModel.y' => NULL),
array('MyModel.y' => '')
)
)
)
)));

Categories