Page 1 of 1

Flat-File - Which is Faster?

Posted: Sun Nov 24, 2002 10:42 am
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!

Posted: Sun Nov 24, 2002 10:59 am
by hob_goblin
It might free up some RAM, but really, you won't ever notice the difference.

others?

Posted: Mon Nov 25, 2002 4:13 am
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!

Posted: Mon Nov 25, 2002 4:29 am
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

interesting

Posted: Mon Nov 25, 2002 6:21 am
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.