1

私はphpが初めてで、通知に問題があります。"Notice: Undefined offset: 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;

}?>

すべての助けを前もって感謝します。

4

4 に答える 4

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.iniPHP エラー レベル定数の完全なリストは、そのファイルにあるはずです。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
于 2014-06-11T14:08:10.867 に答える
0

通知は、特に csv ファイルに奇数の要素がある場合にのみ表示されます。

次の 2 行を見てください。

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

while 条件は index$iが存在することを保証しますが、 index についてはそのような保証はしません$i+1。csv ファイルに項目が 1 つしかない場合は、通知が表示されます。アイテムが 2 つある場合、通知は表示されません。

@JakeGould によるソリューションは機能しますが、別のソリューション (意図したとおりに入力のすべてのペアで動作する) を提供するには、 while 条件を次のように変更するだけです。

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

これには、エントリの数が奇数の場合 (基本的に、コメントが関連付けられていないコメント名のエントリ)、最後のコメントが完全に無視されるという副作用があります。コメント。

于 2014-06-11T14:20:27.020 に答える
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;

    }
?>

または、次の方法も試すことができます。

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;

    }
于 2014-06-11T14:43:12.187 に答える
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 を設定します。通知を受け取っているので、すでに設定されています。

于 2014-06-11T14:26:57.773 に答える