Jump to content

fwrite problems


moon 111

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.