循环将php数据导出到CSV文件

时间:2019-05-25 20:29:33

标签: javascript php arrays export-to-csv

我已经编写了一个代码,该代码使用fantasypremierleague.com的API提取数据。使用for循环提取了38个游戏周的数据。我使用echo来打印数据以验证它是正确的。 由于脚本处于循环状态,我在使用脚本将数据转换为CSV文件时遇到问题。 在每个游戏周打印信息后,我添加了一个按钮(总共38个按钮),该按钮会将每个游戏周的信息转换为CSV文件。

当前,即使脚本是循环的一部分,所有按钮仍在打印游戏第1周的信息。

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, '\r\n')
 .replace(/<th>/g, '')
 .replace(/<\/th>/g, ',')
 .replace(/<td>/g, '')
 .replace(/<\/td>/g, ',')
 .replace(/\t/g, '')
 .replace(/\n/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, '\r\n')
 .replace(/<th>/g, '')
 .replace(/<\/th>/g, ',')
 .replace(/<td>/g, '')
 .replace(/<\/td>/g, ',')
 .replace(/\t/g, '')
 .replace(/\n/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 = 1,那么应该导出游戏周1的团队,如果我= 2,那么就应该导出gameweek2的团队)

1 个答案:

答案 0 :(得分:0)

您需要为每个表添加唯一的ID,例如:

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

然后,您需要让JavaScript引用ID。您现有的代码会为每个表创建一个新脚本,因此要继续执行此操作,每个函数都需要一个引用该表的唯一名称。

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

最好在循环外声明一个函数,并在其中传递一个变量。

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

然后将表号添加到每个事件处理程序。

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