Re: Fwrite Function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx)
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: nickstinemates@xxxxxxxxxxx
Yahoo: nickstinemates@xxxxxxxxx
==================

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux