2011-11-21 65 views
0

編輯:我調用函數writecol()進一步在標籤頁面下。在trxtt.txt數據php動態構建表格行

例如:

東南亞,2222,編碼1

冬歇期,3333,代碼2

我是相當新的PHP。我想基於從數組中讀取的變量動態構建錶行。當我調用這個函數時,我沒有收到錯誤信息,但沒有任何反應。任何想法我做錯了什麼?

$x = file_get_contents('textt.txt'); 
$y = explode("\r\n", $x); 

function writecol(){ 
    foreach ($y as $value) { 
     $z = explode(",", $value); 
     echo "<tr class='visible'><td class='underlinecenter'>" . $z[0] . "</td> <td></td> <td colspan='3' class='underlinecenter'>" . $z[1] . "</td><td></td><td colspan='3' class='underlinecenter'>" . $z[2] . "</td></tr>"; 
    } 
} 
+0

請給出一些'textt.txt' –

回答

2

您似乎沒有調用函數,您的函數也沒有準備好接收帶有數據的變量。

$ Y =後爆炸....插入: writecol($y);

然後用

function writecol($y){

0

更換function writecol(){你必須調用writecol()功能

0

嗯......你從來沒有實際調用函數。因此,PHP只是不知道你打算在你創建的數組上使用這個函數。此外,您應該爲您的功能添加一個參數,因爲從writecol()內您的變量$y將不可見。

試試這樣說:

$y = explode(...); 
function writecol($array) { 
    foreach ($array as $value) { // your code } 
} 
writecol($y); 
0

首先要確保你有這些上(把它們在PHP標籤下的頂部)進行測試,所以你可以看到錯誤

ini_set('display_errors','On'); 
ini_set('error_reporting', -1); 

超越錯誤,你沒有調用這個函數。改變你的功能,並添加一個電話:

function writecol($y){ # <-- pass a variable and call it $y 
    foreach ($y as $value) { 
     $z = explode(",", $value); 

     echo "<tr class='visible'><td class='underlinecenter'>" . $z[0] . "</td> <td></td> <td colspan='3' class='underlinecenter'>" . $z[1] . "</td><td></td><td colspan='3' class='underlinecenter'>" . $z[2] . "</td></tr>"; 
    } 
} 
writecol($y); #<-- function call sending the variable $y