Php problem

Programming, for all ages and all languages.
Post Reply
Kon-Tiki

Php problem

Post by Kon-Tiki »

Code: Select all

$guestbook = file("guestbook.txt");
$guestbook = explode('<hr>', $guestbook);
echo $guestbook[1];
That's the code that's being a donkey. What it should do, is open guestbook.txt as an array, cut it into pieces that end with <hr> and store it as parts of the array (like $guestbook[1], $guestbook[2], etc), then show the first part on the screen.

What it does, is show nothing. It doesn't give an error, but it doesn't show anything either. I tried changing 'hr' into things like "Zeven" and 'Zeven' and made sure there were two entries that contained that, but to no avail. I also tried a different text-file, where I put some random words in, ending with Zeven so it'd cut it there, but didn't work either. If I change $guestbook[1] to $guestbook[0] it does do something. It shows Array, and that's all.
AGI1122

Re:Php problem

Post by AGI1122 »

Try $guestbook[0] ;)

The first key created by an explode is 0 not 1.
Kon-Tiki

Re:Php problem

Post by Kon-Tiki »

I tried that, but it doesn't work. It shows Array :-\ You can see it here. The code is [url=http://raf.patat.org/rommel/test.txt[/url] here. Those lines I'm talking about are at the very bottom, but above

Code: Select all

?>
</body>
</html>
AGI1122

Re:Php problem

Post by AGI1122 »

Ah no wonder, the problem is that you used "file" file returns an array not a string. :P

Use this:

Code: Select all

$guestbook = file("guestbook.txt");
$guestbook = implode("\r\n",$guestbook);
$guestbook = explode('<hr>', $guestbook);
echo $guestbook[1];
Kon-Tiki

Re:Php problem

Post by Kon-Tiki »

Woohay! It works now. Step two: automatically groupin every fifteen parts into one string, then showing that string and a next page- and a previous page-button, unless it's the first page, then no previous page-button, or if it's the last page, then no next page-button.

*Goes to rummage around in php.net :) *
[size=0]This php sure is fun[/size]
Kon-Tiki

Re:Php problem

Post by Kon-Tiki »

Ran into another problem that seems unexplicable to me.

Code: Select all

if ($beginning == 1) {
echo "<center><a href=\"?page=", $_GET['page']+1, "><img src='tags/right.gif'></a></center>";
}
else {
echo "<center><a href=\"?page=", $_GET['page']-1, "><img src='tags/left.gif'></a></center>";
}
Works fine, but

Code: Select all

if ($beginning == 1) {
echo "<center><a href=\"?page=", $_GET['page']+1, "><img src='tags/right.gif'></a></center>";
}
else {
echo "<center><a href=\"?page=", $_GET['page']-1, "><img src='tags/left.gif'></a></center>";
echo "<center><a href=\"?page=", $_GET['page']+1, "><img src='tags/right.gif'></a></center>";
}
Links to ?page=1%3E%3Cimg%20src='tags/left.gif'%3E%3C/a%3E%3C/center%3E%3Ccenter%3E%3Ca%20href=

Anybody know what's the matter with it?
User avatar
Neo
Member
Member
Posts: 842
Joined: Wed Oct 18, 2006 9:01 am

Re:Php problem

Post by Neo »

How about closing the quotes in the <a href> tags?
You've opened them in and then not closed them. Use

Code: Select all

"\"><img src............
Only Human
Kon-Tiki

Re:Php problem

Post by Kon-Tiki »

Oops. It works now.
Post Reply