Jump to content

Sort image gallery with individual links by file name?


02simond

Recommended Posts

Hi

I am trying to build a site which has an image gallery with links on it. I want to list the the images in different ways and I have successfully made it work with a "shuffle"-script like this:

 

<?php 
$links[] = Array("../r/ahlens.html", "k/k.jpg");
$links[] = Array("../r/brothers.html", "k/k.jpg");
$links[] = Array("../r/hm.html", "k/k.jpg");
$links[] = Array("../r/kappahl.html", "k/k.jpg");
$links[] = Array("../r/mq.html", "k/k.jpg");
$links[] = Array("sida1.php", "k/l.jpg");
$links[] = Array("../r/kappahl.html", "k/k.jpg");
$links[] = Array("../r/mq.html", "k/k.jpg");
$links[] = Array("../r/nk.html", "k/k.jpg");

shuffle($links);

foreach($links as $link){
    echo "<a href=\"${link[0]}\" ><img src=\"${link[1]}\" class=\"randomImage\" /></a> ";  
}  
?>

 

It works fine and opens a new page when clicked.

 

Now I would like to make it listed by file name instead of shuffling. How do I do that without loosing the links? And can it be done with a Directory because there will be a lot more photos in the future?

(I would also like the link to pages to be opened in a nice popup. Can I implement that?)

Thanks in advance!

Link to comment
Share on other sites

- array_multisort can sort the array on whichever filename you want.

- When you eventually create $links from files in a directory, use glob: it sorts by filename automatically.

- Yes, you can do popups. But define "popup". Window? Something on the page?

 

Is array_multisort and glob two different ways of doing the same thing? And do I get the links with one of these solutions? Can you show a sample code?

I would like popups in this style(when you click on a picture): http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm

Link to comment
Share on other sites

Is array_multisort and glob two different ways of doing the same thing?

No, they're two completely different functions. Read the manual to see what they do.

 

And do I get the links with one of these solutions? Can you show a sample code?

You'd make the links the same way you're doing now. The only difference is that the array is sorted before you display what's in it.

 

I would like popups in this style(when you click on a picture): http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm

Okay. That page seems to give instructions on how...

Link to comment
Share on other sites

Is array_multisort and glob two different ways of doing the same thing?

No, they're two completely different functions. Read the manual to see what they do.

 

And do I get the links with one of these solutions? Can you show a sample code?

You'd make the links the same way you're doing now. The only difference is that the array is sorted before you display what's in it.

 

I would like popups in this style(when you click on a picture): http://www.dynamicdrive.com/dynamicindex4/php-photoalbum.htm

Okay. That page seems to give instructions on how...

 

Im sorry but I dont know php that well at all. As I will upload new photos to replace the old often the order will need to change often. I dont know how to apply glob or multisort.

 

I started out trying the script on the dynamicdrive site but when I got to the links I realized that it wasnt fixed to each picture. Otherwise that would have been a perfect script. Is there a simple way to modify that script so that each photo in the directory has it's own link?

Link to comment
Share on other sites

You can't really do creation date. Most Linux systems don't track that information.

You can do modified date, however: get an array of files and use usort.

$files = array("1.jpg", "2.jpg", "3.jpg...");
usort($files, "filemtime");

 

Modified date would work. I tried replacing "shuffle($links);" from my first post with "usort($links, "filemtime");" but I just got this error repeated over the screen:

 

Warning: Wrong parameter count for filemtime() in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 52

 

How do I fix this as I have multiple arrays that shows a picture with a link?

Link to comment
Share on other sites

To use filemtime() like that the $files (or $links) array needs to be just an array of filenames. It can't be an array of arrays.

Start with this

$links = Array();
$links["../r/ahlens.html"] = "k/k.jpg";
$links["../r/brothers.html"] = "k/k.jpg";
$links["../r/hm.html"] = "k/k.jpg";
$links["../r/kappahl.html"] = "k/k.jpg";
$links["../r/mq.html"] = "k/k.jpg";
$links["sida1.php"] = "k/l.jpg";
$links["../r/nk.html"] = "k/k.jpg";

shuffle($links);

foreach($links as $file => $image){
    echo " ";  
}  
?>

and instead of shuffle() use

uasort($links, "filemtime");

Link to comment
Share on other sites

To use filemtime() like that the $files (or $links) array needs to be just an array of filenames. It can't be an array of arrays.

Start with this

<?php
$links = Array();
$links["../r/ahlens.html"] = "k/k.jpg";
$links["../r/brothers.html"] = "k/k.jpg";
$links["../r/hm.html"] = "k/k.jpg";
$links["../r/kappahl.html"] = "k/k.jpg";
$links["../r/mq.html"] = "k/k.jpg";
$links["sida1.php"] = "k/l.jpg";
$links["../r/nk.html"] = "k/k.jpg";

