Jump to content

Recommended Posts

Hello!

Yes, I know on the index page it shows how to make a simple mysql search engine. However, I would like to make this search engine without using mysql if possible, so that I don't have to manage a database.

I've been trying to modify a piece of code for hours.  :facewall:

 

This is, basically, the original file.

<?php
if ($_GET['name'] != '') {
$files = glob('maps/*.*');
$results = array();
$query = explode(' ', $_GET['name']);
foreach ($files as $file) {
	$x = 0;
	if (stripos(basename($file), $query[0]) !== false) {
		$results[] = $file;
	}
}
if (count($results) > 0) {
	echo count($results).' files matched the search <em>' . htmlentities($_GET['name']) . "</em>:<br /><br />\n";
	foreach ($results as $file) {
		echo '<a href="' . $file . '">' . basename(substr_replace($file ,"",-) . '</a><br />' . "\n";
	}

} else {
	echo 'No files matched the search <em>' . htmlentities($_GET['name']) . "</em>.<br />\n";
}
} else {
echo 'Search the files:<br />';
}
?>
<br />
<form action="index.php" method="get">
<input type="text" name="name" />
<input type="submit" />
</form>
<?php
$files = glob('maps/*.*');
echo "Searching through ".count($files)." files, with more added daily.<br>";
?>

Anyways, the problem is that if you enter multiple words, (for example, example file,) and the name of the file is something like example blah file, it wont recognize it. Ive tried exploding the query into the component words... But I'm stuck there.  :confused:

So now I'm asking the experts to help. Please help me!

Thanks in advance.  :D

Link to comment
https://forums.phpfreaks.com/topic/168089-php-search-engine-files-in-a-directory/
Share on other sites

well first off the following line

 

stripos(basename($file), $query[0]) !== false

 

only checks if the first word of the query is in the file name

 

something like

 

foreach ($query as $q){
if (stripos(basename($file), $q]) !== false){
$results[] = $file;
break;
}
}

 

instead of that if statement will check every word of the query

 

also I think you may want to use the array_push function instead of the line

$results[] = $file;

 

It's been a bit of time since I've tangoed with PhP heavily, but I am not sure if you can add entries into an array that way

 

hope that helps

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.