2010-12-09 36 views
-1

我有一個HTML表格這種類型的記錄:只剩下最後一個元組填充分隔的文件用PHP

<tr class="simplehighlight" onclick="window.location.href='./games/game191.php';"> 
<td>06/09/2007</td><td>Jennifer Woods Memorial Grand Prix</td><td>C54</td> 
<td>Nikolayev, Igor</td><td>2431</td><td>Parry, Matt</td><td>2252</td><td class="text-center">1-0</td></tr> 

我想在一個分隔的文件讀取,使一個數組,並填充表:(樣本唱片)

game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry, Matt|2252|1-0 

我嘗試這樣做,但它只能從數據文件(/games.csv

<?php 
// open delimited data file "|" if needed and read. 
//game191|06/09/2007|Jennifer Woods Memorial Grand Prix|C54|Nikolayev, Igor|2431|Parry,   Matt|2252|1-0 

if(!isset($_SESSION['games_array'])) {$file = $_SERVER['DOCUMENT_ROOT'].'/games.csv'; 
$fp = fopen($file,"r"); $list = fread($fp, filesize($file)); 
$_SESSION['games_array'] = explode("\n",$list); fclose($fp);} 

// extract variables from each tuple by iteration 
foreach ($_SESSION['games_array'] as $v);{ 
$token = explode("|", $v); 

//write the table row and table data 
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/"; 
echo $token[0]; echo ".php';\">"; 
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>"; 
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>"; 
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>"; 
echo "<td>";echo $token[7];echo "</td>"; 
echo "<td class=\"text-center\">"; echo $token[8];echo "</td>"; 
echo "</tr>";}; 
?> 
顯示的最後一個記錄

我錯過了什麼?

回答

4
foreach ($_SESSION['games_array'] as $v);{ 

應該

foreach ($_SESSION['games_array'] as $v) { 
+0

如果每次我犯了這個錯誤,我都有一毛錢......好吧,反正我有幾毛錢。 – AgentConundrum 2010-12-09 23:41:36

+0

鋒利的眼睛爲+1 – devrooms 2010-12-09 23:41:54

0

嘗試用文件():

$lines = file('filename'); 

foreach ($lines as $line_num => $line) { 
    echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n"; 
} 
0

你有正確的權限來訪問該文件?是你的php error_reporting設置爲顯示所有錯誤?你可以嘗試只使用相對路徑,你嘗試過嗎?嘗試使用file_get_contents ...

0

這比我想象的更簡單......我錯誤地重複使用了其他腳本中的舊垃圾代碼。

我發現這個,它的工作原理!

<?php 
$text = file('games.csv'); foreach($text as $line) {$token = explode("|", $line); 
echo "<tr class=\"simplehighlight\" onclick=\"window.location.href='./games/"; 
echo $token[0]; echo ".php';\">"; 
echo "<td>";echo $token[1];echo "</td>"; echo "<td>";echo $token[2];echo "</td>"; 
echo "<td>";echo $token[3];echo "</td>"; echo "<td>";echo $token[4];echo "</td>"; 
echo "<td>";echo $token[5];echo "</td>"; echo "<td>";echo $token[6];echo "</td>"; 
echo "<td>";echo $token[7];echo "</td>"; 
echo "<td class=\"text-center\">"; echo $token[8]; echo "</td>"; 
echo "</tr>";}; 
?> 

讓我知道,如果你看到任何改進或更快的功能。謝謝 !

相關問題