search hacked into phpalbum

I installed phpalbum early Jan. and was hoping for a new release with search, but just couldn't wait any longer. So I wrote up my own. This is crude and messy but it may give some of the other php writers a little more incite as to how this can be done.

Since I installed this for a dealership, I wanted to only search on Path, Short Description, Long Description. Nothing else can be searched on in my version, although someone can take this and modify it to search on any fields. Also, I don't have that many records in the album right now, so I don't know how it is for performance. It runs pretty quick, but there's not much to search through.

(A note: I have modified the album so that the user can navigate to different pages while still displaying the directory on the side. I use an include and test for whatever variables the user chooses in the menu.)

In my file: /themes/Borders/include/album.tpl.php I have an include:

if(!isset($_GET['var1'])){
if ($_GET['page'] == "search")
{
  include('include/search.php');
}
}

(For now I have everything on a seperate search page.)

Here is the search.php that I'm using. One thing to note, is that it only searches on either a whole phrase or a single word. If you put 2 words in, even though the 2 words may be in the description, if they are not like they were entered in the form, the results will not come up. I thought that I would take the search words and form them into an array, then loop through the rest, but I was happy to just get the minimum working for now.

<?PHP
$search_term = $_POST['search'];
$pieces = explode(" ", $search_term);

include 'config.php';

?>
<Form name ="form1" Method ="POST" Action ="main.php?page=search">
Enter Search:<INPUT TYPE = "Text" VALUE ="<? echo $search_term ?>" NAME = "search"><br><br>
<INPUT TYPE = "Submit" Name = "Submit" VALUE = "Submit">
</FORM>
<?
if (isset($_POST['search'])) {
$path_count = 0;
$path = array();
$seq_file = array();
$test = array();
$file_handle = fopen($data_dir."album_directory.php", "rb");
while (!feof($file_handle) ) {
$line_of_text = fgets($file_handle);
$parts = explode('=>', $line_of_text);
$parts[0] = str_replace("'","",trim($parts[0]));
if ($parts[0] == "path"){
if($path_count != 0){
$path[$path_count] = str_replace("'","",trim($parts[1]));
$path[$path_count] = str_replace("&","%26",trim($path[$path_count]));
$path[$path_count]= preg_replace('/[,]/', '', $path[$path_count]);
}
}
if ($parts[0] == "seq_files"){
if($path_count != 0){
$seq_file[$path_count] = str_replace("'","",trim($parts[1]));
$seq_file[$path_count]= preg_replace('/[,]/', '', $seq_file[$path_count]);
$test[$seq_file[$path_count]] = $path[$path_count];
}
$path_count++;
}

}
fclose($file_handle);
$counter2 = 0;
$search_array = array();
for ( $counter = 1; $counter < $path_count; $counter++) {
$search_counter = 0;
$file_name = "data_d3JhbmdsZXI/album_files_".$seq_file[$counter].".php";
$file_handle2 = fopen($file_name, "rb");
while (!feof($file_handle2) ) {
if ($search_counter >= 3){ $search_counter = 0; }
$line_of_text = fgets($file_handle2);
$parts2 = explode('=>', $line_of_text);
$parts2[0] = str_replace("'","",trim($parts2[0]));
$parts2[1] = preg_replace('/[,]/', '', $parts2[1]);
$parts2[1] = str_replace("'","",trim($parts2[1]));
if (($parts2[0] == "file_name") && ($search_counter == 0)){
if ($parts2[1] != "")
{
$link_string = "<a href=\"main.php?cmd=imageview&var1=".$test[$seq_file[$counter]].str_replace("'","",trim(preg_replace('/[,]/', '', $parts2[1])))."\">";
$search_array[$seq_file[$counter]][$counter2][$search_counter] = $link_string;
$search_counter++;
}
else
{
$search_counter = 3;
}
}
if (($parts2[0] == "desc") && ($search_counter == 1)){
if ($parts2[1] != "")
{
$search_array[$seq_file[$counter]][$counter2][$search_counter] = trim($parts2[1]);
$search_counter++;
}
else
{
unset($search_array[$seq_file[$counter]][$counter2][($search_counter-1)]);
$search_counter = 3;
}
}
if (($parts2[0] == "long_desc") && ($search_counter == 2)){
if ($parts2[1] != "")
{
$search_array[$seq_file[$counter]][$counter2][$search_counter] = trim($parts2[1]);
$search_counter++;
$counter2++;
}
else
{
unset($search_array[$seq_file[$counter]][$counter][($search_counter-2)]);
$search_counter = 3;
}
}
}
fclose($file_handle2);
$counter2 = 0;
}

    function array_search_recursive_all($needle, $haystack, $key, $path = NULL)
    {
        if (!$path['level'])
            $path['level'] = 0;
        if (!$path['work'])
            $path['work'] = array();
        if (!is_array($haystack))
            $haystack = array();

        foreach ($haystack as $_key => $_value)
        {
if (is_string($_value)){
$pos = stripos($_value, $needle);
}
            if (is_scalar($_value) && ($_value == $needle || $pos > -1) && !$key)
            {
                $path['work'][$path['level']] = $_key;
                $path['found'][] = $path['work'];
            }
            elseif (is_scalar($_value) && ($_value == $needle || $pos > -1) && $_key == $key)
            {
                $path['work'][$path['level']] = $_key;
                $path['found'][] = $path['work'];
            }
            elseif (is_array($_value))
            {
                $path['work'][$path['level']] = $_key;
                $path['level'] += 1;
                $path = array_search_recursive_all($needle, $_value, $key, $path);
            }
        }
        array_splice($path['work'], $path['level']);
        $path['level'] -= 1;
        return $path;
    }
$pos = array_search_recursive_all($search_term,$search_array,$x);
$loop = 0;

if ($pos[found] != ""){
foreach ($pos[found] as $value) {
$linkW = $search_array[$pos[found][$loop][0]][$pos[found][$loop][1]][0];
$titleW = $search_array[$pos[found][$loop][0]][$pos[found][$loop][1]][1];
$descW = $search_array[$pos[found][$loop][0]][$pos[found][$loop][1]][2];
if ($search_array[$pos[found][$loop][0]][$pos[found][$loop][1]][0] != $search_array[$pos[found][$loop-1][0]][$pos[found][$loop-1][1]][0]) {
if (is_string($linkW) && is_string($titleW) && is_string($descW)){
print "<li>".$linkW.$titleW."</a> - ".$descW;
}
}
$loop++;
}
}
else {
if (count($pieces) > 1) {
print "No results were found.  Try using single words or another phrase.";
}
else {
print "No results were found.";
}
}
}
?>

It searches for the default fields, even runs through nested directories. If anyone has any improvements I will be more then happy to update this.

You can test this by going to http://www.wranglersalesinc.com/main.php?page=search
Hopefully I can get the owners to insert more records so I can test out the performance a little better.