i have export functionality in yii.
here code:
in controller have query this:
$daterangerecords = yii::app()->db->createcommand("select o.orderuniqueid, o.ordercustomerfirstname, o.ordercustomerlastname, o.ordercustomeremail, o.ordercustomerphone, o.orderdeliveryaddress, o.orderdeliveryfee, o.deliverydate,o.ordertax, o.orderproducttotalamount, o.ordertotalquantity, o.orderpaymentstatus, o.orderdateadded, o.orderstatus tbl_order o, tbl_order_item item o.pkorderid = item.fkorderid , o.orderdateadded >= '".$d_format_one."' , o.orderdateadded <= '".$d_format_two."' "); $arrordersrecord = $daterangerecords->queryall(); if (isset($_post)) { if (isset($_post['csv_export'])) { csvexport::export($arrordersrecord, array( "orderid" => "orderid", "customerfirstname" => "customerfirstname", "customerlastname" => "customerlastname", "customeremail" => "customeremail", "customerphone" => "customerphone", "deliveryaddress" => "deliveryaddress", "deliveryfee" => "deliveryfee", "deliverydate" => "deliverydate", "tax" => "tax", "totalamount" => "totalamount", "totalquantity" => "totalquantity", "paymentstatus" => "paymentstatus", "orderplacedon" => "orderplacedon", "status" => "status" ), true, 'orders.csv'); } else if (isset($_post['excel_export'])) { $headerarr = array( "orderid", "customerfirstname", "customerlastname", "customeremail", "customerphone", "deliveryaddress", "deliveryfee", "deliverydate", "tax", "totalamount", "totalquantity", "paymentstatus", "orderplacedon", "status" => "status", ); $rowarr = array(); $rowarr1 = array(); $i = 0; foreach ($arrordersrecord $orderrecord) { $j = 0; $rowarr1[$j++] = $orderrecord->orderid; $rowarr1[$j++] = $orderrecord->customerfirstname; $rowarr1[$j++] = $orderrecord->customerlastname; $rowarr1[$j++] = $orderrecord->customeremail; $rowarr1[$j++] = $orderrecord->customerphone; $rowarr1[$j++] = $orderrecord->deliveryaddress; $rowarr1[$j++] = $orderrecord->deliveryfee; $rowarr1[$j++] = $orderrecord->deliverydate; $rowarr1[$j++] = $orderrecord->tax; $rowarr1[$j++] = $orderrecord->totalamount; $rowarr1[$j++] = $orderrecord->totalquantity; $rowarr1[$j++] = $orderrecord->paymentstatus; $rowarr1[$j++] = $orderrecord->orderplacedon; $rowarr1[$j++] = $orderrecord->status; $rowarr[$i++] = $rowarr1; } $this->actionexporttoxls($headerarr, $rowarr, 'orders.xls'); } }
in export function there code :
public static function export($rows, $coldefs, $boolprintrows=true, $csvfilename=null, $separator='|') { $endline = "\r\n"; $returnval = ''; if($csvfilename != null) { header("cache-control: public"); header("content-description: file transfer"); header("content-disposition: attachment; filename=".$csvfilename); header("content-type: application/octet-stream"); header("content-transfer-encoding: binary"); } if($boolprintrows == true){ $names = ''; foreach($coldefs $col=>$config){ $names .= $col.$separator; } $names = rtrim($names,$separator); if($csvfilename != null){ echo $names.$endline; } else $returnval .= $names.$endline; } foreach($rows $row){ $r = ''; foreach($coldefs $col=>$config){ $data = explode("->",$config); $tmp = $row; foreach($data $dt){ $tmp = $tmp->$dt; } if($tmp!=''){ //$val = $row->$config; $val = $tmp; $r .= $val.$separator; }else{ $val = 'n/a'; $r .= $val.$separator; } } $item = trim(rtrim($r,$separator)).$endline; if($csvfilename != null){ echo $item; }else{ $returnval .= $item; } } return $returnval; }
problem not able export data according query's result.
data going exported have provided image.
query returns data between 2 dates.
can 1 help?
thanks.
Comments
Post a Comment