Jump to content

TINY search function needed - almost have it


obrian93

Recommended Posts

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.

Link to comment
Share on other sites

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);

?>

Link to comment
Share on other sites

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).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.