2013-04-28 89 views
1

首先,我很抱歉提出的問題必須是新手或簡單的問題,但似乎找不到任何我已研究解決問題的代碼。爆炸中未定義的偏移量問題

注意:未定義偏移:1 C:\用戶\約書亞\桌面\ USBWebserver V8.5 \ 8.5 \根\指派\ Table.php線14上

此錯誤由繼續偏移1 - 7。

,我目前使用從CSV文件中提取數據的代碼如下:

<?php 
$fp = fopen ("quotes.csv","r"); 
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;} 
?> 
<table> 
<tr><th>ID</th> <th>Price</th> <th>Volume </th></tr> 
<?php 

$i=0; 
while (!feof($fp)): 
    $line = fgets($fp, 2048); 
    $out[$i] = array($line); 

    list ($ID, $Price, $StockDate, $StockTime, $Change, $DayHI, $DayLOW, $Volume,) = explode(",", $out[$i][0]); 


    echo "<tr><td>$ID</td> <td>$Price</td> <td>$StockDate </td><td>$StockTime</td> <td>$Change</td> <td>$DayHI</td> <td>$DayLOW</td> <td>$Volume</td></tr>"; 
    $fp++; 
    $i++; 
endwhile; 

?> 
</table> 
<?php 


//echo "<p>".$out[0][0]."</p>"; 
//echo "<p>".$out[1][0]."</p>"; 
//echo "<p>".$out[2][0]."</p>"; 
fclose($fp); 
?> 

我不確定的東西是如何未定義的正確值的數量正在從所謂CSV。

我明白這不是一個網站教人PHP基礎知識,但任何意見將不勝感激,以幫助解決問題,我會感激!

回答

1

我想你不想+1的文件指針:

$fp++; 

刪除此行應該做的伎倆。

UPDATE

您也應該檢查fgets如果不是假的,因爲這意味着文件的末尾:

$line = fgets($fp, 2048); 
if($line === false) break; 

工作的完整示例:

<?php 
$fp = fopen ("quotes.csv","r"); 
if (!$fp) {echo "<p>Unable to open remote file.</p>"; exit;} 
?> 
<table> 
<tr><th>ID</th> <th>Price</th> <th>Volume </th></tr> 
<?php 
$out = array(); 
$i=0; 
while (!feof($fp)): 
    $line = fgets($fp, 2048); 
    if($line === false) break; 
    $out[$i] = array($line); 

    list ($ID, $Price, $StockDate, $StockTime, $Change, $DayHI, $DayLOW, $Volume) = explode(",", $out[$i][0]); 


    echo "<tr><td>$ID</td> <td>$Price</td> <td>$StockDate </td><td>$StockTime</td> <td>$Change</td> <td>$DayHI</td> <td>$DayLOW</td> <td>$Volume</td></tr>"; 
    $i++; 
endwhile; 

?> 
</table> 
<?php 


echo "<p>".$out[0][0]."</p>"; 
fclose($fp); 

echo了我從.csv開始的第一個字符串行。

+0

都能跟得上不幸的是我仍然得到偏移錯誤:/ 注意:未定義偏移量:1在C:\ Users \ Joshua \ Desktop \ USBWebserver v8.5 \ 8.5 \ root \ Assignment \ Table.php on line 14 – 2013-04-28 21:33:54

+0

好吧,讓我檢查其他東西。 – s3m3n 2013-04-28 21:41:13

+0

謝謝我的朋友! :)我希望在這些編碼方面我能做得更好,但我想我們都必須從某個地方開始! – 2013-04-28 22:19:55

0

試着做一個變量的值,爆炸和檢查變量包含所有數據

$temporal_var = explode(",", $out[$i][0]); 
print_r($temporal_var); 
0

你初始化了$out陣列?而且什麼我看你不需要$i指數,因爲你可以叫$out[] = array($line);一個元素追加到數組

0

試試這個辦法,使用PHP本身的CSV處理:

<?php 
// Set a list of headers 
$ths[0] = 'ID'; 
$ths[1] = 'Price'; 
$ths[2] = 'StockDate'; 
$ths[3] = 'StockTime'; 
$ths[4] = 'Change'; 
$ths[5] = 'DayHI'; 
$ths[6] = 'DayLOW'; 
$ths[7] = 'Volume'; 

// Open the file with error handling 
$fp = fopen('quotes.csv', 'r'); 
if(!$fp) { die('<p>Unable to open remote file.</p>'); } 
// Get the size 
$fz = filesize('quotes.csv') + 1; 
// Create a Data holder array 
$fpData = array(); 
// While there is a line. Use native fgetcsv() 
while ($fpRow = fgetcsv($fp, $fz, ',')) { 
    // For each element in the row we asign it to its header 
    for ($i=0; $i < count($fpRow); $i++) { 
     $thisRow[ $ths[$i] ] = $fpRow[ $i ]; 
    } 
    // Append to the Data array 
    $fpData[] = $thisRow; 
} 
// Close the file 
fclose($fp); 
// Test the output 
// print_r($fpData); 
?> 

<!doctype html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Get CVS</title> 
</head> 
<body> 
    <table> 
     <thead> 
      <tr> 
       <?php foreach ($ths as $thisField): ?> 
        <th><?php echo $thisField ?></th> 
       <?php endforeach ?> 
      </tr> 
     </thead> 
     <tbody> 
      <?php foreach ($fpData as $thisItem): ?> 
       <tr> 
        <td><?php echo $thisItem['ID'] ?></td> 
        <td><?php echo $thisItem['Price'] ?></td> 
        <td><?php echo $thisItem['StockDate'] ?></td> 
        <td><?php echo $thisItem['StockTime'] ?></td> 
        <td><?php echo $thisItem['Change'] ?></td> 
        <td><?php echo $thisItem['DayHI'] ?></td> 
        <td><?php echo $thisItem['DayLOW'] ?></td> 
        <td><?php echo $thisItem['Volume'] ?></td> 
       </tr> 
      <?php endforeach ?> 
     </tbody> 
    </table> 
</body> 
</html>