Special Search

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Okay, I've just created this PHP script and know to finish it off, I
want to add a search feature. I want the search to search in a folder
called "songs/". The songs/ folder is full of .txt files all allocated
in subfolders of songs/. It is supposed to work like a regular search
engine. But the results need a little tweaking: if there's a file
called song/s/sing-a-song.txt, I want the PHP script to print a link to
index.php?song=sing-a-song. The value of the song query in the link
will always be the same as the file name, excluding the file extention:

Another example:
There's a text file: songs/b/better-is-one-day.txt
I want the PHP script to print a link to

I tried creating a script that does this, but it has proved futile. I
was hoping if you could help me, or at least guide me. All replies
would be appretiated.

Re: Special Search

Hope I understood and it helps;
1st STEP:
You need a recursive function for subdirectory handling.
recursive function means "a function calls himself" in a loop.
usually an infinite loop but not here. search for similar algorythims
to handle every subdirectories one by one.
function recursive_func($basedir) {
 $handle =opendir($basedir);
 while ($file = readdir($handle)) {
  do your compare and other stuff here;
  if (is_dir($file)) {

2nd STEP:
use a regular expression for keyword search in file names for
use like ereg() or eregi() functions. they explained in references.
"if (ereg("key", $filename)) { print "<a>form your URL here</a>"; }"
esarch better examples.

Re: Special Search

On 2 Nov 2006 14:10:55 -0800, majik92@gmail.com wrote:

Quoted text here. Click to load it
Quoted text here. Click to load it

First I would advice you to move to a database, going through all
those files will be hard on performance, unless it's just a private
script or something only a few people will use.

Second, I need you to describe the file structure a bit more, how
exactly are the files positioned?

best regards,
Martin Mouritzen.

Re: Special Search

A database would be the optimal solution at any rate but, assuming the
songs dir is in the current directory and $song_name is set:

function getfilearray($start) {
  while (false !== ($found=readdir($dir))) { $getit[]=$found; }
  foreach($getit as $num => $item) {
   if (is_dir($start.$item) && $item!="." && $item!="..") {
$output[""]=filearray($start.$item."/"); }
   if (is_file($start.$item)) { $output[""]="FILE"; }
  return $output;

$f_letter = substr($song_name,0,1);
    $files = getfilesarray("./songs/".$fletter."/");
    foreach($files as $name => $type){
        if($name==$songname.".txt" && $type = "FILE")
            $found = true;
echo "<a href=\"index.php?song=$song_name\">$song_name</a>";

Site Timeline