Page 1 of 1

Basename returning filename x 100

Posted: Mon May 15, 2006 2:26 pm
by ozzy
I have included the basename() function into my script:

Code: Select all

<form action="page.php?name=Library&file=search" method="POST"><input size="25" type="text" name="query" value="">&nbsp;<input type="submit" value="Search"></form>

<?php //...
$term = trim($_POST['query']); 
$files = glob($dir."modules/Library/files/*"); 
foreach( $files as $key => $value ){ 
    if(preg_match("#".$term."#i", $value))

$file = basename($value);        // $file is set to "index.php"
 
    echo "<a href=\"page.php?name=Library&file=file&page=$value\">$file</a><br>"; 
} ?>
But the problem is that the results returned are returned like like 1000 times dispending of how many match the query. So it would look something like (if 1 file matches the query)

Code: Select all

The file name
The file name
The file name
The file name
The file name
The file name
The file name
The file name
The file name
The file name
But times 100. Does anyone know how i could resolve this, thanks in advance!

Posted: Mon May 15, 2006 2:35 pm
by alex-weej
Assuming you realise that $term is a regular expression, I don't see anything wrong with that.

Do a var_dump($files); after the glob() and show the first ten lines of output, please!

Posted: Mon May 15, 2006 4:53 pm
by pickle
If you don't put the curly braces {} after an if statement, it only executes the next line. So, your

Code: Select all

$file = basename($value);
is the only one that's conditional.

Code: Select all

echo "<a href=\"page.php?name=Library&file=file&page=$value\">$file</a><br>";
will get executed every time.

Put curly braces around those two statements & it should work fine.

Posted: Tue May 16, 2006 1:59 pm
by ozzy
Im not quite sure what you mean, but is this what you mean...

Code: Select all

$file = basename($value);

if ( $file == "$value" ) {
    echo "$file";
}
Thanks. Oh, and ps... that doesnt work :)

Posted: Tue May 16, 2006 2:26 pm
by pickle
Check your preg_match() then - that must always be returning true.

Posted: Tue May 16, 2006 2:47 pm
by ozzy
Thanks pickel once again :),

I did what you said and moved

Code: Select all

if(preg_match("#".$term."#i", $value))
under the basename() function and changed '$value' to $file

w00t :D