Flat-File - Which is Faster?

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
Dr. PHP
Forum Newbie
Posts: 11
Joined: Sun Nov 24, 2002 8:46 am

Flat-File - Which is Faster?

Post by Dr. PHP »

Well....here goes. Everynight at midnight, my script will run to generate a new flat txt file that looks something like this:

Code: Select all

Story Name||Story Author||Story Publisher||Story Date||Story Priority
It will have 5 new lines each day (always 5 lines total, just a different 5 lines each day). I will be wanting to produce these to HTML. My original intent was to do this (skip this if it seems too long)

Code: Select all

<?php

$nameArray = array("title","author","paper","date","priority");

$filePointer = fopen("stories.txt","r");

$fileArray = array();
$titleArray = array();
$authorArray = array();
$paperArray = array();
$dateArray = array();
$priorityArray = array();

$arrayCounter = 0;
$nameCounter = 0;

while ($nameCounter<=4)
&#123;

	$line = fgets($filePointer,4096);
	
	while ($arrayCounter<=4)
	&#123;
		$setArray = $nameArray&#1111;$arrayCounter]."Array";
		$&#123;$setArray&#125;&#1111;$nameCounter] = // ?? //
		$arrayCounter++;
	&#125;

	$arrayCounter = 0;
	$nameCounter++;
&#125;


fclose($filePointer);
?>
I was going to have 5 different arrays, each with 5 elements. The script was going to run through the file and store all variables, then print them to HTML (also, if anyone can help with the above script I really don't know how to store each section of the file, then skip the || delimiter and move to the next variable).

But, I began to think....would it just be easier and run faster if the script never stored the file variables as actual variable, it just used one temporary variable, read from the file, the printed the HTML, then did it all over again?

Can anyone tell me which was would be faster and more effective, and possibly section a starting point on how to seperate the file from the delimiters? Thanks!
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

It might free up some RAM, but really, you won't ever notice the difference.
Dr. PHP
Forum Newbie
Posts: 11
Joined: Sun Nov 24, 2002 8:46 am

others?

Post by Dr. PHP »

Anyone else have any suggestions on a which way is more acceptable or productive to accomplish this or can give me an idea of a good way to seperate a file with || delimiters into an array or seperate strings?

Thanks!
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can read a file into an array using file() which will separate each line into an array element. You can then use explode() to separate each line into multiple array elements. You know how many new lines you are going to have so you could adapt something like:

Code: Select all

&lt;?php
$lineinfo = array();
$fileinfo = file('myfile.txt');
$num_lines = sizeof($fileinfo);
for ($i = ($num_lines-5); $i &lt; $num_lines; $i++) {
	$lineinfo&#1111;] = explode('||', $fileinfo&#1111;$i]);
}
echo '&lt;pre&gt;';
print_r($lineinfo);
echo '&lt;/pre&gt;';
?&gt;
Mac
Dr. PHP
Forum Newbie
Posts: 11
Joined: Sun Nov 24, 2002 8:46 am

interesting

Post by Dr. PHP »

Interesting, I've never heard of the file() function before. Do you think I should do it this way or should I just read each line temporarily and publish the output HTML line by line instead of storing all of the variables then publishing the HTML? Thanks.
Post Reply