Cofa Posted September 5, 2011 Share Posted September 5, 2011 Hello Larry, Thank you for your book that had helped me effectively changed my html website into php site - it's really clear and easy to learn from your book! I am now reaching the "Files and Directories" section (Chapter 11, page 297) and have gone through the chapter already. I was trying to write some code but was stuck here. I did some research on the web but still cannot make it. Could you please help? What I have been trying to do is to write a php script to do the following: 1) read the "First name" (or a unique "username") from the calling URL (using POST or GET), 2) then check the records in the text file to see if there is a match, 3) if there is a match, print the full name, company name and introduction of that record. I am enclosing a text file and my php script below for your review. The text file contains three lines (records), each line has the First name, Last name, Company name, Introduction. My script so far can only display all the elements of the file. I don't know how to pick only one record (line) from the file, then print elements of that record only. My question is: How could I identify a record in a text file (database), then print the selected values of that record only? I am not using a database program at this point. Thanks, Cofa Tsui New to the forum 9/4/2011 TEXT FILE 3 LINES ONLY "00_test_readtextfile_records.txt" ========================================= Peter##KWAN##First Street Company##This is my introduction. Please send me your comments. Thanks! Mary##THOMSON##Johnson High School##I am a grade 3 teacher here. I love facing the kids everyday! Billy##YU##Billy Travel Agency##We specialize in cruise ship tours in the Caribbean - Let me know when you'd like to have your vacation! PHP SCRIPT "00_test_readtextfile1.php" ========================================= <pre> <p>1ST PART - PRINT ALL LINES IN FILE ("config2" as array):</p> <?php $file = "00_test_readtextfile_records.txt"; $config = array(); $config1 = file_get_contents($file); $config2 = explode("\n",$config1); print_r($config2); ?> </pre> <br><br> <pre> <p>2ND PART - PRINT ELEMENTS OF INDIVIDUAL LINES</p> <?php $num; foreach ($config2 as $i) { $num++; } for ($i=0;$i<=$num-1;$i++) { $config3 = explode("##",$config2[$i]); print_r($config3); } ?> </pre> Link to comment Share on other sites More sharing options...
Larry Posted September 5, 2011 Share Posted September 5, 2011 Thanks for the nice words. It is appreciated. If it's any consolation, what you're trying to do is a little tricky. The most efficient way to do this would be to read the file line by line, using fgets() in a loop: while ($row = fgets($fp)) {... Then, within the while loop, you'd explode() $row and compare the record to the one you're looking for. You can combine those steps by using fgetscsv(), which reads in a row and turns it into an array: while ($row = fgetscsv($fp, 1000, '#')) { The 1000 value needs to be longer (in characters) than the longest line to read in the entire line at a time. The third argument is the delimiter (what breaks up items in a row), but it can only be a single character. Within the loop, you'd compare $row[0] to whatever to see if that's the correct record. If so, you'd do break to exit the loop and terminate looking through the file. So the basic premise is: while ($row = fgetscsv($fp, 1000, '#')) { if ($row[0]==$somevar) { break; } } Link to comment Share on other sites More sharing options...
Cofa Posted September 5, 2011 Author Share Posted September 5, 2011 Hello Larry, Thank you for the quick reply, and for the precise instructions in the step by step way (it needs some adjustments but may be it's just the way you want it to be). And it works out very well and meets my needs. Just one more question: If only one character can be used (the delimiter), what would be the best and safest characters to use in user-entered fields? (For example: # is not safe as user could enter house number as '#29', $ is not safe as one can enter amount as '$200', etc.) Cheers and again, thanks a lot! Cofa 9/5/2011 Link to comment Share on other sites More sharing options...
Cofa Posted September 5, 2011 Author Share Posted September 5, 2011 Oh Larry, Sorry but I forgot one thing. With the working code (adjusted from what you provided, as per my previous post), I notice that the first line (record) of my text file is unable to read. I can still bypass this problem by keeping the first line empty. But is this normal with a text file as a database? Thanks, Cofa 9/5/2011 Link to comment Share on other sites More sharing options...
Larry Posted September 6, 2011 Share Posted September 6, 2011 I could answer your question better if I saw the actual code you're using. Also, for future reference, it's generally nice to share the solution when you have one. As for the delimiter, the pipe (|) is a pretty good choice, or a tab, or a comma and you'd just need to quote all strings in case they have commas within them. Link to comment Share on other sites More sharing options...
Cofa Posted September 6, 2011 Author Share Posted September 6, 2011 Larry, Of course I am glad to share the working code; I was just not sure if it would be appropriate doing so. Below are the files I find working well, except for the "first record issue." Cheers! Cofa 9/5/2011 TEXT FILE "00_test_readtextfile_records.txt" The 4th record "Billy" has a very long "intro" field, just to see the exact number of characters it would print. ============================== 321#Lastname321#321 Company#Introduction 321 Peter#KWAN#First Street Company#This is my introduction. Please send me your comments. Thanks! Mary#THOMSON#Johnson High School#I am a grade 3 teacher here. I love facing the kids everyday! Billy#YU#Billy Travel Agency#We specialize in cruise ship tours in the Caribbean - Let me know when you'd like to have your vacation! 111How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. 222 How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. 333 How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. 3bb How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? 3cc The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. 3dd How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? 444 The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. How long can this line be? The quick brown fox jumps over the lazy dog. Peter2#KWAN2#First Street Company#This is my introduction. Please send me your comments. Thanks! PHP SCRIPT ============================== <?php /* Run file with matcher "00_test_readtextfile3.php?matcher=321" The above url will display empty elements */ ?> <div style="width:780px; margin:30px;"> <p>1ST PART - PRINT ALL LINES IN FILE ("config2" as array):</p> <?php $matcher = $_REQUEST['matcher']; $file = "00_test_readtextfile_records.txt"; $fp = fopen($file, r); while ($row = fgets($fp)) { while ($row = fgetcsv($fp, 2048, '#')) { if ($row[0] == $matcher) { $firstname = $row[0]; $lastname = $row[1]; $companyname = $row[2]; $intro = $row[3]; break; } } } fclose($fp); ?> <br><br> <p>2ND PART - PRINT ELEMENTS OF MATCHED RECORD</p> <?php echo "<br>First name = " . $firstname; echo "<br>Last name = " . $lastname; echo "<br>Company name = " . $companyname; echo "<br>Introduction = " . $intro; ?> </div> Link to comment Share on other sites More sharing options...
Larry Posted September 7, 2011 Share Posted September 7, 2011 It's always okay to share what you've learned. The problem is you have one while loop that calls fgets() and then an internal one that calls fgetscsv(). Apparently this wasn't clear from what I was saying. What I was saying is that you could use fgets() to go line by line, and then explode() within that loop, OR you could skip to the chase and use fgetscsv() in a while loop. So drop the fgets() while loop (which is reading in the first line, but not doing anything with it) and you should be fine. Link to comment Share on other sites More sharing options...
Cofa Posted September 8, 2011 Author Share Posted September 8, 2011 Hi Larry, Yes, it works perfectly after removing the "fgets() while loop" from my code. But I just don't understand (may be I miss something). With my not-so-perfect code: while ($row = fgets($fp)) { while ($row = fgetcsv($fp, 2048, '#')) { if ($row[0] == $matcher) { How come the 2nd (or internal) loop could not read the first line of the text file from the first while loop? Or how come it (the 2nd loop) could not read the first line of the text file if it (the 2nd loop) works on its own? Hope this won't take up too much of your time Cofa 9/7/2011 Link to comment Share on other sites More sharing options...
abigail Posted September 8, 2011 Share Posted September 8, 2011 I think it's because when the first while reads the first line, the $fp (file pointer) is advanced to (point at) the second line. 1 Link to comment Share on other sites More sharing options...
Larry Posted September 9, 2011 Share Posted September 9, 2011 Yes, abigail's exactly right (thanks!). The second loop reads the second row from the file and assigns it to $row. You could have used $row within the first while loop but before the second to access that first line. Of course, you actually don't want two loops anyway. Also understand that with that syntax, the first while loop is only executed once, because the inner while loop reads the rest of the file. Once that inner loop is done, the outer loop gets control again and has nothing left to read from the file. Link to comment Share on other sites More sharing options...
Cofa Posted September 9, 2011 Author Share Posted September 9, 2011 Thanks to Larry and abigail for the help. I now understood the logic. By the way, also thanks to Larry for the suggestions on the delimiter. Have a nice weekend! Cofa 9/9/2011 Link to comment Share on other sites More sharing options...
Recommended Posts