02simond Posted May 3, 2011 Share Posted May 3, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/ Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 - 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? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1209935 Share on other sites More sharing options...
02simond Posted May 3, 2011 Author Share Posted May 3, 2011 - 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 Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1209951 Share on other sites More sharing options...
requinix Posted May 3, 2011 Share Posted May 3, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1209969 Share on other sites More sharing options...
02simond Posted May 4, 2011 Author Share Posted May 4, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1210281 Share on other sites More sharing options...
02simond Posted May 4, 2011 Author Share Posted May 4, 2011 I think I can solve my problem if I only can change the shuffle command to a command that lists the photos based on their creation date. If you could help me write that part I think I will solve the rest Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1210287 Share on other sites More sharing options...
requinix Posted May 4, 2011 Share Posted May 4, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1210554 Share on other sites More sharing options...
02simond Posted May 5, 2011 Author Share Posted May 5, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1210807 Share on other sites More sharing options...
requinix Posted May 5, 2011 Share Posted May 5, 2011 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"); Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1211103 Share on other sites More sharing options...
02simond Posted May 6, 2011 Author Share Posted May 6, 2011 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1211335 Share on other sites More sharing options...
requinix Posted May 6, 2011 Share Posted May 6, 2011 What's your code now? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1211634 Share on other sites More sharing options...
02simond Posted May 8, 2011 Author Share Posted May 8, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1212247 Share on other sites More sharing options...
requinix Posted May 8, 2011 Share Posted May 8, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1212290 Share on other sites More sharing options...
02simond Posted May 9, 2011 Author Share Posted May 9, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1212701 Share on other sites More sharing options...
requinix Posted May 9, 2011 Share Posted May 9, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1213004 Share on other sites More sharing options...
02simond Posted May 10, 2011 Author Share Posted May 10, 2011 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1213245 Share on other sites More sharing options...
requinix Posted May 10, 2011 Share Posted May 10, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1213420 Share on other sites More sharing options...
02simond Posted May 11, 2011 Author Share Posted May 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1213706 Share on other sites More sharing options...
02simond Posted May 11, 2011 Author Share Posted May 11, 2011 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 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 <?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 Quote Link to comment https://forums.phpfreaks.com/topic/235418-sort-image-gallery-with-individual-links-by-file-name/#findComment-1213838 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.