deadbeat Posted September 7, 2011 Share Posted September 7, 2011 Hi, Firstly, I just got the book, was having some troubles in a class I'm taking at uni and this has helped me heaps so far. Thankyou! So what I am trying to acheive, is to be able to read from a .txt file and print info from one line if what was put in the text input form is in that line, if not print an error. So I already have a page that is a form that writes data to a .txt file. It puts each input from the form on the same line, spaced by a tab and then a new form completion will be the same but on a new line. ie. B0001 Medina Great Ocean Road 01 01 2011 2 Yes B0002 Stamford Penguin Parade 01 01 2011 3 No So I want to be able to have another page with a text input box where i can search for "B0001" and then it will print the results like so: Booking Code: B0001 Hotel Pick Up: Medina Tour: Great Ocean Road Tour Date: 01/01/2011 Number of Persons: 2 With Lunch: Yes But if I where to input say "B1" into the text input it would give an error message. I can do all of the above fine at the moment. But I can't get it to search past the first line of the .txt file. This is the code I have at the moment. $fp = fopen("../data/bookings.txt", "r"); $details = fgetcsv($fp, 1000, "\t"); if (($details [0]) == $_POST['search']){ print "<p>Booking Code: {$details [0]}</p>"; print "<p>Hotel Pick Up: {$details [1]}</p>"; print "<p>Tour: {$details [2]}</p>"; print "<p>Tour Date: {$details [3]}/{$details [4]}/{$details [5]}</p>"; print "<p>Number of Persons: {$details [6]}</p>"; print "<p>With Lunch: {$details [7]}</p>"; } else { print '<p class="error">Booking code not found!</p>'; } I'm not sure how to approach the next step to get what I want. Any help would be greatly appreciated!! Link to comment Share on other sites More sharing options...
Larry Posted September 7, 2011 Share Posted September 7, 2011 You need to use a loop to access every line. Another poster is trying a similar thing and what I've written there may be useful: http://www.larryullman.com/forums/index.php?/topic/589-how-to-identify-a-record-in-text-file-then-print-selected-values-of-that-record/page__fromsearch__1 Link to comment Share on other sites More sharing options...
deadbeat Posted September 8, 2011 Author Share Posted September 8, 2011 Thanks Larry!! This is what I came up with, for anyone who might be interested. $fp = fopen("../data/bookings.txt", "r"); $search = $_POST['search']; $match = FALSE; while ($details = fgetcsv($fp, 1000, "\t")) { if ($details[0] == $search){ $match = TRUE; break; } } fclose($fp); $code = $details[0]; $hotel = $details[1]; $tour = $details[2]; $day = $details[3]; $month = $details[4]; $year = $details[5]; $persons = $details[6]; $lunch = $details[7]; if ($match) { print "<p>Booking Code: $code</p>"; print "<p>Hotel Pick Up: $hotel</p>"; print "<p>Tour: $tour</p>"; print "<p>Tour Date: $day/$month/$year</p>"; print "<p>Number of Persons: $persons</p>"; print "<p>With Lunch: $lunch</p>"; } else { print '<p class="error">Booking code not found!</p>'; } Works a treat! Link to comment Share on other sites More sharing options...
Larry Posted September 8, 2011 Share Posted September 8, 2011 Glad it works and thanks for sharing! Link to comment Share on other sites More sharing options...
HartleySan Posted September 9, 2011 Share Posted September 9, 2011 While slightly more advanced, you could also use the PHP cURL extension. It offers more options and is more secure (which means you can more readily use it to access files outside your server as well). Link to comment Share on other sites More sharing options...
Larry Posted September 9, 2011 Share Posted September 9, 2011 HartleySan, would you really recommend using cURL to read through a text file on the same server as the PHP script? And in what ways is cURL more secure than using fopen(), fgets(), and fclose() for reading local files? Link to comment Share on other sites More sharing options...
HartleySan Posted September 9, 2011 Share Posted September 9, 2011 G'damn, you're all over people today, Larry! You are right, though. I have never used cURL for local files, nor would I. The wording of my last post was less than ideal. What I was trying to throw out there is that there is this thing called cURL, and it's great for accessing external files. Link to comment Share on other sites More sharing options...
Larry Posted September 9, 2011 Share Posted September 9, 2011 I see. Yes, cURL is quite excellent, but wouldn't be used here. Gotta try to keep these forums as accurate as possible; already too much misinformation online! Link to comment Share on other sites More sharing options...
HartleySan Posted September 9, 2011 Share Posted September 9, 2011 All right, all right. Yes, you're right. Just, you reaction was a bit more than I expected. Link to comment Share on other sites More sharing options...
Larry Posted September 9, 2011 Share Posted September 9, 2011 Well, my reaction was based upon the theory that you knew something I did not (in this case, specifically about cURL). If it is true that there's an argument for using cURL for local files and if it is true that cURL is more secure, that's information I ought to know. I learn from these forums, too, so my questioning was in that regard. And, as already said, to ensure technical accuracy for the sake of the original poster and those that might read this thread. In terms of the strength of my reaction, I'll point out that I did not write anything along the lines of "HartleySan, I don't think that answer is going to help too much", which is the kind of statement you're prone to making when you disagree with a reply's approach. Link to comment Share on other sites More sharing options...
HartleySan Posted September 9, 2011 Share Posted September 9, 2011 Hehe! I wouldn't say prone, but yes, I did make that statement in regards to an answer of yours the other day. But let's be honest, your answer could have been a lot more thorough, which is why I followed it up with my own answer. I mean, I don't know the topic poster, but I could tell by the nature of his question that your answer was gonna fly right over his head. Link to comment Share on other sites More sharing options...
Recommended Posts