我已经使用一个函数填充了一个多维 PHP 数组,我希望允许我的管理员用户下载内容。
我找到了一个 PHP 函数,它应该允许我将数组导出为 CSV 并将其放入我的 functions.php 中,使用第二个函数将其挂接到 AJAX 并使用 jQuery 来触发 AJAX 函数。
有什么问题?
所以我 99% 确定 AJAX 正确地发送到 PHP 函数,但由于某种原因没有开始下载。
我对此进行了很多研究,但一直在努力寻找解决方案 - 非常感谢指出正确方向的观点!
// Function to generate download
function convert_to_csv( $input_array, $output_file_name, $delimiter ) {
/** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
$f = fopen( 'php://memory', 'w' );
/** loop through array */
foreach ( $input_array as $line ) {
/** default php csv handler **/
fputcsv( $f, $line, $delimiter );
}
/** rewrind the "file" with the csv lines **/
fseek( $f, 0 );
/** modify header to be downloadable csv file **/
header( 'Content-Type: application/csv' );
header( 'Content-Disposition: attachement; filename="' . $output_file_name . '";' );
/** Send file to browser for download */
fpassthru( $f );
}
我已经使用另一个函数将它连接到 Wordpress AJAX
function laura_function() {
$input_array = $_POST["inputarray"];
convert_to_csv($input_array, 'output.csv', ',');
exit;
}
add_action('wp_ajax_nopriv_ajaxlaurafunction', 'laura_function');
add_action('wp_ajax_ajaxlaurafunction', 'laura_function');
我还编写了一些 jQuery 函数来在单击按钮时进行 AJAX 调用
<button type="button" id="downloadcsv">Download CSV</button>
<script>
jQuery(document).ready(function () {
jQuery('#downloadcsv').click(function () {
var data = {
action: 'ajaxlaurafunction', // Calls PHP function - note it must be hooked to AJAX
inputarray: '<?php echo $inputarray?>', // Sends a variable to be used in PHP function
};
// This is a shortened version of: https://api.jquery.com/jquery.post/
jQuery.post("<?php echo admin_url('admin-ajax.php'); ?>", data, function () {
//window.location.replace(location.pathname)
});
});
});
最佳答案
根据@charlietfl 的建议,我发现“您不能通过 Ajax 完成此操作,因为 JavaScript 无法将文件直接保存到用户的计算机(出于安全考虑)。- According to this post”
替代解决方案
我的替代解决方案是使用“post 方法”来设置 post 参数。
// Form on Page
<form method="post" id="download_form" action="">
<input type="submit" name="download_csv" class="button-primary" value="Download CSV" />
</form>
如果“download_csv”参数已发布,此代码将运行以下载数据。目前我的代码将输出虚拟数据,但我正在努力。
add_action("admin_init", "download_csv");
function download_csv() {
if (isset($_POST['download_csv'])) {
function outputCsv( $fileName, $assocDataArray ) {
ob_clean();
header( 'Pragma: public' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' );
header( 'Cache-Control: private', false );
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment;filename=' . $fileName );
if ( isset( $assocDataArray['0'] ) ) {
$fp = fopen( 'php://output', 'w' );
fputcsv( $fp, array_keys( $assocDataArray['0'] ) );
foreach ( $assocDataArray AS $values ) {
fputcsv( $fp, $values );
}
fclose( $fp );
}
ob_flush();
}
// This is dummy data.
$data = array(
array( 'item' => 'Server', 'cost' => 10000, 'approved by' => 'Joe' ),
array( 'item' => 'Mt Dew', 'cost' => 1.25, 'approved by' => 'John' ),
array( 'item' => 'IntelliJ IDEA', 'cost' => 500, 'approved by' => 'James' ),
);
outputCsv( 'expenses.csv', $data );
exit; // This is really important - otherwise it shoves all of your page code into the download
}
}
关于javascript - 在 Wordpress Admin 中从 PHP 数组创建 'Download to CSV' 按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135172/
出于纯粹的兴趣,我很好奇如何按顺序创建PI,而不是在过程结果之后生成数字,而是让数字在过程本身生成时显示。如果是这种情况,那么数字可以自行产生,我可以对以前看到的数字实现垃圾收集,从而创建一个无限系列。结果只是在Pi系列之后每秒生成一个数字。这是我通过互联网筛选的结果:这是流行的计算机友好算法,类机器算法:defarccot(x,unity)xpow=unity/xn=1sign=1sum=0loopdoterm=xpow/nbreakifterm==0sum+=sign*(xpow/n)xpow/=x*xn+=2sign=-signendsumenddefcalc_pi(digits
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我的代码目前看起来像这样numbers=[1,2,3,4,5]defpop_threepop=[]3.times{pop有没有办法在一行中完成pop_three方法中的内容?我基本上想做类似numbers.slice(0,3)的事情,但要删除切片中的数组项。嗯...嗯,我想我刚刚意识到我可以试试slice! 最佳答案 是numbers.pop(3)或者numbers.shift(3)如果你想要另一边。 关于ruby-多次弹出/移动ruby数组,我们在StackOverflow上找到一
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要读入一个包含数字列表的文件。此代码读取文件并将其放入二维数组中。现在我需要获取数组中所有数字的平均值,但我需要将数组的内容更改为int。有什么想法可以将to_i方法放在哪里吗?ClassTerraindefinitializefile_name@input=IO.readlines(file_name)#readinfile@size=@input[0].to_i@land=[@size]x=1whilex 最佳答案 只需将数组映射为整数:@land边注如果你想得到一条线的平均值,你可以这样做:values=@input[x]
我对最新版本的Rails有疑问。我创建了一个新应用程序(railsnewMyProject),但我没有脚本/生成,只有脚本/rails,当我输入ruby./script/railsgeneratepluginmy_plugin"Couldnotfindgeneratorplugin.".你知道如何生成插件模板吗?没有这个命令可以创建插件吗?PS:我正在使用Rails3.2.1和ruby1.8.7[universal-darwin11.0] 最佳答案 随着Rails3.2.0的发布,插件生成器已经被移除。查看变更日志here.现在
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer