busnut Posted October 8, 2010 Share Posted October 8, 2010 Continuing on from another thread i've made about my photo gallery, I thought I would make a new one as i've pretty much got a working version, except I can't get the category to display just once. It either doesn't show at all or shows the category for each photo depending on where I locate the echo H2 reference. So what i'm wanting is this: category image image image etc next category image image image etc next category image image image etc Here is the script: $dir = "gallery"; $text = "gallery.txt"; $cols = 3; $colswidth = 33; $fh=fopen($text, "r"); while(!feof($fh)) { $temp = explode("|", $line); $title[$temp[0]] = $temp[1]; $description[$temp[0]] = $temp[2]; $photographer[$temp[0]] = $temp[3]; $category[$temp[0]] = $temp[4]; $line=fgets($fh); unset($temp); } function ListFiles($dir) { if($dh = opendir($dir)) { $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { echo "<h2>$category[$file]</h2><p>"; $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles($dir) as $key=>$file){ if (preg_match("/.jpg/", $file)) { echo "<a href=\"$file\" rel=\"lightbox\" title=\"$title[$file]<br>$description[$file]<br>© $photographer[$file]\"><img src=\"gallery-thumbnail.php?file=$file\">"; } } Quote Link to comment Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 Some thing like this if($dh = opendir($dir)) { $last_cat = ''; $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { if($last_cat != $categpry[$file]) { echo "<h2>$category[$file]</h2><p>"; $last_cat = $category[$file]; } $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } Quote Link to comment Share on other sites More sharing options...
busnut Posted October 8, 2010 Author Share Posted October 8, 2010 unfortuneatly it still isn't displaying the category. I've put the <h2> line in every position possible, with only down below the 'foreach' statement does it show the category, but shows it for every image Quote Link to comment Share on other sites More sharing options...
jl5501 Posted October 8, 2010 Share Posted October 8, 2010 I notice there is a typo in my code in the spelling of category that might be the issue Quote Link to comment Share on other sites More sharing options...
busnut Posted October 8, 2010 Author Share Posted October 8, 2010 I notice there is a typo in my code in the spelling of category that might be the issue didn't notice that myself, so i've changed that, still no luck sorry Quote Link to comment Share on other sites More sharing options...
busnut Posted October 9, 2010 Author Share Posted October 9, 2010 Got it working, FINALLY. Thanks to jl5501 for the snippet of code, instead of just moving the <h2> line, i've moved the whole bit about if last cat section to below the foreach statement. Attached is the code: gallery.php <h1>Photo Gallery</h1> <? $dir = "gallery"; $text = "gallery.txt"; $cols = 3; $colswidth = 33; $fh=fopen($text, "r"); while(!feof($fh)) { $temp = explode("|", $line); $title[$temp[0]] = $temp[1]; $description[$temp[0]] = $temp[2]; $photographer[$temp[0]] = $temp[3]; $category[$temp[0]] = $temp[4]; $line=fgets($fh); unset($temp); } function ListFiles($dir) { if($dh = opendir($dir)) { $last_cat = ''; $files = Array(); $inner_files = Array(); while($file = readdir($dh)) { if($file != "." && $file != ".." && $file[0] != '.') { if(is_dir($dir . "/" . $file)) { $inner_files = ListFiles($dir . "/" . $file); if(is_array($inner_files)) $files = array_merge($files, $inner_files); } else { array_push($files, $dir . "/" . $file); } } } closedir($dh); return $files; } } foreach (ListFiles($dir) as $key=>$file){ if (preg_match("/.jpg/", $file)) { if($last_cat != $category[$file] && $category[$file]<>"") { echo "<h2>$category[$file]</h2>"; $last_cat = $category[$file]; } if ($title[$file]<>"") { echo "<a href=\"$file\" rel=\"lightbox\" title=\"$title[$file]<br>$description[$file]<br>© $photographer[$file]\"><img src=\"gallery-thumbs.php?file=$file\" height='100' width='150'></a> "; } else echo "<a href=\"$file\" rel=\"lightbox\"><img src=\"gallery-thumbs.php?file=$file\" height='100' width='150'></a> "; } } ?> gallery-thumbs.php <?php // define directory path $image = exif_thumbnail($_GET['file']); header("Content-Type: image/jpeg"); echo $image; ?> And then with the description text file, upload it to the same directory as the gallery.php & gallery-thumbs.php directory in this format: gallery/subfolder name/file name.jpg|title|description|copyright|category And remember to upload your images under the 'gallery' folder then in their subfolders. then to get the lightbox working, download lightbox (search google) and implement some css to go with it. Thanks once again 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.