PHP blog scrip

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

PHP blog scrip

Post 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
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ericburnard
Forum Contributor
Posts: 104
Joined: Wed Jun 15, 2005 5:11 pm
Location: Chesterfield, UK

Post 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+????
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply