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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重
  • ¥15 vite打包后,页面出现h.createElement is not a function,但本地运行正常