dongqi7631 2014-07-03 19:27
浏览 37
已采纳

将文件读入表时未定义的偏移错误

I've got Undefined Offset error, but I don't know what's wrong.

Here's the code

<?php

        $documentRoot = $_SERVER['DOCUMENT_ROOT'];
        $fileName = "$documentRoot/Tutorials/PHP/Assignments/Assignment 3/data/quote.txt";
        $filePointer = fopen($fileName, 'r');

        $lineCounter = 0;
        $display = "";

        while(true)
        {

            $line = fgets($filePointer);
            list($firstName, $lastName, $contactMethod, $phoneMail, $resideCity, $comments) = explode("|", $line);

            if (!isset($comments))
            {
                $comments = "";
            }

            $lineCounter++;

            if(feof($filePointer))
            {
                break;
            }

            if($lineCounter % 2 == 0)
            {
                $style = "style = 'background-color:white';";
            }
            else
            {
                $style = "style = 'background-color:lightgray';";
            }

            //Write to table

            print"<tr $style>";
            print"<td>$firstName</td>";
            print"<td>$lastName</td>";
            print"<td>$contactMethod</td>";
            print"<td>$phoneMail</td>";
            print"<td>$resideCity</td>";
            print"<td>$comments</td>";
            print"</tr>";
        }

            fclose($filePointer);




        ?>

I added:

            if (!isset($comments))
            {
                $comments = "";
            }

Because I assume the offset error appears because I didn't type in anything in comments. However, I still get this error.

Please help.

Here's the error message:

Notice: Undefined offset: 5 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

Notice: Undefined offset: 4 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

Notice: Undefined offset: 3 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

Notice: Undefined offset: 2 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

Notice: Undefined offset: 1 in /Users/Lio/Documents/Eligio's/Tutorials/PHP/Assignments/Assignment 3/quotes.php on line 32

  • 写回答

1条回答 默认 最新

  • douwen1313 2014-07-03 19:33
    关注

    The problem is before your isset... so when you do explode and pass results to list... that expects 6 parametes only receive 5 or less and you get the error.

    I suggest the following replacement:

    $line = fgets($filePointer);
    $data=explode("|", $line);
    while(count($data)<6) $data[]="";
    list($firstName, $lastName, $contactMethod, $phoneMail, 
    $resideCity, $comments) = $data;
    

    That will add enough data for list to set vars.

    and should work!

    Happy coding!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!