Page 1 of 1

Flat File Tutorial

Posted: Thu May 29, 2003 8:00 pm
by WizyWyg
Is there any site that has a comprehensive tutorial for PHP working with Flat files.

All I have come across are "built" applications for use with flat files.

Just looking for a step-by-step tutorial on how to make a basic form write to a .txt file.

And then how to display contents from that file.
Or limit what to show from that file.
Etc

does anyone have links to more detailed tutorials?

Posted: Thu May 29, 2003 11:11 pm
by phice

Posted: Fri May 30, 2003 8:03 pm
by WizyWyg
thanks ,but that is not comprehensive.

Comprehensive means that they show step by step and explain how to go from form, to txt, retreieve and display.

Posted: Mon Jun 02, 2003 3:03 pm
by WizyWyg
Back to this:

I have so far this:

Code: Select all

<body>
Test<br>
<p>
Orders</p>
<?
$fileread = file("orders.txt");
foreach($fileread as $key => $val) { 
	$data[$key] = explode("|", $val); } 
	
$totallines = count($fileread); 
for($i = 0; $i < $totallines; $i++) 
	{ 
	//$tel = count($fileread); 
	echo '<p>';
	echo 'First Name: '.$data[$i][2].'<br>';
	echo 'Last Name: '.$data[$i][3].'<br>'; 
	echo 'Title: '.$data[$i][4].'<br>';
	echo 'Phone: '.$data[$i][5].'<br>';
	echo 'Ext.: '.$data[$i][6].'<br>';
	echo 'Email: <a href=mailto:'.$data[$i][7].'>'.$data[$i][7].'</a><br>';
	echo 'Img: <img src=emps/'.$data[$i][8].'><br>';
	}
?>
</body>

and my text file has info like this:

Code: Select all

1|7|Fnamea|Lnamea|Jobtitlea|(888) 888-888|888|888@nospamme.com|image1.jpg| | |3| |0
2|6|Fnameb|Lnameb|Jobtitleb|(888) 888-888|888|888@nospamme.com|image2.jpg| | |0| |1
3|9|Fnamec|Lnamec|Jobtitlec|(888) 888-888|888|888@nospamme.com|image3.jpg| | |0| |0
4|1|Fnamed|Lnamed|Jobtitled|(888) 888-888|888|888@nospamme.com|image4.jpg| | |0| |1

How can I make it , if i only want to show those who's last column is 1 ?

Or the second column is 7?

Posted: Mon Jun 02, 2003 3:15 pm
by Anthron
Ok, I've done a lot of work with flat-file databases.

Basically, here's what you want. Say you have a file with a bunch of seperated values.

Code: Select all

1|Anthron|anthron@hisemailaddress.com|1
2|WizyWyg|wizywyg@hisemail.com|0
3|Someoneelse|someoneelse.yahoo.com|1
And that is saved in file db.txt

Code: Select all

$raw = file('db.txt');
foreach ($raw as $line)
&#123;
$parts = explode('|', $line);
if ($parts&#1111;3] == 1)
&#123;
// Manipulate the line of code
&#125;
&#125;
That's basically the best way to do it. If you're really bored, you can make a class which you can use to manipulate your flat-files. I really do believe this is the best way to go about things. Once Invision FlatBoard is released, I think you'll be able to borrow/look at the class used to work with flat-file dbs and make some pretty powerful flat-file scripts. :)

And if i'm not mistaken, there are a few other flat-file boards in development now who have released classes for working with flat-file dbs. I would take a look at those.

Posted: Tue Jun 03, 2003 2:35 pm
by WizyWyg
Im not familiar with making classes, so I rather stick with just working out the coding and functions. What if I wanted to order the results (ie by Last Name?)

Posted: Thu Jun 05, 2003 2:14 pm
by WizyWyg
Anyone? Links to sites that have comprehensive use of flat files? how to order results? etc

playing around

Posted: Thu Jun 05, 2003 5:01 pm
by phpScott
I think what Anthron said is the basics for reading info from a file.
Once the information is read you have to store is some where maybe in arrays and then you would have to do array manipulation to order the info in what ever order you wanted.

Learn to love and understand http://www.php.net. They quite often have very good user comments on how to use the language.
Do a function search for files and arrays and you will get scads of info

If you are asking for some one to give up their code for you I think that you might be disappointed as we try to answer specific questions about a problem.

Take what Anthron has and play with it a little to see what you can come up with.

phpScott

Posted: Fri Jun 06, 2003 7:02 am
by Tubbietoeter
to order results you can either use the php function multisort (see php.net for info on the function plus examples) or sort the file your reading before reading it (the unix commando sort can sort by columns, see "man sort" for info).

Re: playing around

Posted: Fri Jun 06, 2003 3:18 pm
by WizyWyg
phpScott wrote:I think what Anthron said is the basics for reading info from a file.
Once the information is read you have to store is some where maybe in arrays and then you would have to do array manipulation to order the info in what ever order you wanted.

Learn to love and understand http://www.php.net. They quite often have very good user comments on how to use the language.
Do a function search for files and arrays and you will get scads of info

If you are asking for some one to give up their code for you I think that you might be disappointed as we try to answer specific questions about a problem.

Take what Anthron has and play with it a little to see what you can come up with.

phpScott
can you explain how to create classes? How they help? What can be achieved by using them? how do they make things better? etc etc?

These are not explained in the manual.

Also, the comments on the "flat file" part of the php manual do not lend much info, which is why I am asking for help on the boards.

Posted: Fri Jun 06, 2003 3:20 pm
by WizyWyg
Tubbietoeter wrote:to order results you can either use the php function multisort (see php.net for info on the function plus examples) or sort the file your reading before reading it (the unix commando sort can sort by columns, see "man sort" for info).
How would go about doing the latter? Is it in the php manual (i searched, nothing on man sort or mansort )