alvinrow Posted June 14, 2008 Share Posted June 14, 2008 Hi - This is going to be a really easy fix I'm sure, so apologies for my inexperience, but I'm trying to pull a large amount of images from 2 directories, and then sort them in alphabetical order on a page. It's a real basic thing - it's just for me to look at as a reference tool, so it doesn't need to have any special formatting - just a great big block of pictures in alphabetical order (according to file name, obviously). I'm currently doing the following, which I know is really bloated, but as I said, this is just for me to use: <?php $dirname = "a1/"; $images = scandir($dirname); $ignore = Array(".", "..", "index.php"); foreach($images as $curimg){ if(!in_array($curimg, $ignore)) { echo "<img src='a1/$curimg' alt='a1' />"; }; } $dirname2 = "a2/"; $images = scandir($dirname2); $ignore = Array(".", "..", "index.php"); foreach($images as $curimg){ if(!in_array($curimg, $ignore)) { echo "<img src='a2/$curimg' alt='a2' />"; }; } ?> However, doing this obviously doesn't sort them as one list, but rather just places the a2/ images after the a1/ images are echoed. Can anyone just give me a quick & dirty fix to make them sort as one block? Too cut a long story short: I want to display all images from two directories in alphabetical order. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/ Share on other sites More sharing options...
sasa Posted June 14, 2008 Share Posted June 14, 2008 try <?php $out = array(); $dirname = "a1/"; $images = scandir($dirname); $ignore = Array(".", "..", "index.php"); foreach($images as $curimg){ if(!in_array($curimg, $ignore)) { //echo "<img src='a1/$curimg' alt='a1' />"; $out[$curimg][] = $dirname; }; } $dirname2 = "a2/"; $images = scandir($dirname2); $ignore = Array(".", "..", "index.php"); foreach($images as $curimg){ if(!in_array($curimg, $ignore)) { //echo "<img src='a2/$curimg' alt='a2' />"; $out[$curimg][] = $dirname2; }; } ksort($out); foreach ($out as $curimg => $d){ foreach ($d as $dirname){ echo "<img src='$dirname/$curimg' alt='$dirname' />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-565647 Share on other sites More sharing options...
alvinrow Posted June 15, 2008 Author Share Posted June 15, 2008 Sasa - that worked perfectly, thanks very much! Just out of curiosity, how does this line: echo "<img src='$dirname/$curimg' alt='$dirname' />"; Still output the images from a2/, even though that is specified as $dirname2 previously and this only requests $dirname? I'm very slowly learning PHP, so sorry if this is a dumb question. Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-565877 Share on other sites More sharing options...
kenrbnsn Posted June 15, 2008 Share Posted June 15, 2008 You can do this very simply with the use of the glob() function: <?php $pics = array(); foreach (glob('a*/*.jpg') as $fn) { list($d,$f) = explode($fn); $pics[$d] = $f; } asort($pics); foreach ($pics as $d => $fn) echo "<img src='$d/$fn alt='$dirname' />"; ?> Ken (note untested) Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-565882 Share on other sites More sharing options...
alvinrow Posted June 15, 2008 Author Share Posted June 15, 2008 You can do this very simply with the use of the glob() function: <?php $pics = array(); foreach (glob('a*/*.jpg') as $fn) { list($d,$f) = explode($fn); $pics[$d] = $f; } asort($pics); foreach ($pics as $d => $fn) echo "<img src='$d/$fn alt='$dirname' />"; ?> Ken (note untested) Hi Ken - I just tried running this and it kicked out the following error around 40 times, and at the bottom was a broken image link: Warning: Wrong parameter count for explode() in [...] (where [...] is the path to the site) The line it says is causing the error is: list($d,$f) = explode($fn); Any thoughts? Thanks for the advice too by the way. Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-565888 Share on other sites More sharing options...
sasa Posted June 15, 2008 Share Posted June 15, 2008 Sasa - that worked perfectly, thanks very much! Just out of curiosity, how does this line: echo "<img src='$dirname/$curimg' alt='$dirname' />"; Still output the images from a2/, even though that is specified as $dirname2 previously and this only requests $dirname? I'm very slowly learning PHP, so sorry if this is a dumb question. Thank you again! 1st I try to kreate array witk key = image name and value = image dir, sort this array with key (ksort) and utput key and value of sorted array, but if exist image with same name in both dirs the 2nd one replace 1st. I change my array that have key = image name and value = array of dirs. in 1st foeach i exstract key in variable $curimg an array of dirs in varijable $d in 2nd foreach i extract value(s) of $d in variable $dirname tray to print_r($out) if don't understund Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-565929 Share on other sites More sharing options...
kenrbnsn Posted June 16, 2008 Share Posted June 16, 2008 Here's my fixed code -- I just had some minor typos and bugs in the first version -- like I said, I didn't test it. This version I tested: <?php $pics = array(); foreach (glob('a*/*.jpg') as $fn) { list($d,$f) = explode('/',$fn); $pics[$f] = $d; } ksort($pics); foreach ($pics as $fn => $d) echo "<img src='$d/$fn' alt='$dirname' />"; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-566228 Share on other sites More sharing options...
alvinrow Posted June 16, 2008 Author Share Posted June 16, 2008 Here's my fixed code -- I just had some minor typos and bugs in the first version -- like I said, I didn't test it. This version I tested: <?php $pics = array(); foreach (glob('a*/*.jpg') as $fn) { list($d,$f) = explode('/',$fn); $pics[$f] = $d; } ksort($pics); foreach ($pics as $fn => $d) echo "<img src='$d/$fn' alt='$dirname' />"; ?> Ken Excellent, thank you. Really appreciate the help from you all! Quote Link to comment https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/#findComment-566857 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.