2014-06-11 52 views
1

即時通訊新的PHP和有一些通知的麻煩。 「通知:未定義抵消:1」的問題是在第38行:通知未定義的偏移

$commentator_comment = $comment_array[$i+1]; 

當我寫了這個代碼,我在一箇舊的筆記本電腦,我不得不在服務器上運行的工作,因爲WAMP等。沒有工作,我沒有得到通知。它前一段時間我寫了,但我盡我所能找到問題沒有運氣。

<?php 
$filename = "data/comments.txt"; 
    if(isset($_POST["name"]) && isset($_POST["comment"])) { 
$comment_name = @$_POST["name"]; 
$comment_comment = @$_POST["comment"]; 
$theComment = "<div id='comment'><p><b>" . $comment_name . "</b><br>" . $comment_comment . "</p></div>"; 

file_put_contents($filename, $theComment, FILE_APPEND);} 
?> 

<?php 
$comments_from_file = file_get_contents($filename); 
$comment_array = str_getcsv($comments_from_file, "|"); 
$i = 0; 
while ($i < count($comment_array)){ 
    $commentator_name = $comment_array[$i]; 
    $commentator_comment = $comment_array[$i+1]; 

    echo "$commentator_name" . "$commentator_comment"; 


    $i = $i + 2; 

}?> 

在此先感謝所有幫助,其appriciated。

+0

請問你comments.txt樣子? – Xatenev

回答

2

簡單修復「通知」:只需檢查該索引是否存在。所以這一段代碼:

$commentator_name = $comment_array[$i]; 
$commentator_comment = $comment_array[$i+1]; 

更改爲:

$commentator_name = $commentator_comment = ''; 
if (isset($comment_array[$i])) { 
    $commentator_name = $comment_array[$i]; 
} 
if (isset($comment_array[$i+1])) { 
    $commentator_comment = $comment_array[$i+1]; 
} 

當我寫了這個代碼,我在一箇舊的筆記本電腦,我不得不在服務器上運行 工作,因爲WAMP等沒有工作,我沒有得到通知 然後。

無論舊服務器設置如何,它都可能是新服務器設置&之間的錯誤報告級別。這意味着新的服務器被設置爲報告PHP通知,但舊的服務器沒有。

要調整錯誤報告設置,您可以先進入php.ini並查找error_reporting行。在Ubuntu 12.04 &其他Linux安裝的php.ini位於:

/etc/php5/apache2/php.ini 

通常是線正好被設置爲E_ALL這樣的:

error_reporting = E_ALL 

要禁用通知,您可以調整該行這樣的:

error_reporting = E_ALL & ~E_NOTICE 

而且你甚至可以添加更多的選項來禁用,如下所示將它們鏈接到該行的末尾:

error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING 

PHP錯誤級別常量的完整列表應該在php.ini文件中。這裏是一個副本,如果是在一個典型的php.ini文件的註釋所示的那些:

; Error Level Constants: 
; E_ALL    - All errors and warnings (includes E_STRICT as of PHP 6.0.0) 
; E_ERROR   - fatal run-time errors 
; E_RECOVERABLE_ERROR - almost fatal run-time errors 
; E_WARNING   - run-time warnings (non-fatal errors) 
; E_PARSE   - compile-time parse errors 
; E_NOTICE   - run-time notices (these are warnings which often result 
;      from a bug in your code, but it's possible that it was 
;      intentional (e.g., using an uninitialized variable and 
;      relying on the fact it's automatically initialized to an 
;      empty string) 
; E_STRICT   - run-time notices, enable to have PHP suggest changes 
;      to your code which will ensure the best interoperability 
;      and forward compatibility of your code 
; E_CORE_ERROR  - fatal errors that occur during PHP's initial startup 
; E_CORE_WARNING - warnings (non-fatal errors) that occur during PHP's 
;      initial startup 
; E_COMPILE_ERROR - fatal compile-time errors 
; E_COMPILE_WARNING - compile-time warnings (non-fatal errors) 
; E_USER_ERROR  - user-generated error message 
; E_USER_WARNING - user-generated warning message 
; E_USER_NOTICE  - user-generated notice message 
; E_DEPRECATED  - warn about code that will not work in future versions 
;      of PHP 
; E_USER_DEPRECATED - user-generated deprecation warnings 
+1

非常感謝!這工作 – Halis

+0

我剛剛創建了我的帳戶,我需要15點聲望upvote,但我會做到這一點,只要我得到15。 – Halis

0

您的通知將只在某些情況下表現出來,特別是當你有一個奇數在CSV文件中的元素。

看看這兩條線:

while ($i < count($comment_array)){ 
    // ... 
    $commentator_comment = $comment_array[$i+1]; 
    // ... 

while條件將保證指數$i會存在,但它使指數$i+1沒有這樣的保證。如果您的csv文件中只有一個項目,它將顯示通知。如果您有2件物品,則不會看到通知。

通過@JakeGould該解決方案將工作,而是提供一個替代解決方案(將所有對輸入操作,因爲你打算),那麼你可以簡單地改變你的,而條件是:

while(($i + 1) < count($comment_array)) { 
    // ... 

這會產生副作用,如果您有奇數個條目(基本上是沒有關聯評論的評論名稱條目),那麼它將完全忽略最後一條評論,這可能比處理更有利於您沒有評論的評論名稱。

0

短語法:

$commentator_name = isset($comment_array[$i]) ? $comment_array[$i] : ''; 

$commentator_comment = isset($comment_array[$i+1]) ? $comment_array[$i+1] : ''; 

在本地開發環境中設置的error_reporting = E_ALL。由於您已收到已設定的通知。

0

我認爲是因爲您正在嘗試訪問循環上次交互時不存在的偏移量。試試這個:

<?php 
    $filename = "data/comments.txt"; 
    if(isset($_POST["name"]) && isset($_POST["comment"])) { 
     $comment_name = @$_POST["name"]; 
     $comment_comment = @$_POST["comment"]; 
     $theComment = "<div id='comment'><p><b>" . $comment_name . "</b><br>" . $comment_comment . "</p></div>"; 
     file_put_contents($filename, $theComment, FILE_APPEND); 
    } 
?> 

<?php 
    $comments_from_file = file_get_contents($filename); 
    $comment_array = str_getcsv($comments_from_file, "|"); 
    $i = 0; 
    while ($i < (count($comment_array)-1)){ 

     $commentator_name = $comment_array[$i]; 
     $commentator_comment = $comment_array[$i+1]; 

     echo "$commentator_name" . "$commentator_comment"; 


     $i = $i + 2; 

    } 
?> 

Alternativaly,你可以嘗試過:

while ($i < count($comment_array){ 

     $commentator_name = $comment_array[$i]; 
     if ($i < (count($comment_array)-1)) { 
      $commentator_comment = $comment_array[$i+1]; 
     } 

     echo "$commentator_name" . "$commentator_comment"; 


     $i = $i + 2; 

    }