我遇到了OOP PHP和json数据。我对OOP并不是全新的,但我无法理解这一点。如果有人能向我解释,那就太好了!
我在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;
}
它由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);
我想要的是能够使用PHPExcel将当前的sql查询(它可以是全部或搜索结果)导出到Excel中。所以我有toExcel.php和函数toexcel($ query) - 这将需要一个查询并将其导出为excel。
现在 - 如何通过AJAX将sql查询从网格传递到toexcel?
我知道我需要添加到$ data():
$ data ['sql'] = $ sql;
下一步是什么?
更新: 我正在使用以下jquery网格: http://square-bracket.com/openjs
我知道应该通过grid或jquery
启动PHPExcel答案 0 :(得分:12)
关于你能做什么的一般概念:
创建一个按钮,例如
<a href="#" id="export">export to Excel</a>
然后在jquery中你必须创建类似的东西:
var grid = $(".grid.digital_edit").loadGrid({...}); //or similar - what you did to load the data into the grid
$('#export').click(function() {
$.ajax({
url: "export_to_excel.php", // the url of the php file that will generate the excel file
data: grid.getData(), //or similar - based on the grid's API
success: function(response){
window.location.href = response.url;
}
})
});
文件export_to_excel.php将包含生成excel文件的代码:
听起来可能过于复杂,但要尝试将目标分开并一次实现一个目标。 E.g。
修改强>
为了帮助您:您只需要一个PHP脚本/文件。同一个人将从javascript文件接收AJAX调用,将生成excel文件并将文件url返回/响应到javascript文件(按此顺序)。一个简化的例子是:
<?php
//export_to_excel.php
$data = $_POST['data']; // get the data from the AJAX call - it's the "data: grid.getData()" line from above
//... format the received data properly for the PHPExcel class
// later on in the same file:
$xls = new PHPExcel();
$xls->loadData($formattedData); //I assume that there is a similar loadData() method
$xls->exportToFile('/vaw/www/example.com/public/files/new_excel.xls'); // I assume that there is an exportToFile() method
$response = array(
'success' => true,
'url' => 'http://www.example.com/files/new_excel.xls'
);
header('Content-type: application/json');
// and in the end you respond back to javascript the file location
echo json_encode($response);
然后在javascript中显示带有此行的文件
window.location.href = response.url; //response.url is $response['url'] from the PHP script