通过 AJAX 将数据从 PHP 类传递到 PHPExcel
我被 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
我知道 PHPExcel 应该由 grid 或 jquery 启动
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以做什么的一般想法:
创建一个按钮,例如
然后在jquery中您必须创建类似的内容:
文件export_to_excel.php将包含生成excel文件的代码:
这听起来可能太复杂,但请尝试将您的目标分开并一次实现一个目标。例如
编辑
为您提供更多帮助:您只需要一个 PHP 脚本/文件。同一个将从 javascript 文件接收 AJAX 调用,生成 excel 文件并将文件 url 返回/响应到 javascript 文件(按顺序)。一个简化的例子是:
然后在 javascript 中用这一行显示文件
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