yanjchan Posted August 1, 2009 Share Posted August 1, 2009 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 ,"",-) . '</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] ,"",-)."<br>"; } echo "</em><br>You might want to try them! Just search for them in our collection!"; ?> </body> </html> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/ Share on other sites More sharing options...
ignace Posted August 1, 2009 Share Posted August 1, 2009 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 } Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-888189 Share on other sites More sharing options...
yanjchan Posted August 1, 2009 Author Share Posted August 1, 2009 Thanks, but how should I print the matches that have the most relevancy first? As your comment says, I could use count, but Im not sure how to implement it. (Because it's an array in an array D:) Thanks Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-888507 Share on other sites More sharing options...
yanjchan Posted August 1, 2009 Author Share Posted August 1, 2009 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-888568 Share on other sites More sharing options...
yanjchan Posted August 2, 2009 Author Share Posted August 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-888683 Share on other sites More sharing options...
ignace Posted August 2, 2009 Share Posted August 2, 2009 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); } Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-888854 Share on other sites More sharing options...
yanjchan Posted August 2, 2009 Author Share Posted August 2, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-889004 Share on other sites More sharing options...
ignace Posted August 2, 2009 Share Posted August 2, 2009 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) Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-889037 Share on other sites More sharing options...
yanjchan Posted August 11, 2009 Author Share Posted August 11, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-895261 Share on other sites More sharing options...
yanjchan Posted August 16, 2009 Author Share Posted August 16, 2009 Ump Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-899310 Share on other sites More sharing options...
yanjchan Posted November 2, 2009 Author Share Posted November 2, 2009 Sorry that I haven't followed up with this topic in a long time. I've decided to revisit this, as the problem behind this is much bigger than this single project for me. Can anyone explain? Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-949015 Share on other sites More sharing options...
yanjchan Posted November 3, 2009 Author Share Posted November 3, 2009 I'm sorry. I'm still awaiting help. Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-949802 Share on other sites More sharing options...
yanjchan Posted November 4, 2009 Author Share Posted November 4, 2009 Anyone? Quote Link to comment https://forums.phpfreaks.com/topic/168361-php-search-engine-not-checking-all-queries/#findComment-950610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.