Page 1 of 1

Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 12:43 pm
by nyo
Ok, I gave up following the Automatic Image Gallery tutorial for the moment. I will go with a rather simpler version of image gallery now. I have two issues though.

Firstly, please go to http://nailyener.com/otel/resimler.php and you will see how it looks now. You will notice that the images are not sorted alphabetically, this is problem 1, I want to sort images alphabetically.

Next, when you click on an image, another page opens with a larger version of the image and "Back" "Next" buttons. I have no idea how to code "Back" and "Next" buttons, this is problem 2.

Below, you will see the files:

resimler.php
<?php include 'includes/header.php'; ?>
<div id="title"><a href="tr.php">Ana Sayfa</a> » Resimler</div>
	<?php
		$dir = "gallery/";
		if ($opendir = opendir($dir)) 
		{
			while (($file = readdir($opendir)) !== FALSE) 
			{
				if ($file!="."&&$file!="..")
					echo "<div class='images'><a href='tr-img.php?img=$file'><img src='$dir$file' width='100' height='100' alt='$file' /></a></div>";
			}
		}
	?>
<?php include 'includes/footer.php'; ?>
tr-img.php
<?php include 'includes/header.php'; ?>
<div id="title"><a href="tr.php">Ana Sayfa</a> » <a href="resimler.php">Resimler</a></div>
<div id="content">
	<div class="imgpage">
	<?php
		$file = $_GET[img];
		
		$dir = "gallery/";
		echo "<img src='$dir$file' width='200' height='200' alt='$file' />";
	?>
	</div>
	<div class="imgnavi"><a href=''>Back</a> | <a href=''>Next</a></span></div>
</div>
<?php include 'includes/footer.php'; ?>

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 1:20 pm
by jacek
To sort the images differently you will have to go with the scandir method we talked about before.

And the best sorting function to use would probably be natcasesort() check http://php.net/natcasesort for how to use it.

As for the next and back buttons, you will have to create the same array of images, scandir again, somehow work out the current image's position in the array and then add 1 to it. array_search() may be useful.

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 1:39 pm
by nyo
Jacek, when I did this on localhost, it sorted images alphabetically, but when I uploaded to the server, it didn't. I will try your suggestions.

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 2:06 pm
by jacek
nailyener wrote:Jacek, when I did this on localhost, it sorted images alphabetically, but when I uploaded to the server, it didn't.
Thats a difference between windows and linux :)

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 2:58 pm
by nyo
jacek wrote:
nailyener wrote:Jacek, when I did this on localhost, it sorted images alphabetically, but when I uploaded to the server, it didn't.
Thats a difference between windows and linux :)
Ohh, I see. There are mountains of things I need to learn about PHP and web design... :)

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 3:22 pm
by nyo
jacek wrote:It should be
    <?php
                    $dirs = scandir('gallery/');
                    unset($dirs[0], $dirs[1]);

                    foreach ( $dirs as $files ) {
                             echo "<img src='gallery/$file' width='200' height='200' alt='Melek Apart Hotel Resim' /><br />";
                    }
            ?>
Jacek, this reply is one of the replies that I had missed, just to give an example to my suggestion post. I changed my code with the help of yours and now the images are sorted alphabetically, you can see it: http://nailyener.com/otel/resimler.php

Now, I will see what I can do about the Back and Next buttons...

Re: Simplest Image Gallery with PHP

Posted: Tue May 10, 2011 3:23 pm
by jacek
Cool, it looks like it worked what ever you did. Using the more modern method is usually better anyway.