Keep from repeating entry in directory

05-08-07, 10:31 PM
|
|
Wannabe Coder
|
|
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
Keep from repeating entry in directory
I am really getting the hang of this FLAT file stuff. It is a great excercise in array manipulation.
Any way- I have this script:
It basically creates three text files.
1- contains the First Name, Last Name, Address and Phone Number
2- contains a UID, First Name, Last Name, Address and Phone Number
3- simply lists the UID into a Directory text file.
If I desire to add info to a particular file using the UID along with other info- How can I keep it from logging the UID twice in the directory?
After opening the directory.txt - how can I check for the UID and stop the script from listing it again?
Thanks in advance.
Andy
Last edited by Christian; 05-08-07 at 10:55 PM.
Reason: Please use [PHP] tags when posting PHP code.
|

05-09-07, 10:21 AM
|
|
Newbie Coder
|
|
Join Date: Mar 2007
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hey
you can use rand() function to create an unique id for each group of files you are creating....
but my suggestion is to use a database is simpler and more flexible....
|

05-09-07, 11:49 AM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Ok, I understand what the first file is for that stores their personal information, and I understand what the directory file is for ( keeps track of the individual files that are created).
But what I don't understand is what all the individual files are for that stores duplicate personal information.
Could you please explain what it is that your program is suppose to do?
__________________
Jerry Broughton
|

05-09-07, 01:34 PM
|
|
Wannabe Coder
|
|
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
job-
Well- I am trying to master flat files. Because manipulation of a flat file is difficult to pull one set of data from the array- I find it easier that upon the input (from the form) to have a script that creates several instances of the gathered info so that I can retrieve it in several different manners.
As exampled above-
A) it first logs all the input into one txt file.
B)Then it logs the info into a seperate txt file with a unique id attached to the same set of input.
C) it creates a log of all files created with the UID
Therefore- if two people had the same name- I could query by UID. I have several scripts that obviously display the data in different manners.
My problem is that the current script will duplicate UID's in the UID Directory. I would like to have it first check the UID directory and if the number is present- do not log it again. In this manner I could still add info to a file that is already present- but would not needlessly re-list the UID in the Directory.
Does this make better sense?
Andy
|

05-09-07, 03:49 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
No I don't understand yet. If different people with same names are assigned unique ID's then why do you need two or more different files? Why don't you just store their ID in place of the filename, in one file. Exploding each line of the file into a multi-dimensional array is a lot easier than you think. And working with a multi-dimensional array to retrieve, modify or add data is fairly easy also.
Explode() breaks-up a line of text and stores it in an array. And Implode() converts an array back into a line of text.
You can explode each line of text from your file into a multi-dimensional array as individual rows of data. Then it is quite simple to retrieve individual rows, columns or pieces of data from the array, make comparisons or changes to the data at will and then implode each row back into your file if you need to save any changes.
I think if you spend some time learning about arrays, you will find it will make your task much easier.
And like tomasgtz said, working with a MySQL database is even easier.
But if you must use files then I am more than willing to help you learn how to use arrays, if you have the time.
__________________
Jerry Broughton
Last edited by job0107; 05-09-07 at 03:54 PM.
|

05-09-07, 04:08 PM
|
|
Wannabe Coder
|
|
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I absolutely have the time and desire to learn about arrays. There is not a whole lot of examples out there on how to work with and manipulate arrays (with examples that are easy to understand). Therefore- if you could point me in the right direction it would be most appreciated.
No knowing how to work with multi-dimensional arrays- can you see where I was attempting to use what knowledge I do have to accomplish the task at hand?
Also- there is limited info out there with respect to flat files (most likely as it is outdated). I will eventually change to MySQL but for now, I think learning flat file manipulation willl help me greatly understand php and arrays.
So- any help your offering- I would be most thankful.
Andy
|

05-09-07, 04:32 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Yes, I can see what you were trying to do. But using arrays is far simpler. It's just like having a table in a database. Only your table is going to be loaded into a table like array (multi-dimensional array).
__________________
Jerry Broughton
Last edited by job0107; 05-09-07 at 04:37 PM.
|

05-09-07, 07:46 PM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
This little program will take your existing file if you have one and load it into a table shaped array ( rows / columns ).
Then it gets the POST variables and adds them to the end of the array and appends them to the end of the file ( if the file doesn't exist yet it will create it. ).
I am using $record_count as an ID because it is always unique.
Now you can do anything with the data in the array, make comparisons or changes to the data and save it back to the file again if you wish.
This program is just beginning. If you want to try to program the rest of the logic for this program, go for it. Otherwise I will need some help from you. You will need to describe to me in detail exactly what you want this program to do.
!!! Important information !!! : Do not run this program without data or it will create a BLANK record.
__________________
Jerry Broughton
Last edited by job0107; 05-09-07 at 08:14 PM.
|

05-09-07, 09:07 PM
|
|
Wannabe Coder
|
|
Join Date: Jun 2006
Posts: 239
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Jerry-
Thanks. That works very nicely. If you look here you will see the form I created. Your script creates this:
Quote:
0|Keith|Andersen|1374 Wayburn|555-1234
1|Mark|Thompson|444 Beacon Pointe|333-3333
2|Alfred|Fields|333 My Way|999-9999
|
as the flat file. If you click here you will see how I display it in an I-frame using this code;
Am I doing it correct so far? I do not understand how your code puts the array into a table row/column format?
Now- Is it possible to query this file and simply pull up a single persons info into a display field? Additionally- lets say Alfred changes his address. Is it possible to change the address in a flat file such as this?
Again- much thanks for the time and lesson.
Andy
|

05-10-07, 03:18 AM
|
 |
Community Liaison
|
|
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
|
|
Yes it's possible to use your method to query the file. But you will be making a lot of calls to the file and it will slow your program to a crawl if you have a lot of traffic.
It is ten times faster and easier if you load the file into a multi-dimensional array or a group of arrays.
By using the list function as you are doing you can simulate a multi-dimensional array by loading the data into array variables instead of single value variables.
But then your coding becomes more cumbersome having to deal with multiple arrays instead of just one multi-dimensional array.
For example:
Is the same as:
Then:
A multi-dimensional array is very easy to understand if you look at it this way.
$hold[ arg1][ arg2]
where arg1 equals the row and arg2 equals the column.
So, if you increment arg1 each time you read in a line from the file, it will create another row.
When you are finished reading in all the lines from the file, $hold will contain all the data configured as a table with rows and columns.
Now you won't have to open the file again. All the data in the file will be stored in the array $hold. Unless you plan to write the data back to the file.
Now you can manipulate the data in the $hold array any way you want by addressing it by row and column.
One final note:
There would be one advantage to use this method:
It would allow you to use array_multisort() to sort your data by column.
__________________
Jerry Broughton
Last edited by job0107; 05-10-07 at 03:44 AM.
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|