sleepyw Posted April 23, 2013 Share Posted April 23, 2013 (edited) I received some code from someone several months ago when I was searching for a way to simply upload photos to an FTP site and have the PHP automatically generate thumbnails for each gallery/directory. It uses jquery for displaying the images themselves. I cleaned it up and modified the PHP portion a little, but it's not code I'm familiar with (readdir, etc.). This is for an internal "stock photo" site our department uses. All of the file names are the keywords of the photos. We used to have the images on a SharePoint site, so the keywords were searchable. But since I had to relocate the images and create my own gallery, I don't have that search function anymore. I'm clueless about how to read through the directories with the search function like a query in MySQL. I'm trying to add the keyword GET function to the URL so the page would display the images including the keyword entered by the user (with an IF/ELSE statement to ignore the keyword search if ithe GET is empty). That's not the problem. The entire set of code is below, as its free for use to anyone else that is looking for something where they don't have to manually create thumbnails for an image gallery. The first block of code is the original code. The second block is where I'm trying to insert the search box and the code to perform the search. The third block is the original ending code. The 2nd block, where I need help, is where it would be something like 'QUERY($files) WHERE $files is LIKE %$keyword%'. The search needs to be global (all subdirectories, not just the top level). Obviously that's just an incomplete piece, but an idea of what I'm trying to do. Is this an easy query to create? <? // USER DEFINED $dir = "pics/"; // directory of images as related to index.php $height ="100"; //thumbnail image height -- needs to be defined for IE $width ="100"; //thumbnail image width -- needs to be defined for IE ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Photo Gallery</title> <link href="css/css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="js/jquery-1.8.0.min.js"></script> <script type="text/javascript" src="js/jquery.mousewheel-3.0.6.pack.js"></script> <script type="text/javascript" src="js/fancybox/jquery.fancybox.js?v=2.1.0"></script> <link rel="stylesheet" type="text/css" href="js/fancybox/jquery.fancybox.css?v=2.1.0" media="screen" /> </head> <? $gallery = $_GET["gallery"]; $keyword = $_GET["keyword"]; $dir = $dir.$gallery."/"; //Put files into an array // create a handler to the directory $dirhandler = opendir($dir); // read all the files from directory $nofiles=0; while ($file = readdir($dirhandler)) { // if $file isn't this directory or its parent //add to the $files array if ($file != '.' && $file != '..') { $nofiles++; $files[$nofiles]=$file; } } //close the handler closedir($dirhandler); //Back button to appear at the top of each page to go to the previous directory if not on the main page if ($gallery !="" or $keyword !="") { echo "<div><a href='javascript:history.go(-1)'><img src='images/up.png' border='0'></a></div>"; } //*************************************************************************// // KEYWORD SEARCH BOX- create text box to search entire directory for that keyword and display page in grid pattern with results ?> <div style='position:relative; left:10px;'> <form action='index_search.php?keyword=$keyword' method='get' name='search'> <input type='text' name='keyword' size='25'> <input type='submit' value='Search Keyword'> </form> </div> <? // PERFORM KEYWORD SEARCH if ($keyword !="") { echo "<div class='keytext'>Keyword search: <b>" . $keyword . "</b><br/></div>"; /*********************************************************************************************************/ /*********************************************************************************************************/ /* ***** THIS IS WHERE THE SEARCH OF DIRECTORY FILES NEEDS TO OCCUR AND OUTPUT RESULTS AS $files */ /* get results where $file LIKE %$keyword%; */ /*********************************************************************************************************/ /*********************************************************************************************************/ //Show images foreach ($files as $file) { if ($file!="."&&$file!="..") { $extention = explode('.', $file); if ($extention[1] != "") { echo "<div class='imgwrapper'>"; echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>"; echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>"; echo"</a><br/>"; $file_name = current(explode('.', $file)); echo substr($file_name,0,21); echo "</div>"; } } } } else { // starts the split from keyword or no keyword //***********************************************************************// sort($files); // sort folder names alphabetically //Show the folders foreach ($files as $file){ if ($file!="."&&$file!="..") { sort($files); //Sorts the array (file names) alphabetically -- not the directory/folder names $extention = explode('.', $file); if ($extention[1] == "") { echo "<div class='imgwrapper'>"; echo "<a href='?gallery=$gallery/$file'>"; echo "<img src='images/folder.jpg' border='0'><br/>"; echo current(explode('.', $file)); echo "</a>"; echo "</div>"; } } } ?> <div style="clear:both"></div> <? //Show images foreach ($files as $file){ if ($file!="."&&$file!="..") { $extention = explode('.', $file); if ($extention[1] != "") { echo "<div class='imgwrapper'>"; echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>"; echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>"; echo"</a><br/>"; $file_name = current(explode('.', $file)); echo substr($file_name,0,21); echo "</div>"; } } } } // ends the else splitting the search from non-search ?> <script type="text/javascript"> $(document).ready(function() { $(".fancybox").fancybox(); }); </script> Thanks in advance if anyone can help. Edited April 23, 2013 by sleepyw Quote Link to comment Share on other sites More sharing options...
lemmin Posted April 23, 2013 Share Posted April 23, 2013 (edited) You aren't ever checking the keyword against anything. Add an if statement to check the keyword in your first iteration of the files: foreach ($files as $file) { if ($file!="."&&$file!="..") { $extention = explode('.', $file); if ($extention[1] != "") { if (stripos($extention[0], $keyword) !== false) { echo "<div class='imgwrapper'>"; echo"<a class='fancybox' rel='group'' href='$dir$file' return false' title='$filename'>"; echo "<img src='timthumb.php?src=$dir$file&h=$height&w=$width' alt='$extention[0]' width='$width' height='$height'/>"; echo"</a><br/>"; $file_name = current(explode('.', $file)); echo substr($file_name,0,21); echo "</div>"; } } } } The sample code expects that you are storing the file names in a database (which you should), but this code should work for what you have right now Edited April 23, 2013 by lemmin Quote Link to comment Share on other sites More sharing options...
sleepyw Posted April 26, 2013 Author Share Posted April 26, 2013 Thank you for taking a look. The intent was not to put the files in a database....for ease of use (to be able to just upload via FTP and have everything appear--which is works perfectly in that respect). I'm not sure if the code you put in your post was the suggested fix, but I inserted it and it doesn't do anything. Quote Link to comment Share on other sites More sharing options...
sleepyw Posted May 16, 2013 Author Share Posted May 16, 2013 Ouch....crickets... Quote Link to comment 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.