RE: [PHP-DB] Using text files instead of a DB

From: Date: Wed, 14 Mar 2001 22:52:47 +0000
Subject: RE: [PHP-DB] Using text files instead of a DB
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-7494@lists.php.net to get a copy of this message
This following script might help. It assumes you are reading in a file with the following format: 0;this is line zero 1;this is a line one 2;this is a line two 3;this is a line three 4;this is a line four 5;this is a line five <?php // get a the file into an array, you wouldn't want to do this for a very big file // I tried it with a file 4000+ lines and it seemed to work just fine // if you know the file will be big then we would read only a limited number of lines // at a time using fopen() and fgets() $fcontents = file ('products.txt'); // I provide the following variable ($method) in the url // For example: // http://www.yoursite.com/index.phtml?method=id&product_id=123 if($method == "id"){ // Loop through the file trying to match the begining // of a line with the desired id while (list ($line_num, $line) = each ($fcontents)) { if(ereg("^".$keyword, $line)){ echo "<b>Line $line_num:</b> " . htmlspecialchars ($line) . "<br>\n"; } } // keywords are seperated by | (pipe) // For example: // http://www.yoursite.com/index.phtml?method=search&keyword=black|piano|mozart }elseif($method == "search"){ while (list ($line_num, $line) = each ($fcontents)) { // Notice the use of eregi() for the keyword part // it allows for case-insensitive searches if(eregi($keyword, $line)){ echo "<b>Line $line_num:</b> " . htmlspecialchars ($line) . "<br>\n"; } } }else{ // supply a method, or do not run } ?> Robert Zwink -----Original Message----- From: David W. Fenton [mailto:dfassoc@bway.net] Sent: Wednesday, March 14, 2001 12:02 AM To: php-db@lists.php.net Subject: RE: [PHP-DB] Using text files instead of a DB On 13 Mar 01, at 9:43, Robert V. Zwink wrote: > http://phpclasses.upperdesign.com/browse.html/package/52 Thanks for this. Very simple and straightforward, but I don't see much in there that helps me. > The above link will take you to a class to manipulate CSV files. > This might help you understand file access in php. If you want to > maintain a database within a text file using CSV file format may > help. There is not (and will never be) any editing of this text file on the website. It's simply read-only, a periodically uploaded catalog of selected items to allow website viewers to search selected inventory on this client's website: <http://www.wurlitzerbruck.com/>. > If you have specific questions don't hesitate to post. [Keep in mind that I'm an experienced Visual Basic programmer but I've never done very much file I/O and I'm a complete newbie to PHP with no PERL experience, either] I can open the file with fgetcsv(), and there it all is in an array, which is very nice. However, what I really want is to pull only rows matching certain criteria into an array and then display *those*. I've created the data output as a flat file, with the many-to-one data flattened into single fields. [am I correct in surmising that the array created by fgetcsv() is addressable only by index number, and not by column name?] I need to match on: CreatorID (one per row, exact match) CreatorCategories (multiple items per row, match multiple partial strings) KeyWords (multiple items per row, match partial string) Title (one per row, partial match against a concatenation of three columns) The WHERE clause from my ODBC SQL strings would run like this: WHERE (CreatorID IN (232,654,1034)) AND (CreatorCategories Like '%Composer%' OR CreatorCategories Like '%Pianist%') AND (KeyWords Like '%Ballet%' OR KeyWords Like '%Cantatas%' OR KeyWords Like '%Chamber Music%') AND ((BibHeader & AutoHeader & OtherHeader) Like 'Sonata%') (the example makes no sense, of course, but includes all the possibilities). Now, obviously, I need a regular expression to compare a field value to a list of values, and another to see if any of a list of values is a substring of the field value. Regular expressions always make my eyes glaze over, but these are so simple (aren't they?) that I can't see any examples that I can understand are applicable to what I want -- the examples all seem designed to help you with more complicated tasks than what I need. And, obviously, I want to return just the rows that have a match. If I have the whole file in an array (with fgetcsv()), the row number of the array should be all I need to return, and then I can copy that whole row into a second array. What I'd really like is a way that would selectively copy to an array rather than copying from one big array to a smaller array, but the file has to be open, in memory, either way, right? Or can one read it in one line at a time, test against the conditions, and copy to the array or move on depending on the match? Once the selected rows are in an array, I'm set, since that's a piece of cake. I've read the f functions and the preg and ereg functions, but I'm stuck on: 1. don't know what strategy is best for opening the file. 2. don't know how to construct the needed regular expressions. 3. don't know how to find something in the file with the regex and return a line number (or array number if it's in an array), and then get that line into the array I really want (or just display the results at that point?). Actually, it makes a great deal of sense to fgetcsv() the file, then walk through it testing each row, and outputting only the matches. However, that won't be sorted, so I'd really need to copy the matches into an array where they can be sorted, since the output needs to be sorted. And, of course, in the end, I'll want to deliver results in blocks of 20 per page or so (but I think that can be dealt with by passing a row identifier and the array on to the next page to display; not that I know how to do that, but I can figure it out). Geez. It sounds pretty complicated, after all. Thanks in advance for any help/pointers any of you are able to give. -- David W. Fenton | http://www.bway.net/~dfenton David Fenton Associates | http://www.bway.net/~dfassoc -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net For additional commands, e-mail: php-db-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net

« previous php.db (#7494) next »