moon 111 Posted March 30, 2008 Share Posted March 30, 2008 I'm working on a shoutbox using OOP but my addMessage function isn't working. Also, can you tell me if this is the correct way of writing it? I'm rather new to OOP and I'm not sure whats what. Would there be a better/easier way? Thanks class_lib.php <?php class shoutbox { public $file = 'shoutbox.txt'; public function __construct() { } public function parseFile() { $tempfile = file($this->file); foreach($tempfile as $value) { $temp = explode(':-:', $value); $file[] = array($temp[0], $temp[1], $temp[2]); } return $file; } public function displayShoutBox() { $file = $this->parseFile(); foreach($file as $value) { $date[] = $value[0]; $name[] = $value[1]; $message[] = $value[2]; } $content .= "<table border='0' width='500' cellspacing='0' cellpadding='5' bgcolor='#FFFFFF'>"; $i = 0; foreach($name as $value) { $content .= "<tr><td>" . $date[$i] . "<td><td><b>" . $name[$i] . ":</b> " . $message[$i] . "</td></tr>"; $i++; } $content .= "</table>"; return $content; } public function addMessage($name, $message) { $handle = fopen($this->file, "rw"); if(filesize($this->file) > 0) $content = fread($handle, filesize($this->file)); else $content = ""; $date = date("H:i:s"); $content = $date . ":-:" . $name . ":-:" . $message . "\r\n" . $content; fwrite($handle, $content); } } ?> shoutbox.php (main) <?php include_once('class_lib.php'); $shoutbox = new shoutbox; $shoutbox->addMessage("Moshe Teutsch", "Testing..."); echo $shoutbox->displayShoutBox(); ?> The text file has the following format: 10:19:50:-:Moshe Teutsch:-:Testing... 10:26:26:-:Moshe Teutsch:-:Test number two... etc... Link to comment https://forums.phpfreaks.com/topic/98607-fwrite-problems/ Share on other sites More sharing options...
moon 111 Posted March 30, 2008 Author Share Posted March 30, 2008 SOLVED! (Where did the solved button go?) I wrote rw instead of r+. Also I didn't need to read it in the first place so only r. I hate small details... Link to comment https://forums.phpfreaks.com/topic/98607-fwrite-problems/#findComment-504684 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.