shuffle($links);

foreach($links as $file => $image){
    echo "<a href=\"$file\" ><img src=\"$image\" class=\"randomImage\" /></a> ";  
}  
?>

and instead of shuffle() use

uasort($links, "filemtime");

 

Thank you! Really appreciate all your help :D

Tried this but now the links doesnt work. It just links to numbers. The first picture links to 1, the second to 2 and so on.

How do I fix this?

 

The shuffle command works fine but when I try uasort($links, "filemtime"); it says like this:

Warning: Wrong parameter count for filemtime() in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 48

Link to comment
Share on other sites

What's your code now?

 

Like this:

<?php
$links = Array();
$links["../r/ahlens.html"] = "k/k.jpg";
$links["../r/brothers.html"] = "k/k.jpg";
$links["../r/hm.html"] = "k/k.jpg";
$links["../r/kappahl.html"] = "k/k.jpg";
$links["../r/mq.html"] = "k/k.jpg";
$links["sida1.php"] = "k/l.jpg";
$links["../r/nk.html"] = "k/k.jpg";

shuffle($links);

foreach($links as $file => $image){
    echo "<a href=\"$file\" ><img src=\"$image\" class=\"randomImage\" /></a> ";  
}  
?>

 

This makes links to numbers. like http://mypage.com/shuffle/1 if I click the top left image.

 

Link to comment
Share on other sites

shuffle() will lose the HTML filenames in the array (the array "keys") and replace them with numbers.

 

I thought you wanted to sort the array, not randomize it...

 

Yes I want to sort AND shuffle. I want to have both options on two different pages and I will have much more links later on so it would have been nice if I could have used the same code so I dont have to update to different lists of links.

 

Anyway as I said in an earlier post when I try with uasort I get this message printed repeatedly:

Warning: Wrong parameter count for filemtime() in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 48

 

And as I get this faulty message the ordering by modified date doesnt work.

Any clues why I get this message?

Link to comment
Share on other sites

define("RANDOMIMAGE_FORMAT", ' ');

// $a-$b=ascending order, $b-$a=descending order
function sortbymtime($a, $b) { return filemtime($a[0]) - filemtime($b[0]); }

$links = array(
array("../r/ahlens.html", "k/k.jpg"),
array("../r/brothers.html", "k/k.jpg"),
array("../r/hm.html", "k/k.jpg"),
array("../r/kappahl.html", "k/k.jpg"),
array("../r/mq.html", "k/k.jpg"),
array("sida.php", "k/l.jpg"),
array("../r/nk.html", "k/k.jpg")
);

///

if (/* shuffle */) {
shuffle($links);
} else {
usort($links, "sortbymtime");
}

foreach ($links as $link) {
array_unshift($link, RANDOMIMAGE_FORMAT);
call_user_func_array("printf", $link);
}

Link to comment
Share on other sites

<?php

define("RANDOMIMAGE_FORMAT", '<a href="%0$s"><img src="%1$s" class="randomImage" /></a> ');

// $a-$b=ascending order, $b-$a=descending order
function sortbymtime($a, $b) { return filemtime($a[0]) - filemtime($b[0]); }

$links = array(
array("../r/ahlens.html", "k/k.jpg"),
array("../r/brothers.html", "k/k.jpg"),
array("../r/hm.html", "k/k.jpg"),
array("../r/kappahl.html", "k/k.jpg"),
array("../r/mq.html", "k/k.jpg"),
array("sida.php", "k/l.jpg"),
array("../r/nk.html", "k/k.jpg")
);

///

if (/* shuffle */) {
shuffle($links);
} else {
usort($links, "sortbymtime");
}

foreach ($links as $link) {
array_unshift($link, RANDOMIMAGE_FORMAT);
call_user_func_array("printf", $link);
}

 

Wow that was some more code :) I tried it but got this error: Parse error: syntax error, unexpected ')' in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 57

 

I dont know much about php but do I really need all this code? The shuffle command doesnt need to be on the same page or anything. I already have that working on another page. What I meant was that it would have been nice if I could include a php file with the list of files on both the shuffle page and the date order page - but as the array lists are different that doesnt work.

 

 

Link to comment
Share on other sites

Wow that was some more code :) I tried it but got this error: Parse error: syntax error, unexpected ')' in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 57

Copy and paste all you want, but you'll still have to make a particular edit to the code I posted to make it work.

 

I dont know much about php but do I really need all this code? The shuffle command doesnt need to be on the same page or anything. I already have that working on another page. What I meant was that it would have been nice if I could include a php file with the list of files on both the shuffle page and the date order page - but as the array lists are different that doesnt work.

- What do you mean "all this code"? Not counting the array it's only a dozen lines of code, and you could collapse it down to half of that with a few seconds of work.

