Jump to content
Larry Ullman's Book Forums

Cofa

Members
  • Posts

    54
  • Joined

  • Last visited

Everything posted by Cofa

  1. 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>
  2. 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
  3. 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
  4. 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>
×
×
  • Create New...