Woad.php Posted December 14, 2006 Share Posted December 14, 2006 I have a shoutbox.txt that looks something similar to this:[code]<div align="left" style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#FFFFFF; font-size:10px;"> <b>Anonymous</b> ? <br><b>Anonymous</b> ? <br><b>24.16.80.177</b> ? <br><b>RA</b> ? <br><b>24.16.80.177</b> ? <br><b>asdfafdf</b> ? <br><b>asdfafdf</b> ? <br><b>asdf</b> ? Enterasdf Message<br><b>Beaches</b> ? Bork bork<br><b>Sup?</b> ? Hahaha<br><b>Anonymous</b> ? <br>[/code]And right now I'm using this code to show whats been said:[code]<table bgcolor="#292929" border="0" align="center" cellpadding="3" cellspacing="0" width="98%"> <tr> <td valign="bottom" style="background-color: #444444; border: 1px solid #000000"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><?php include "shoutbox.txt"; ?></font></td> </tr> </table>[/code]How could I only show to people visiting the site only say the most recent 10 entries and delete the rest? Quote Link to comment Share on other sites More sharing options...
btherl Posted December 14, 2006 Share Posted December 14, 2006 Instead of just adding new entries to shoutbox.txt, you can read in shoutbox.txt, delete the oldest entry, add the newest entry and rewrite it. Is this the kind of solution you are looking for?The "sophisticated" way would be to use mysql, but it's not necessary for something so simple. Quote Link to comment Share on other sites More sharing options...
Woad.php Posted December 14, 2006 Author Share Posted December 14, 2006 Yes, that's exactly what I wanted to do, but I couldn't figure out how to delete specific parts of the text file. Could you post the code to do all of that? Quote Link to comment Share on other sites More sharing options...
btherl Posted December 14, 2006 Share Posted December 14, 2006 Hmm.. assuming the format of shoutbox.txt is fixed, you could do[code=php:0]$shoutbox_lines = file('shoutbox.txt') or die('Couldn't open shoutbox.txt');# This part depends on order of entries.. I am assuming the latest entry goes at the bottom, and top-most entry is deleted.unset($shoutbox_lines[1]); # Delete the second line (numbering starts at 0$shoutbox_lines[] = $new_line;file_put_contents('shoutbox.txt', join("\n", $shoutbox_lines)) or die('Couldn't write new shoutbox.txt');[/code] Quote Link to comment Share on other sites More sharing options...
keeB Posted December 14, 2006 Share Posted December 14, 2006 [code]<?php/** * This apps assume 1 line / user. */class shoutbox { public static $sb_arrData; public static $sb_text; public static $sb_maxReturn=10; public function shoutbox($textfile="shoutbox.txt") { $handle = fopen($textfile, "r"); $this->sb_text = fread($handle, filesize($textfile)); $this->sb_arrData = explode("\n", $this->sb_text); //turn contents in to array seperated by spaces; } public function returnTotalCount() { return count($this->sb_arrData); } public function returnTop10() { $totalCount = shoutbox::returnTotalCount(); $last10 = $totalCount - 10; for ($x=$last10; $x<=$totalCount; $x++) { print $this->sb_arrData[$x]; } }}?><table bgcolor="#292929" border="0" align="center" cellpadding="3" cellspacing="0" width="98%"> <tr> <td valign="bottom" style="background-color: #444444; border: 1px solid #000000"><font face="Verdana, Arial, Helvetica, sans-serif" size="1"><div align="left" style="font-family:Verdana, Arial, Helvetica, sans-serif; color:#FFFFFF; font-size:10px;"> <?php $s = new shoutbox();print $s->returnTop10(); ?></font></div></td> </tr> </table>[/code]Reformatted your code to return the last 10 messages in the shoutbox. It is actually configurable by the [code=php:0]$sb_maxReturn[/code] variable.Let me know if you need some more help...- Keeb Quote Link to comment Share on other sites More sharing options...
Woad.php Posted December 15, 2006 Author Share Posted December 15, 2006 Thanks guys for your help. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.