Basename returning filename x 100

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Basename returning filename x 100

Post 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!
alex-weej
Forum Newbie
Posts: 19
Joined: Sun May 14, 2006 11:20 am

Post 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!
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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 :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Check your preg_match() then - that must always be returning true.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ozzy
Forum Commoner
Posts: 74
Joined: Tue Apr 11, 2006 4:45 pm

Post 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
Post Reply