Jump to content

Recommended Posts

Hello!

This is my very simple search engine script, I'll keep it short:

The problem is that the first query isnt being rechecked with the results to see if they are compatible. (it's searching files in a directory)

 

Example:

 

Search: llamas mexico

 

Find all files with "llamas" in name.

Find all files with "mexico" in name.

Remove files without "mexico" in name.

 

However, the last part about removing files with "llamas" in name doesn't work.

 

Heres is the code:

Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>ch4n.net's Starcraft maps!</title>

<style type= "text/css" title="styleid" media="all">

<!--

body {

Font-family:Arial, Helvetica, sans-serif;

background:#EEE;

}

hr {

margin: 17px 0 18px;

height: 0;

clear: both;

border: solid #ddd;

border-width: 1px 0 0;

border-top: 1px solid #ddd;

border-bottom: 1px solid #fff;

}

h4 {

text-shadow: 0 1px 1px #FFF;

}

-->

</style>

 

</head>

 

<body>

<h4>ch4n.net Starcraft map collection</h4>

<?php

if ($_GET['name'] != '') {

$files = glob('maps/*.*');

$results = array();

$query = explode(' ', $_GET['name']);

if ($query[1] == '') {

foreach ($files as $file) {

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

$results[] = $file;

}

}

} else {

$a = 0;

$numquery = count($query);

$results = array();

while ($a != $numquery) {

foreach ($files as $file) {

if (stripos(basename($file), $query[$a]) !== false) {

$results[] = $file;

}

}

 

$b = 0;

$numres = count($results);

while ($b != $numres) {

$results[$b] = $resultz;

if (stripos(basename($resultz), $query[$a]) == false) {

unset($results[$b]);

}

$b++;

 

 

}

$a++;

}

 

 

}

 

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 ,"",-8)) . '</a><br />' . "\n";

}

echo "Not finding what you want? We're working on improving our search engine. Click <a href='tips.html'>here</a> for some tips!<br>";

} else {

echo 'No files matched the search <em>' . htmlentities($_GET['name']) . "</em>.<br />\n";

echo "It's OK. Though or map collection is growing daily, we might not have picked this one up yet. However, you can always click <a href='tips.html'>here</a> for some tips!<br>";

}

} 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)." maps, with more added daily.<br>";

echo "Say, have you ever heard of these maps?<br><br><em>";

$i = 1;

while ($i <= 2) {

$i ++;

$random = rand(0, count($files));

echo basename(substr_replace($files[$random] ,"",-8))."<br>";

}

echo "</em><br>You might want to try them! Just search for them in our collection!";

?>

 

</body>

</html>

Thanks in advance!

if (!empty($_GET['name'])) {
    $globQuery = implode(DIRECTORY_SEPARATOR, array(realpath('maps'), '*/*'));
    $files = glob($globQuery);
    $words = str_word_count($_GET['name'], 1);
    $matches = array();//array_flip($words);
    foreach ($files as $file) {
        $file = basename($file);
        foreach ($words as $word) {
            if (stripos($file, $word)) {
                $matches[$file][] = $word; // the more words a file has the higher it's relevancy
            }
        }
    }
    //executes in count($files)^count($words) I think
    //do something with $matches
}

OK, this is what I have now.

	$files = glob('maps/*.*');   $words = str_word_count($_GET['name'], 1);
    $matches = array();//array_flip($words);
    foreach ($files as $file) {
        $file = basename($file);
        foreach ($words as $word) {
            if (stripos($file, $word)) {
                $matches[$file][] = $word; // the more words a file has the higher it's relevancy
            }
        }

	if (count($matches[$file]) == count($words)) {
		array_push($results, $file);
	}

    }

However, the final if statement NEVER evaluates true.

I feel like i'm missing something really obvious.

I have slightly modified the code and sorted $matches for your convenience.

 

if (!empty($_GET['name'])) {
    $globQuery = implode(DIRECTORY_SEPARATOR, array(realpath('maps'), '*/*'));
    $files = glob($globQuery);
    $words = str_word_count($_GET['name'], 1);
    $matches = array();//array_flip($words);
    foreach ($files as $file) {
        $file = basename($file);
        foreach ($words as $word) {
            if (stripos($file, $word)) {
                if (!isset($matches[$file])) {
                    $matches[$file] = 1;
                } else {
                    $matches[$file]++;
                }
            }
        }
    }
    arsort($matches);    
}

Thanks for all your help, but I still have two problems.

1) Each "match" always evaluates to one, and

2) When I use a foreach ($matches as $file), I end up printing 1.

 

Thanks!

 

foreach ($matches as $file => $times)

  • 2 weeks later...

I'm sorry its been a while since I've replied, but I'm still confused about this.

            if (stripos($file, $word)) {
                if (!isset($matches[$file])) {
                    $matches[$file] = 1;
                } else {
                    $matches[$file]++;
                }
            }

Each file ALWAYS evaluates to one match.

File: ABC DEF

File 2: ABC

Query: AB DE

Results: File 1 & File 2, both have only one match.

It seems like the second part isn't ever evaluating to be true.

 

Please help =(

 

EDIT: I think I have found out the problem!

Apparently there can be two values of an array with the same name? So because this is a two dimensional array, (or whatever you call it) the name of an array is the duplicate as the other, so it will always evaluate to notset... which doesnt really make sense.

 

EDIT # 2: Nevermind.. I just had a file with underscores instead of spaces that I was mistaking as identical. I still cannot find out the problem!

  • 2 months later...
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.