dongqi0644 2014-05-16 12:22
浏览 12
已采纳

使用php从文本文件中生成无序列表[关闭]

I'm very new to php and I'm trying to create an unordered list from a text file. I read in the text file "oscars.txt" and then create the unordered list. I believe my logic is correct, but when I host the page, I cannot get anything to show up. It says it cannot load my page. This leads me to believe that something in my syntax is incorrect. Does anyone have any ideas what I have done incorrectly here and how I can go about resolving it? Thanks for any help. Here is what I have done so far:

<html>
    <head>
    <title>Un-ordered list</title>
    </head>
    <body>
    <?php
        $file = fopen("oscars.txt", "r")
        $i=0;
        while(!feof($myfile)){
            $members[]= fgets($file);
        }
        fclose($file);
        $arrlength =count($members)
        $title = True;
        for($i=0;$i<($arrlength);$i++){
            if($title=True){
                echo "<h2"> . $members[$i] . "<h2><ul>";
                $title = False;
            }
            if(trim($members[$i])==''){
                echo "</ul><h2>" . $members[{$i+1] . "</h2><ul>";
                $i++;
            } else { 
                echo "<li>" . $members[$i] . "</li>" ;
            }
        }
    ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • duanjiong2021 2014-05-16 12:53
    关注

    There are quite a few things wrong with the code.

    1. A missing semi-colon in $file = fopen("oscars.txt", "r")

    2. Calling the wrong file while(!feof($myfile)){ it should be $file

    3. Another missing semi-colon for $arrlength =count($members)

    4. A misplaced quote in echo "<h2"> . $members[$i] . "<h2><ul>";

    5. Plus, a brace inside $members[{$i+1]

    Reworked:

    <html>
        <head>
        <title>Un-ordered list</title>
        </head>
        <body>
        <?php
            $file = fopen("oscars.txt", "r");
            $i=0;
            while(!feof($file)){
                $members[]= fgets($file);
            }
            fclose($file);
            $arrlength =count($members);
            $title = True;
            for($i=0;$i<($arrlength);$i++){
                if($title=True){
                    echo "<h2>" . $members[$i] . "<h2><ul>";
                    $title = False;
                }
                if(trim($members[$i])==''){
                    echo "</ul><h2>" . $members[$i+1] . "</h2><ul>";
    
    
                    $i++;
                } else { 
                    echo "<li>" . $members[$i] . "</li>" ;
                }
            }
        ?>
        </body>
    </html>
    

    Add/enable error reporting to the top of your file(s) which will help during production testing.

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?