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条)

报告相同问题?