Simplest Image Gallery with PHP

Ask about a PHP problem here.
Post Reply
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Simplest Image Gallery with PHP

Post 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'; ?>
Last edited by nyo on Fri May 13, 2011 3:39 pm, edited 1 time in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Simplest Image Gallery with PHP

Post 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.
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Simplest Image Gallery with PHP

Post 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.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Simplest Image Gallery with PHP

Post 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 :)
Image
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Simplest Image Gallery with PHP

Post 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... :)
nyo
Posts: 124
Joined: Mon May 09, 2011 1:55 pm
Location: Mobile

Re: Simplest Image Gallery with PHP

Post 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...
Last edited by nyo on Fri May 13, 2011 3:39 pm, edited 1 time in total.
User avatar
jacek
Site Admin
Posts: 3262
Joined: Thu May 05, 2011 1:45 pm
Location: UK
Contact:

Re: Simplest Image Gallery with PHP

Post by jacek »

Cool, it looks like it worked what ever you did. Using the more modern method is usually better anyway.
Image
Post Reply