John_S Posted March 3, 2009 Share Posted March 3, 2009 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 More sharing options...
rhodesa Posted March 3, 2009 Share Posted March 3, 2009 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); } ?> Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775687 Share on other sites More sharing options...
cooldude832 Posted March 3, 2009 Share Posted March 3, 2009 Any reason you aren't using mysql? Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775689 Share on other sites More sharing options...
John_S Posted March 3, 2009 Author Share Posted March 3, 2009 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()? Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775693 Share on other sites More sharing options...
cooldude832 Posted March 3, 2009 Share Posted March 3, 2009 lol it is databasing the data in a way so Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775695 Share on other sites More sharing options...
rhodesa Posted March 3, 2009 Share Posted March 3, 2009 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]) Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775699 Share on other sites More sharing options...
John_S Posted March 3, 2009 Author Share Posted March 3, 2009 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 ), 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 Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775704 Share on other sites More sharing options...
cooldude832 Posted March 3, 2009 Share Posted March 3, 2009 Well you can be like MySQL and do a supporting file containing indexes that link to the "bit" location of each entry in the field and then when u go to read the file read from that start bit to its end bit and you have said entry. Link to comment https://forums.phpfreaks.com/topic/147764-solved-saving-articles-in-a-flat-file/#findComment-775815 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.