Jump to content

I need some help with my shoutbox


Woad.php

Recommended Posts

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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.