AeriesIII Posted November 16, 2010 Share Posted November 16, 2010 I currently am working on a project where I code a "simple" telephone directory. There are three main tasks that it needs to do: 1. Directory.php(index page) has a "First Name" and "Last Name" field and a search button. When a name is searched from the directory.txt file, it displays First Name, Last Name, Address, City, State, Zip and phone in findinfo.php in designated text boxes...first name, last name, etc. 2. From the findinfo.php, like previously stated, the users information is listed in the appropriate text boxes. From there, there is an update button that will overwrite the user's information to directory.txt if that button is selected. It will then say the write was sucessful. 3. (completed this step) From the index page, there is a link that will take you to addnew.php where you enter First Name, Last Name, Address, City, State, Zip and phone in a web form and write it to directory.txt. This is the php code for the third step: <?php $newentryfile = fopen("directory.txt", "a+"); $firstname = $_POST['fname']; $lastname = $_POST['lname']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $phone = $_POST['phone']; $newentry = "$firstname $lastname\n\r $address\n\r $city, $state $zip\n\r $phone\n\r"; if (flock($newentryfile, LOCK_EX)) { if (fwrite($newentryfile, $newentry) > 0) echo "<p>" . stripslashes($firstname) . " " . stripslashes($lastname) . " has been added to the directory.</p>"; else echo "<p>Registration error!</p>"; flock($newentryfile, LOCK_UN); } else echo "<p>Cannot write to the file. Please try again later</p>"; fclose($newentryfile); if(empty($firstname) || empty($lastname) || empty($address) || empty($city) || empty ($state) || empty($zip) || empty($phone)) { echo "<p>Please go back and fill out all fields.</p>"; } ?> So to sum it all up, what would be my best approach? I am totally stumped and not sure which function to use. Should I work my way from step 1 to step 2? I see it as when I do the search for the name from directory.php, it takes me to findinfo.php, listing the users information in the text boxes. From there, if I needed to, having the user's information already listed I could hit the update button to overwrite the new information to directory.txt. Doing the update when then tell me that the write was successful. I have literally been scouring the internet for hours. What would be the best function to do this? I hope I was clear enough. Please help me out and thank you for your time. Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 I think you need to rethink how you are storing the information in the text file. You are basically build a "database" of storts. So you should be storing the raw data not trying to store formatted data (i.e. the line breaks, commas, etc.): $newentry = "$firstname $lastname\n\r $address\n\r $city, $state $zip\n\r $phone\n\r"; I would suggest one of two approaches. 1. Store the data tab delimited: $newentry = "$firstname\t$lastname\t$address\t$city\t$state\t$zip\t$phone\n\r"; You could them read the file using file() which will parse teh file into an array with each line of the file an element of the array. You would then use the position of each record as it's ID. Then when you need to update a record, just update the record at the index of the ID. New records would always be added at the bottom 2. Work with the data as a multi-dimensional array and serialize it when writing it and then unserialize when reading it. This would work pretty similar to #1, but with this approach you could give each piece of data a label. This would make the logic a little easier to work with in my opinion. Quote Link to comment Share on other sites More sharing options...
AeriesIII Posted November 17, 2010 Author Share Posted November 17, 2010 Ok, great! Thanks a lot for pointing out on how to properly store the data tab delimiters. You say #2 makes more sense but #1 seems like it will be easier to figure out. Does method #1 change if I need the data to be over written instead of just adding the new records at the bottom of the txt file? Quote Link to comment Share on other sites More sharing options...
laffin Posted November 17, 2010 Share Posted November 17, 2010 Well, Method 1 provides for versatility, and working with CSV type files, u can use php functions fgetcsv[/url — Gets line from file pointer and parse for CSV fields fputcsv — Format line as CSV and write to file pointer The nice thing about CSV, is that u can import/export the data from other sources (i.e.: maitaining the database in a spreadsheet) Option 2 would be significantly faster, however u lose the flexibility of a CSV file. (U maintain data from web page) both approaches work well for small databases Quote Link to comment Share on other sites More sharing options...
Psycho Posted November 17, 2010 Share Posted November 17, 2010 You say #2 makes more sense but #1 seems like it will be easier to figure out. Does method #1 change if I need the data to be over written instead of just adding the new records at the bottom of the txt file? I see advantages and disadvantages to both methods. Personally, I like method #2 because you can use named indicies for the inner dimensions of the array to give your data more strucure array( 0 => array( 'fname' => "dave", 'lname' => "Roberts", 'phone' => "123-456-7890" ) 1 => array( 'fname' => "Jane", 'lname' => "Doe", 'phone' => "111-222-3333" ) ) With option #1 you could only identify each piece of data by it's position based on the tab delimiters The process to overwrite vs. add should be very similar for both. If you are overwriting, read the text file into an array [either using file() or unserialize()] then change the value of the selected record based upon the index: $dataArray[$indexToUpdate] = $newRecord; New record would either be a tab delimited string or an array based upon option #1 or #2. As for new records, you could add them to the end or you could save the records in some logical sorting order. To do that would be easiest if the data is in a multi-dimensional array. That would require an extra step with option #1. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.