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>
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>";
<html>
<head>
<title>Un-ordered List</title>
</head>
<body>
<?php
$file = fopen("oscars.txt", "r");
$i=1;
while ($line = fgets($file)) {
if(strlen(trim($line)) == 0){
print "</ul>";
$i=0;
}
if ($i !=0){
if ($i==1) print "<b>".$line."</b>";
else {
if ($i==2) print "<ul><li>".$line."</li>";
else print "<li>".$line."</li>";
}
}
$i++;
}
?>
</body>
</html>
The result is:
Best Picture
Best Actor
Best Actress
Once you have become more confident with PHP, I recommend reading up on MySQL and databases, because it will be more suited to this kind of data storage. However, I have managed to come up with a solution to your problem, but it involves adding an asterisk (*) to the beginning of each heading in the text file. This is so the code can determine which lines are headings and which aren't.
<html>
<head>
<title>Un-ordered List</title>
</head>
<body>
<?php
$members = file("oscars.txt"); // load file into array
foreach($members as $member) // assign each object in the array to $members on each iteration
{
$member = trim($member); // perhaps unnecessary, but good practise
if(substr($member, 0, 1) == '*') // if there is an asterisk, it is a header
{
echo "<h2>" . substr($member, 1, strlen($member)) . "</h2>"; // print the heading, minus the asterisk
}
else if ($member !== '') // don't print blank lines
{
echo "<li>" . $member . "</li>"; // not a heading, so print as a list element
}
}
?>
</body>
</html>
I have almost entirely rewritten the code but I feel that some of the things you had written were unnecessary, such as using $arrlength =count($members)
when a foreach
loop is better suited to the job. It will take each string from the $members
array and then output it in place of the $member
variable.
I have also changed some formatting, but that is not important.