0

Hi guys I'm trying to make a search engine in my website. Search engine must browse in a category and after search must display all results. But the engine doesn't display the results.

Example: X user is searching in PC-games for Y game. The X user type in text area the name of game and then click button search. After search script doesn`t display the results of Y game. Here is my script

<form action="search.php" method="POST">
<p><br />
<input name="q" type="hidden" />
<font color="black">Hunt Data:</font>
<input name="qfront" type="text" style="width: 230px" name="nume"id="nume" />
<select name="category">
<option value="0">All</option>
<option value="1">PC-Games</option>
<option value="2">Console</option>
<option value="3">Movies</option>
<option value="4">Music</option>
<option value="5">XXX</option>
<option value="6">Windows</option>
<option value="7">Linux</option>
<option value="8">Software</option>
<option value="9">Documents</option>
<input type="submit" value="Search" />
</p>
</form>

PHP script

<html>
<body>
<center>
<font color="black" size="4">
<?php
//define each directory here, the index starts with 0 == all and so on.
$categorydir = array('/Category/All/', '/Category/PCGames/', '/Category/Console/', '/Category/Movies/', '/Category/Music/', '/Category/XXX/', '/Category/Windows/', '/Category/Linux/', '/Category/Software/', '/Category/Documents/');
//if option selected and its valid number
if (isset($_POST['category'])) 
    if(ctype_digit($_POST['category']) && isset($categorydir[$_POST['category']])) {
        if(array_key_exists($_POST['category'], $categorydir) && is_dir($categorydir[$_POST['category']])) {
            $handle = opendir($categorydir[$_POST['category']]); //LINE 14
            $files = scandir($handle);
            echo 'Results of search:<br></br>';
            foreach($files as $file){ //LINE 17
                echo($file);
            }
        } else {
            echo 'target directory not found';
    }
}
?>
</font>
</center>
</body>
</html>
<html>
<head>
<title>DataHunters</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="header">
<h1 id="logo"><a href="index.html">DataHunters</a>
</h1>
</div>
<div id="navigation">
<ul>
<li><a class="active" href="index.html">Home</li></a>
<li><a href="chat.html">Chat</li></a>
<li><a href="contact.html">Contact</li></a>
<li><a href="http://www.forum.datahunters.ro">Forum</li></a>
</ul>
</li>
</div>
</body>
</html>
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Rzn Rzv
  • 19
  • 6
  • 3
    Have you thought about using a database? – ficuscr Mar 14 '13 at 17:45
  • Yes but I don`t know how to create a database. So I`m trying without it. – Rzn Rzv Mar 14 '13 at 17:46
  • Try removing the `ctype_digit($_POST['category']`? It is a string no?.. ahh nvm. Still be explicit with the... `array(0 => 'category', etc)`... If for nothing other than readability. Think your issue though is that `scandir` takes a string dir, not a file handle. – ficuscr Mar 14 '13 at 17:49
  • 1
    If you're going to reinvent the wheel and make a search engine, you should learn database basics. – Aaron Blenkush Mar 14 '13 at 17:53
  • 1
    Please don't [post](http://stackoverflow.com/questions/15056015/php-search-function-in-a-category) [multiple](http://stackoverflow.com/questions/15120727/display-a-file-on-page-using-php) [instances](http://stackoverflow.com/questions/15072949/php-search-engine-notice-undefined-index-category) [of](http://stackoverflow.com/questions/15074996/php-post-data-after-search) [the](http://stackoverflow.com/questions/15096861/post-a-file-after-search) [same](http://stackoverflow.com/questions/15122093/php-search-issued-file) [question](http://stackoverflow.com/questions/15416470) (SEVEN)! – Aaron Blenkush Mar 14 '13 at 18:00

1 Answers1

1

Here, I'll post that as an answer if you feel you can close this.

I think your issue is that scandir takes a string argument for the directory, not a file handle.

As people are saying, you should really take the time to learn more about databases. There are many tutorials available on the web and this is pretty much a required skill to posses for any sort of development. MySQL and PDO is a good place to start.

Good luck.

ficuscr
  • 6,975
  • 2
  • 32
  • 52