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!

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