Re: Fwrite Function
| From: | Nick Stinemates | Date: | Sun, 17 Feb 2008 07:46:09 +0000 |
| Subject: | Re: Fwrite Function | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-269425@lists.php.net to get a copy of this message | ||
Yuval Schwartz wrote:
> Hello,
>
> Can you please help me, I am writing code where I create a file and write to
> it from a form on a webpage and then read and display this file on the
> webpage.
> I want to change the color of the text that is written to the file.
> Do you know how I can do this?
>
> This is some of my code if you need clarification:
> * $boardFile = "MessageBoard.txt";
> $boardFileHandle = fopen($boardFile,'a') or die("can't open file");
> fwrite($boardFileHandle, $name);
> fwrite($boardFileHandle, $talk);
> fclose($boardFileHandle);
> }
> $boardFile = "MessageBoard.txt";
> $boardFileHandle = fopen($boardFile,"r");
> $talkR = fread($boardFileHandle, filesize($boardFile));
> fclose($boardFileHandle);
> echo $talkR;*
> **
> **
> Thanks
>
>
First question is -- why aren't you using a database for this
information? I would recommend sqlite (http://www.sqlite.org/)
Now that that's taken care of, if you're trying to color the text on
output, I would do it like this:
_______________________________________________________________________
<?php
$name = "nick"; //added for testing
$talk = "a message"; //added for testing
$NAMEFORMAT = '<font color="red">';
$MESSAGEFORMAT = '<font color="blue">';
$boardFile = "MessageBoard.txt";
$boardFileHandle = fopen($boardFile,'a') or die("can't open file");
/*
* Here we are going to write to the file, notice I added
another line that prints a comma. This will be useful
so that you can easily parse out and potentially
format the 2 elements at will
*/
fwrite($boardFileHandle, $name);
fwrite($boardFileHandle, ","); // added
fwrite($boardFileHandle, $talk);
fclose($boardFileHandle);
$boardFile = "MessageBoard.txt";
$boardFileHandle = fopen($boardFile,"r");
/* removed
$talkR = fread($boardFileHandle, filesize($boardFile));
*/
while (($data = fgetcsv($boardFileHandle, 1000, ",")) !== FALSE) {
/*
* put 1000byte buffer to $data, this also goes 1 line at a time.
*/
echo $NAMEFORMAT . $data[0] . "</font>"; // print the name
echo $MESSAGEFORMAT . $data[1]; // print the text
}
fclose($boardFileHandle);
?>
If you have any questions regarding the implementation I suggest the
following reading material:
http://us3.php.net/manual/en/function.fgetcsv.php
Good luck!
==================
Nick Stinemates (nick@stinemates.org)
http://nick.stinemates.org
AIM: Nick Stinemates
MSN: nickstinemates@hotmail.com
Yahoo: nickstinemates@yahoo.com
==================