- Why do you have two pages to do pretty much the same thing?

- What array lists? What's different?

Link to comment
Share on other sites

Wow that was some more code :) I tried it but got this error: Parse error: syntax error, unexpected ')' in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 57

Copy and paste all you want, but you'll still have to make a particular edit to the code I posted to make it work.

 

I dont know much about php but do I really need all this code? The shuffle command doesnt need to be on the same page or anything. I already have that working on another page. What I meant was that it would have been nice if I could include a php file with the list of files on both the shuffle page and the date order page - but as the array lists are different that doesnt work.

- What do you mean "all this code"? Not counting the array it's only a dozen lines of code, and you could collapse it down to half of that with a few seconds of work.

- Why do you have two pages to do pretty much the same thing?

- What array lists? What's different?

 

Sorry for all this confusion.

 

The problem is that I dont know how to change the code to make it work. I need two pages. One with shuffle and one in latest updated order. It takes some time to explain why I need to have two different pages so if we ignore that I need and have a shuffle page that works and just focus on the latest modified order page.

 

If we back up to the part where you gave me this code that I tried and got this error:

Warning: Wrong parameter count for filemtime() in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 48

Why is the filemtime line giving me this error, and how do I fix it?

 

<?php
$links = Array();
$links["../r/ahlens.html"] = "k/k.jpg";
$links["../r/brothers.html"] = "k/k.jpg";
$links["../r/hm.html"] = "k/k.jpg";
$links["../r/kappahl.html"] = "k/k.jpg";
$links["../r/mq.html"] = "k/k.jpg";
$links["sida1.php"] = "k/l.jpg";
$links["../r/nk.html"] = "k/k.jpg";

uasort($links, "filemtime");

foreach($links as $file => $image){
    echo "<a href=\"$file\" ><img src=\"$image\" class=\"randomImage\" /></a> ";  
}  
?>

 

Thank you for bearing with me :)

Link to comment
Share on other sites

Wow that was some more code :) I tried it but got this error: Parse error: syntax error, unexpected ')' in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 57

Copy and paste all you want, but you'll still have to make a particular edit to the code I posted to make it work.

 

I dont know much about php but do I really need all this code? The shuffle command doesnt need to be on the same page or anything. I already have that working on another page. What I meant was that it would have been nice if I could include a php file with the list of files on both the shuffle page and the date order page - but as the array lists are different that doesnt work.

- What do you mean "all this code"? Not counting the array it's only a dozen lines of code, and you could collapse it down to half of that with a few seconds of work.

- Why do you have two pages to do pretty much the same thing?

- What array lists? What's different?

 

Sorry for all this confusion.

 

The problem is that I dont know how to change the code to make it work. I need two pages. One with shuffle and one in latest updated order. It takes some time to explain why I need to have two different pages so if we ignore that I need and have a shuffle page that works and just focus on the latest modified order page.

 

If we back up to the part where you gave me this code that I tried and got this error:

Warning: Wrong parameter count for filemtime() in /storage/content/79/129179/xn--trdet-hra.se/public_html/r/s.php on line 48

Why is the filemtime line giving me this error, and how do I fix it?

 

<?php
$links = Array();
$links["../r/ahlens.html"] = "k/k.jpg";
$links["../r/brothers.html"] = "k/k.jpg";
$links["../r/hm.html"] = "k/k.jpg";
$links["../r/kappahl.html"] = "k/k.jpg";
$links["../r/mq.html"] = "k/k.jpg";
$links["sida1.php"] = "k/l.jpg";
$links["../r/nk.html"] = "k/k.jpg";

uasort($links, "filemtime");

foreach($links as $file => $image){
    echo "<a href=\"$file\" ><img src=\"$image\" class=\"randomImage\" /></a> ";  
}  
?>

 

Thank you for bearing with me :)

 

Yes :D I solved it. Thank you! Sat with your code and thought a lot and finally I got it working. The problem was that I thought that thew script would sort by the photos modified date :) but it actually sorts by the files i am linking - which is better :D

 

<?php 
function cmd($a, $b)
{
  return filemtime($b[0]) - filemtime($a[0]); 
    }

$links = array(
array("../r/ahlens.html", "k/k.jpg"),
array("../r/brothers.html", "k/k.jpg"),
array("../r/hm.html", "k/k.jpg"),
array("../r/kappahl.html", "bilder/stam.jpg"),
array("../r/mq.html", "k/k.jpg"),
array("sida1.php", "k/l.jpg"),
array("../r/nk.html", "k/k.jpg"));

usort( $links, "cmd"); 

foreach($links as $link){
    echo "<a href=\"${link[0]}\" id=\"popup\"><img src=\"${link[1]}\" class=\"randomImage\" /></a> ";  
}  
?>

 

Thank you SO much for all your help :D

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.