douxin5953 2014-05-19 19:49
浏览 67
已采纳

试图在php中创建一个无序列表

I'm very new to php and I am trying to write a small application that reads a text file and makes it into an un-ordered list. My goal is to make the text file more readable, so I want to make the headers bold, and simply make the listed items have little markers by them (like they should if they are in an un-ordered list). I wrote the php to do this, but it is not doing what I expected. Instead, it takes each line and makes it bigger and bigger font until it gets so big its unreadable. I cannot see what I am doing wrong here. Does anyone see something that I am missing or have written incorrectly? Below I have pasted the original text file and my php code respectively. Thanks in advance for your help and patience with my limited php knowledge.

Best Picture
American Hustle
Captain Phillips
Dallas Buyers Club
Gravity
Her
Nebraska
Philomena
12 Years a Slave
The Wolf of Wall Street

Best Actor
Christian Bale (American Hustle)
Bruce Dern (Nebraska)
Leonardo DiCaprio (The Wolf of Wall Street)
Chiwetel Ejiofor (12 Years a Slave)
Matthew McConaughey (Dallas Buyers Club)

Best Actress
Amy Adams (American Hustle)
Cate Blanchett (Blue Jasmine)
Sandra Bullock (Gravity)
Judi Dench (Philomena)
Meryl Streep (August: Osage County)

** The 3 headers are supposed to be best picture, best actor, and best actress.

** Below is my php code so far:

<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;
echo $members;
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>
  • 写回答

3条回答 默认 最新

  • duanchai0028 2014-05-19 20:05
    关注

    Change = into == in the if. And change the second <h2> into </h2>.

    But for compactness, consider this code:

    echo "<html><head><title>Un-ordered List</title></head><body>";
    foreach( array_map("htmlspecialchars", file("oscars.txt")) as $line ) {
      if (!isset($flag)) {
        $flag = true;
        echo "<h2>{$line}</h2><ul>";
      } else {
        echo "<li>{$line}</li>";
      }
    }
    if (isset($flag)) {
      echo "</ul>";
    }
    echo "</body></html>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法