Flat File Tutorial

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
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Flat File Tutorial

Post 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?
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Image Image
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post 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.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post 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?
Anthron
Forum Newbie
Posts: 8
Joined: Mon May 20, 2002 6:25 pm
Location: usa
Contact:

Post 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.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post 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?)
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post by WizyWyg »

Anyone? Links to sites that have comprehensive use of flat files? how to order results? etc
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

playing around

Post 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
Tubbietoeter
Forum Contributor
Posts: 149
Joined: Fri Mar 14, 2003 2:41 am
Location: Germany

Post 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).
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Re: playing around

Post 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.
WizyWyg
Forum Commoner
Posts: 92
Joined: Tue Aug 06, 2002 7:20 pm

Post 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 )
Post Reply