function render(Grid_Abstract $grid) { /* Data to be inserted into excel in an array of arrays * Each array is the avg monthly temp of a different city */ $data = $this->_recordSet; $csvTitle = $this->_filetitle; /* We know the keys of each sub-array are the same, so * extract them from the first sub-array and set them * to be our column titles */ $titleArray = $this->_headers; /* Set your desired delimiter. You can make this a true * .csv and set $delimiter = ","; but I find that tabs * work better as commas can also be present in your data. * Note that you must use the .tsv or .xls file extension for Excel * to correctly interpret tabs. Otherwise if you are using commas * for your delimiter, use .csv for your file extension. */ $delimiter = ","; //Set target filename - see above comment on file extension. $filename = $this->_filename . ".csv"; //Send headers header('Content-type: text/csv; charset=utf-8'); //header ( "Content-type: application/octet-stream" ); //header("Content-Type: text/csv"); header("Content-Disposition: attachment; filename={$filename}"); header("Pragma: no-cache"); header("Expires: 0"); //print the title to the first cell //print $csvTitle . "\r\n"; $fields = array(); foreach ($grid->ExtFields as $obt) { $titleArray[] = lcfirst($obt['field']); } //Separate each column title name with the delimiter $titleString = implode($delimiter, $titleArray); print $titleString . "\r\n"; //Loop through each subarray, which are our data sets $Record = $grid->query(); $dataRowString = array(); foreach ($Record as $rs) { $i = 0; $subArray = null; while (isset($grid->ExtFields[$i]['field'])) { $strcell = ''; if (trim($grid->ExtFields[$i]['function']) != '') { $funcName = $grid->ExtFields[$i]['function']; $data = $rs; if (function_exists($funcName)) { $strcell .= call_user_func($funcName, $data); } else { $strcell .= "griderror : NoFuncDefined"; } } else { $strcell .= $rs[$grid->ExtFields[$i]['field']]; } //if ($strcell=='')$strcell="''" ; $de = $this->_delimited; $subArray[] = $de . "{$strcell}" . $de; ++$i; } $dataRowString = implode($delimiter, $subArray); print $dataRowString . "\r\n"; } }
public function render(Grid_Abstract $grid) { header("Content-Type: text/xml;charset=utf-8"); //$xw->startDocument ( '1.0', 'UTF-8' ); //$xw->endDtd (); //print_r($this->ExtFields); $Record = $grid->query(); $pgoto = $grid->pgoto(); $stop = $grid->getPagelen(); $Begin = $pgoto + 1; $End = $pgoto + $stop; $fields = array(); foreach ($grid->ExtFields as $obt) { $fields[] = lcfirst($obt['field']); } $xml = '<?xml version="1.0"?>'; $xml .= "<data>"; $xml .= "<header>"; $xml .= $this->writeElement('field', (string) count($grid->ExtFields)); //$Extfields $xml .= $this->writeElement('numberOfResults', (string) count($Record)); $xml .= $this->writeElement('totalRecords', (string) $grid->getTotalrecord()); $xml .= $this->writeElement('orderBy', (string) $grid->getOrder()); $xml .= $this->writeElement('orderType', (string) $grid->getType()); $xml .= $this->writeElement('page', (string) $grid->pageselected()); $xml .= $this->writeElement('totalpage', (string) $grid->getTotalpage()); $xml .= $this->writeElement('pagelen', (string) $grid->getPagelen()); $xml .= $this->writeElement('filter', (string) $grid->get_textSearch()); $xml .= $this->writeElement('viewingRows', $Begin . ' - ' . $End); $xml .= $this->writeElement('fields', join(',', $fields)); $xml .= $this->writeElement('uri', $grid->getAllparam()); //$xml .= $this->writeElement ( 'table',$this->strTable); //$xml .= $this->writeElement ( 'sql',$grid->getFullSql() ); //$xml .= $this->writeElement ( 'url', $_SERVER['REQUEST_URI'] ); $xml .= "</header>"; //print_r($Record); //echo $this->intTotalrecords; //echo count($Record); if (count($Record) > 0) { $xml .= "<items>"; foreach ($Record as $rs) { $xml .= "<" . $grid->getObjName() . " rownumber='" . $rs['rowid'] . "' " . ">"; $i = 0; while (isset($grid->ExtFields[$i]['field'])) { $strcell = ''; if (trim($grid->ExtFields[$i]['function']) != '') { $funcName = $grid->ExtFields[$i]['function']; $data = $rs; if (function_exists($funcName)) { $strcell .= call_user_func($funcName, $data); } else { $strcell .= "griderror : NoFuncDefined"; } } else { $strcell .= $rs[$grid->ExtFields[$i]['field']]; } $xml .= $this->writeElement($grid->ExtFields[$i]['field'], $strcell); ++$i; //$xml .= "</".$this->objName.">"; } $xml .= "</" . $grid->getObjName() . ">"; } $xml .= "</items>"; } else { $xml .= "<items/>"; } $xml .= "</data>"; return $xml; }