Jump to content

Display Category Title once


busnut

Recommended Posts

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\">";
} 

} 

Link to comment
Share on other sites

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;
    }

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.