obrian93 Posted November 17, 2009 Share Posted November 17, 2009 After all of a week or so of PHP under my belt, I've hit a bit of a wall. I have a flat file (names.txt) with 2000 or so rows of this: L-00001025|FRIDAY ZONE L-00001067|NATURAL HERITAGE OF INDIANA L-00001078|NATURAL HERITAGE OF INDIANA and some Google'd code that will search the file and print a result: $file = file_get_contents("names.txt"); if(strpos($file, "L-000001067")) { echo "IS THERE"; }else{ echo "IS NOT THERE"; } I've got more code that will print out everything in the file in a nice table that treats the pipes as delims: $fp = fopen('names.txt','r'); if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;} while (!feof($fp)) { $line = fgets($fp, 1024); //use 2048 if very long lines list ($field1, $field2) = explode ('|', $line); echo ' <tr> <td>'.$field1.'</td> <td>'.$field2.'</td> </tr>'; $fp++; } fclose($fp); Is there a way to marry the two so that finding field1 just echos field2? Eventually it'll be picked up by another search routine so that descriptions are returned in addition to results. thanks. My own searches have all returned too little or WAY too much. Quote Link to comment https://forums.phpfreaks.com/topic/181920-tiny-search-function-needed-almost-have-it/ Share on other sites More sharing options...
Psycho Posted November 17, 2009 Share Posted November 17, 2009 This is not tested , but the logic should be right <?php //This function processes the import file and returns an array in the format //array( // ['L-00001025'] => 'FRIDAY ZONE', // ['L-00001067'] => 'NATURAL HERITAGE OF INDIANA', // ['L-00001078'] => 'NATURAL HERITAGE OF INDIANA' //) function importFile($filePath) { $output = array(); $lines = file($filePath); foreach($lines as $line) { list($code, $value) = explode('|', $line); $output[$code] = $value; } return $output; } //This function will output // - ALL value if no search value is passed // - All values that match a single search value passed as a string or an array // - All values that match multiple search values passed as an array function displayResults($resulAry, $search=false) { if ($search !== false) { if(is_string($search)) { $outputAry = $resulAry[$search]; } elseif(is_array($search)) { $searchKeys = array_fill_keys($search, ''); $outputAry = array_intersect_key($resulAry[$search], $searchKeys); } } else { $outputAry = $resulAry; } echo "<table> "; foreach($outputAry as $code => $value) { echo "<tr><td>{$code}</td><td>{$value}</td></tr> "; } echo "</table> "; } //Example usage $data = importFile('names.txt'); echo "<br>Here are the records for the code 'L-00001067':<br> "; displayResults($data, 'L-00001067'); echo "<br><br>Here are the records for the codes 'L-00001067' & 'L-00001067':<br> "; $searchAry = array('L-00001067', 'L-00001067'); displayResults($data, $searchAry); echo "<br><br>Here are ALL the results for 'L-00001067':<br> "; displayResults($data); ?> Quote Link to comment https://forums.phpfreaks.com/topic/181920-tiny-search-function-needed-almost-have-it/#findComment-959564 Share on other sites More sharing options...
obrian93 Posted November 19, 2009 Author Share Posted November 19, 2009 Thanks, mjdamato. Just so I'm clear on this--I need to pull the whole text file in as an array before I can search against it? I'm starting to wonder if sucking it up and installing MySQL might be a little easier. I've since at least branched out to legit csv files (a direct result of reading up on the functions you gave me--thanks again). Quote Link to comment https://forums.phpfreaks.com/topic/181920-tiny-search-function-needed-almost-have-it/#findComment-961513 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.