Subselect and sum row

subselect() will create a new column with some value from other (or current) table. You can use any columns from current table as parameters. Subselect will be called for each row.


public function subselect_and_sum_row()
{
  $xcrud = get_xcrud();
  $xcrud->table('customers');
  $xcrud->columns('customerName,city,creditLimit,Paid,Profit'); // specify only some columns
  $xcrud->subselect('Paid','SELECT SUM(amount) FROM payments WHERE customerNumber = {customerNumber}'); // other table
  $xcrud->subselect('Profit','{Paid}-{creditLimit}'); // current table
  $xcrud->sum('creditLimit,Paid,Profit'); // sum row(), receives data from full table (ignores pagination)
  $xcrud->change_type('Profit','price','0',array('prefix'=>'$')); // number format
    
  $data['content'] = $xcrud->render();
  
  return view('demos', $data);
}

Customers

#CustomernameCityCreditlimitPaidProfit 
1Mini Gifts Distributors Ltd.San Rafael210500$0.00
2Zbyszek Zbyszek PiestrzeniewiczWarszawa1230$0.00
3Euro+ Shopping ChannelMadrid227600695729.4500000001$468,129.45
4Danish Wholesale ImportsKobenhavn83400107446.5$24,046.50
5Saveley & Henriot, Co.Lyon123900130305.9$6,405.90
6Dragon Souveniers, Ltd.Singapore103800156251.03$52,451.03
7Muscle Machine IncNYC138500177913.95$39,413.95
8Technics Stores Inc.Burlingame84600104545.22$19,945.22
9Handji Gifts& CoSingapore97900105420.56999999999$7,520.57
10Herkku GiftsBergen9680097562.47$762.47
Σ  7452941.127253583.860000001$12,583.74 
Search