Jump to content
Larry Ullman's Book Forums

Reading From .Txt Help


Recommended Posts

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

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

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

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

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

 Share

×
×
  • Create New...