douben1891 2019-05-25 20:29
浏览 101

将循环中的php数据导出为CSV文件

I have written a code which uses the API of fantasypremierleague.com to extract data. The data is being extracted for 38 gameweeks using a for loop. I print the data using echo to verify that it is correct. I am having a problem converting the data into CSV files using a script since its in a loop. After printing information each gameweek i have added a button (in total 38 buttons) which should convert the information of each gameweek to a CSV file.

Currently, all the buttons are printing the information of gameweek 1 even though the script is part of the loop.

ini_set('max_execution_time', 300);
for ($i=1; $i <39 ; $i++) { 


$json=file_get_contents("https://fantasy.premierleague.com/drf/entry/1224012/event/".$i."/picks");
$data = json_decode($json, true);
$json1=file_get_contents("https://fantasy.premierleague.com/drf/elements/");
$data1 = json_decode($json1, true);

?>

<table id ="gameweek_history">
<tr>
 <th>Average Gameweek Score</th>
 <th>Highest Score</th>
 <th>Highest Scorer ID</th>
</tr>
</br>
</br>

<td><?PHP echo $data['event']['average_entry_score']; ?></td>
<td><?PHP echo $data['event']['highest_score']; ?></td>
<td><?PHP echo $data['event']['highest_scoring_entry']; ?></td>

</table>


<table id ="gameweek_info">
<tr>
 <th>Gameweek</th>
 <th>Active Chip</th>
 <th>ID</th>
 <th>Points</th>
 <th>Total Points</th>
 <th>Rank</th>
 <th>Overall Rank</th>
 <th>Gameweek Transfers</th>
 <th>Gameweek Transfer Cost</th>
 <th>Points on Bench</th>
 <th>Bank</th>
 </tr>
 </br>
 </br>

<td><?PHP echo $data['entry_history']['event']; ?></td>
<td><?PHP echo $data['active_chip']; ?></td>
<td><?PHP echo $data['entry_history']['id']; ?></td>
<td><?PHP echo $data['entry_history']['points']; ?></td>
<td><?PHP echo $data['entry_history']['total_points']; ?></td>
<td><?PHP echo $data['entry_history']['rank']; ?></td>
<td><?PHP echo $data['entry_history']['overall_rank']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers']; ?></td>
<td><?PHP echo $data['entry_history']['event_transfers_cost']; ?></td>
<td><?PHP echo $data['entry_history']['points_on_bench']; ?></td>
<td><?PHP echo $data['entry_history']['bank']; ?></td>

</table>

</br>
</br>
</br>

<script>function doCSV() {
 var table1 = document.getElementById("gameweek_history").innerHTML;
 var data1 = table1.replace(/<thead>/g, '')
 .replace(/<\/thead>/g, '')
 .replace(/<tbody>/g, '')
 .replace(/<\/tbody>/g, '')
 .replace(/<tr>/g, '')
 .replace(/<\/tr>/g, '
')
 .replace(/<th>/g, '')
 .replace(/<\/th>/g, ',')
 .replace(/<td>/g, '')
 .replace(/<\/td>/g, ',')
 .replace(/\t/g, '')
 .replace(/
/g, '');
 var table2 = document.getElementById("gameweek_info").innerHTML;
 var data2 = table2.replace(/<thead>/g, '')
 .replace(/<\/thead>/g, '')
 .replace(/<tbody>/g, '')
 .replace(/<\/tbody>/g, '')
 .replace(/<tr>/g, '')
 .replace(/<\/tr>/g, '
')
 .replace(/<th>/g, '')
 .replace(/<\/th>/g, ',')
 .replace(/<td>/g, '')
 .replace(/<\/td>/g, ',')
 .replace(/\t/g, '')
 .replace(/
/g, '');
 var data= data1.concat(data2);
 var mylink = document.createElement('a');
 mylink.download = "Gameweek.csv";
 mylink.href = "data:application/csv," + escape(data);
 mylink.click();
}</script>

<button onclick="doCSV()">Export HTML Table To CSV File</button>

<?PHP
}
?>

I expect each button to convert the information of the particular gameweek which the loop is currently on (for e.g. i i=1 then gameweek 1 team should be exported, if i=2 then gameweek2 team should be exported)

  • 写回答

1条回答 默认 最新

  • doucong1992 2019-05-26 12:58
    关注

    You'll need to add unique ids to every table like:

    <table id ="gameweek_history<? echo $i; ?>">
    

    Then you'll need to have your javascript refer to the id. Your existing code creates a new script for every table, so to keep doing that, each function will need a unique name that has refers to that table.

    <script>function doCSV<? echo $i; ?>() {
    var table1 = document.getElementById("gameweek_history<? echo $i; ?>").innerHTML;
    

    It would probably be better to just have one function declared outside the loop with a variable passed into it.

    <script>function doCSV(table_number) {
    var table1 = document.getElementById("gameweek_history" + table_number).innerHTML;
    

    Then add the table number to each event handler.

    <button onclick="doCSV(<? echo $i; ?>)">Export HTML Table To CSV File</button>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值