i got stuck with OOP PHP and json data. i'm not completely new to OOP, but i can't get my head around this. if anyone can please explain to me, would be great!
i have the following grid object in PHP:
Class Grid {
var $data;
var $joins;
var $fields;
var $where;
var $table;
var $groupBy;
var $having;
var $limit;
var $order_by;
var $sort;
var $security;
var $set;
var $sql;
....
// loads data into the grid
function load() {
...
// setup the sql - bring it all together
$sql = "
SELECT $post[cols]
FROM `$table`
$joins
$where
$groupBy
$having
ORDER BY $order_by $sort
$limit
";
$this->sql = $sql;
// execute the sql, get back a multi dimensial array
$rows = $this->_queryMulti($sql);
// form an array of the data to send back
$data = array();
$data['rows'] = array();
foreach($rows as $i=>$row) {
foreach($row as $col=>$cell) {
// use primary key if possible, other wise use index
$key = $primaryKey ? $row[$primaryKey] : $i;
// primary key has an _ infront becuase of google chrome re ordering JSON objects
//http://code.google.com/p/v8/issues/detail?id=164
$data['rows']["_".$key][$col] = $cell;
}
}
...
$data['order_by'] = $order_by;
$data['sort'] = $sort;
$data['page'] = $page;
$data['start'] = $startRow + 1;
$data['end'] = $startRow + $nRowsShowing;
$data['colData'] = $colData;
$this->data = $data;
}
and it's called by AJAX callgrid.php:
$grid->load();
// here we need to add field in data[sql] = sql query, then we can pass it to toExcel() - how?
echo json_encode($grid->data);
what i'm trying to get is to be able to export current sql query (it can be all or searched results) into Excel using PHPExcel. So i've got toExcel.php with function toexcel($query) - that will take a query and export it to excel.
now - HOW do i pass sql query from grid to toexcel via AJAX?
I understand that i need to add to $data():
$data['sql'] = $sql;
what next?
UPDATE: I'm using the following jquery grid: http://square-bracket.com/openjs
I understand that PHPExcel should be initiated either by grid or jquery
A general idea of what you could do:
Create a button e.g.
Then in jquery you have to create something like:
The file export_to_excel.php will contain the code that generates the excel file:
It may sound too complex, but try to separate your goals and achieve one at a time. E.g.
EDIT
To help you a bit more: You need only one PHP script/file. The same one will receive the AJAX call from the javascript file, will generate the excel file and will return/respond the file url to the javascript file(in that order). A simplified example would be:
And then in javascript you display the file with this line