Text file database 
Author Message
 Text file database

Kenn:


Quote:
> I wonder if anyone could point in the direction of a good tutorial for
> using a delimited text file database with PHP?

> I do not have a real database available to me, but I don't think that
> will be a problem because my needs are extremely simple. This text file
> will be read-only, with probably no more than 30 or so lines. Each line
> will have six fields, delimited by a pipe ("|") character.

> There will be a single parameter passed in the URL indicating which line
> is needed. For example, foo.php?bar=1 means line 1 should be used. I will
> then need to display the contents of that line.

Should be pretty straight forward:
Let your data be stored in $data[];

Open your file: fopen()
  http://www.*-*-*.com/

set your index counter to 0;
loop while not eof

reading one line at a time into a variable:  fgets()
http://www.*-*-*.com/

Strip the newline from it:
http://www.*-*-*.com/

make an array out of it, breaking on the seperator character
http://www.*-*-*.com/

add this array to your array of lines.
$data[$index++] = your line array

when done, close the file:      fclose()
http://www.*-*-*.com/

Remember though, PHP arrays start at a 0,
so your first line/first data is $data[0][0];

HTH

--
Alan Walkington
Technical Staff
United Defense, San Jose

Quote:
> I know how to open the file using fopen() and fread(), but I do not know
> how to anything with the data. I'm guessing I'll load the data into a
> multidimensional array. For example, $line[1][1] would refer to the first
> field of the first line. Likewise, $line[15][2] is the second field of
> line 15. I could then display the data by printing $line[1][1] or
> whatever where needed. Is this correct?

> I've searched the web a bit but haven't come up with anything that really
> shows me how to do this. Any help would be greatly appreciated. Thanks.

> --
> Kenn
> s/example.gov/mindspring.com to e-mail



Mon, 01 Aug 2005 02:19:40 GMT  
 Text file database
see below


Quote:
> Kenn:



> > I wonder if anyone could point in the direction of a good tutorial for
> > using a delimited text file database with PHP?

> > I do not have a real database available to me, but I don't think that
> > will be a problem because my needs are extremely simple. This text file
> > will be read-only, with probably no more than 30 or so lines. Each line
> > will have six fields, delimited by a pipe ("|") character.

> > There will be a single parameter passed in the URL indicating which line
> > is needed. For example, foo.php?bar=1 means line 1 should be used. I
will
> > then need to display the contents of that line.

> Should be pretty straight forward:
> Let your data be stored in $data[];

> Open your file: fopen()
>  http://www.php.net/manual/en/function.fopen.php

> set your index counter to 0;
> loop while not eof

> reading one line at a time into a variable:  fgets()
> http://www.php.net/manual/en/function.fgets.php

> Strip the newline from it:
> http://www.php.net/manual/en/function.trim.php

> make an array out of it, breaking on the seperator character
> http://www.php.net/manual/en/function.explode.php

------------------------------------------------------
check the (no subject) post by 'galway boy';
I didn't know about fgetcsv()
http://www.php.net/manual/en/function.fgetcsv.php
(that's 'comma seperated values')
It reads and parses a delimited line in one step!

------------------------------------------------------

- Show quoted text -

Quote:
> add this array to your array of lines.
> $data[$index++] = your line array

> when done, close the file:      fclose()
> http://www.php.net/manual/en/function.fclose.php

> Remember though, PHP arrays start at a 0,
> so your first line/first data is $data[0][0];



Mon, 01 Aug 2005 12:03:41 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Text File Database

2. Reading plain text file database tables

3. Export Clarion database to Excel or text file?

4. How to transfer Clarion database to text file

5. Import text file into access database by active x

6. Deleting text in text file

7. Email text body from text file?

8. How do I read lines of Text from Text File and add them to Array

9. Write 2D Text Array in a text file

10. Inserting a string of text into a text file

11. parsing a long text file for specific text

12. text file and text box script

 

 
Powered by phpBB® Forum Software