Currently I am doing this:
$lines = file('data/index');
foreach ($lines as $value)
list($title, $location) = explode("|", $value);
echo '<div id="entry"><a href="'.$location.'">'.$title.'</a></div>';
And the contents of data/index is:
This is a test post|http://google.com
Another test post|http://google.com
However, it only prints out the last line of that file. Almost like each “echo” is being overwritten by a new one. I’m obviously not doing this correctly. Why isn’t it working and how can I make it perform the way I intend it to?
In your code, only the third line is part of the loop. If you want to have both statements in the loop, you have to create a block:
Furthermore,
echoing HTML with PHP should be avoided. You should embed PHP into your HTML instead. It makes your code, especially your HTML markup easier to maintain and is less error prone.Reference:
foreach