Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/110230-solved-sort-images-from-2-directories/
Share on other sites

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' />";
}
}
?>

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!

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)

 

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.

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

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

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!

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.