PHP search function...

philjohns

Senior Member
Im going to be honest from the start....Im a PHP NEWB!

Ive used it before in forms etc and know the basics (although never written my own code bar the odd form) and need some help with a personal project Im doing.

Im trying to develop a small, password protected website for me and a group of mates to use to upload photos that we take of eachother, travels etc that we can all then download if we want to...and yes...I know Ive pretty much described Flickr and the like but I also want to see if I can do this and hope I will learn along the way.

Ive got the uploading of the files sorted and that works (I used a script but am editing it to add more fields and options).

All I need now (I say all - haha) as a function that allows the user to type a keyword into a search box which will be in the images description or title (say their name) and then on submission the search box will search all images within a directory for that keyword...

Is this possible? If so - how do I go about it?

Phil
 
Yeah it is depends on your database, and I can help if it is MYSQL, as that is my baby.

basically you need to connect to the DB,

So something like ~
<?php
1.$cxn=mysqli_connect('localhost','root','username','password');

2.$query=mysqli_query($cxn,"SELECT * FROM Photos WHERE title LIKE '".$_POST['search_term'])."%' LIMIT 0,10");
3.while($result=mysqli_fetch_assoc($query)){
4.extract($result);
5.echo $name."<br />";
6.}
?>


Now I take it only friends can view this and they are not going to try to hack it and screw the site, if not we need to add some cleaning into.

But what this does is ~

1. We connect to your database and set it to a variable.
You need to add you user name and password,
2. We set a variable containing the query we are sending to look up.
3. We run a while loop though the results.
4. We extract each rows variables,
5. echo out the name field of any potential matches to the browser.
6. end the while loop.

If that is what you are looking for let me know and I'll help you expand it.

Jaz


Key:
Purple ~ Mysql and php
 
OK I have never had to use it but you can use scandir() to accomplish this.
Now this may be totally wrong but my first guess on using it would be something like ~
1.<?php
2.$search_term=$_POST['search_results'];
3.$success=null;
4.$files_in_dir=scandir("directory name you want to scan");
5.for($i=0;$i<sizeof($files_in_dir);$i++){
6.if($files_in_dir[$i]==$search_term){
7.echo"<img src='../img/".$files_in_dir[$i]."' alt='' />";
8.$success="found";
9.}
10.}
11.if(!isset($success)){
12.echo"We are sorry we could not find that image."
13.}
14.?>

Ok now I have not used this before and if you run into difficulties let me know, as to me this seems the best way of using it.

Right lets run through it.
1. We start a PHP section.
2. We set the variable $search_term to what the user entered in the form.
3. We set the success variable to null (nothing).
4. We search the directory, the results are set it to an array variable, if that makes sense.

The results would be an array of every file in that directory.
So for example if it contained ~
pictures.jpg, paris.png, london.jpg and new_york.jpg

For example our file array would be ~
[0] =>
pictures.jpg
[1] =>
paris.png
[2] =>
london.jpg
[3] =>
new_york.jpg

5. We then set up a for loop, set the $i variable to 0, we then test to make sure $i is less then the total array length, if it is we execute the loop, once the loop has finished we plus 1 to $i and test again, until $i equals the array length, then we finish the loop and continue with the rest of the code.

6. We then run an if statement and test if
$files_in_dir[$i], in other words in the first execution of the for loop it is the equivalent of saying $files_in_dir[0] on the second execution it would be $files_in_dir[1], on third it would be $files_in_dir[2], etc....

is equal to the corresponding number in the array.

7. If a match is found we print out an image tag to the browser to show the image.

For the image to display it would have to be up 1 directory and into the img directory, change that section of the file path to match your image directory.

8. We then set the $success variable, indicating a match has been found, to success.

9. We end the if statement.

10. We end the for loop.

11. After the for loop has tested each file name to see if it matched we then test to see if the $success varaible is not set, if no matches where found $success will still equal nothing so wont be set.

12. If it is not set no results are found so echo out a not find statement to the user.

13. End the second if statement.

14. End the PHP section.

Things to note ~
1. I have not used this before, but that should work, if it does not, make sure the file path in the directory you are searching is correct, I assume it will be relative to your current file, and not relative to the root directory, if it fails, let me know what output you get, including any output to the actual source page and I'll have a re-think.
2. For this to actually work the user would also need to search for the file extention. Now we can run a regular expression match and get rid of it if you just want them to search for the file name instead, or in fact we can improve it and do both, get it working first though, lol.

Let me know if this is what you mean, and let me know any problems, if you encounter any.
Jaz

Key:
Purple ~ PHP
 
Back
Top