Page 1 of 1

PHP blog scrip

Posted: Wed Jun 15, 2005 5:20 pm
by ericburnard
hey there. I have been playing with php for the past few weeks and have come up with a simple txt based blog syste. After i fill out the form and submit it it goes to addblog.php and adds it to the txt file news.txt. Eventually i have alot of blogs in the news.txt file.

What i am wanting my code to do now is that when i submit a new post it adds it to the new.txt file and takes the previous post and adds it to the oldnews.txt file (so that the blog list grows there)

My code for the addblog.php is -

Code: Select all

<?php
 if ($message != '') 
 {

 $newRow = '<div class=&quote;viewGuestbook&quote;><b>' . ($subject) . ' - ' .
  	    ($date) . '</b> &#1111;' . ($time) . '] ' . '<br>' . ($message) . '<br>' . '<br>' . '<br>' .
  	    '</div>';

  	      
  $oldRows = join ('', file ('news.txt') );
  $fileName = fopen ('news.txt', 'w');
  fputs ($fileName, $newRow . chr(13) . chr(10) . $oldRows);
  fclose ($fileName);
 }
 
 include (&quote;http://eric.madashatters.com/infusion/index.php&quote;);
?>
Hope this all makes sence

Help would be great
Thanks

Eric

Posted: Thu Jun 16, 2005 1:55 am
by Syranide
that might be quite tricky because of they way you set it up.
the easiest thing you can do is (I guess), is to read everything in the file using file_get_contents, then you do strrpos (strpos from the back) on '<div class="viewGuestbook">'. then you simply substr the file content, start 0, length is the strrpos return value. and then write the string from substr to the file.

try working it out yourself, I'll help you with code otherwise.

Code: Select all

$oldRows = join ('', file ('news.txt') );
$fileName = fopen ('news.txt', 'w');
fputs ($fileName, $newRow . chr(13) . chr(10) . $oldRows);
fclose ($fileName);
is the same as (but better)

Code: Select all

$content = file_get_contents('news.txt');
$fileName = fopen ('news.txt', 'w');
fputs ($fileName, $newRow . chr(13) . chr(10) . $content);
fclose ($fileName);
EDIT: otherwise you might want to take a look at ftruncate, but I'd bet that would just fuzz up your mind.

Posted: Thu Jun 16, 2005 2:16 pm
by ericburnard
Ok i have used the code that you have posted and it still works the same, which is good (i guess). Im sorry but my php knowlage is small as i have only just started learning a few months ago. Could you give me a bit of a hand with the coding as i dnt know what the strrpos thing is

thanks again

Eric

Posted: Thu Jun 16, 2005 2:25 pm
by pickle
Open the new.text file and read in all the contents. Then, open the old.txt file with mode a+ (which will put the pointer at the end of the file), and write what you just read from new.txt.

Then, open new.text with mode w+ (which will truncate the file to zero length, effectively removing what was already there), and write in the latest blog entry.

If you ever start feeling more comfortable with PHP, try putting this into a database. A database will make things quite a bit easier, quicker and with less overhead.

Posted: Thu Jun 16, 2005 2:34 pm
by ericburnard
pickle wrote:Open the new.text file and read in all the contents. Then, open the old.txt file with mode a+ (which will put the pointer at the end of the file), and write what you just read from new.txt.

Then, open new.text with mode w+ (which will truncate the file to zero length, effectively removing what was already there), and write in the latest blog entry.

If you ever start feeling more comfortable with PHP, try putting this into a database. A database will make things quite a bit easier, quicker and with less overhead.
What do you mean open the file with mode a+ and mode w+????

Posted: Thu Jun 16, 2005 2:52 pm
by pickle
Look at the documentation for fopen(). Opening with different modes determines where the file pointer is placed by default, and what permissions you will have relative to the file.