Jump to content

[SOLVED] Saving articles in a flat file.


John_S

Recommended Posts

Hello there everybody,

 

I am currently writing a small news script which I would like to save all articles into a flat file separating :

title, id, date, author,category, content

 

by | sign in the flat file, and then extract these entries to display them. I tried all I could but I don't get how to write that :S

 

Could somebody help please?

Thanks a lot in advance!

Link to comment
https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/
Share on other sites

instead of separating it with |, i would just use var_export(). so build an array of the info:

<?php
  $news = array(
    array(
      'title' => 'This is a title',
      'id' => 123,
      'date' => '2009-03-03',
      //etc
    ),
    array(
      'title' => 'This is another title',
      'id' => 456,
      'date' => '2009-03-01',
      //etc
    ),
  );
?>

 

then, to write it to a file:

<?php
  file_put_contents('filename.php','<?php $news = '.var_export($news,1).'; ?>'); //Before the semi-colon, there is a single quote...it's not printing it for some reason
?>

 

then, to read it back, just include the file:

<?php
include('filename.php');
foreach($news as $article){
  print_r($article);
}
?>

  Quote

Any reason you aren't using mysql?

 

Hello there!

 

Because I am not allowed, it's a part of a school project, and one of the conditions is: No MySQL or any other database software, only flat files. :(

 

@rhodesa

 

Thanks a lot for your help. But then how can I search for IDs for example? invert each array and check with in_array()?

  Quote

either loop over the $news array and build an array of just IDs, or use the ID as the array key, then you can just use

isset($news[$id])

 

Thanks! A lot :)

 

  Quote

lol it is databasing the data in a way so :)

 

True, but it is allowed (well as far as I know :P), a friend of mine uses a specific folder with a txt file for each entry, but personally I think that would be a bit too messy

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.