This is my block of PHP.
<?php
I know that this is where the array is defined.
$string =array();
$dir = opendir($filePaththumb);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
I assume I should use natsort() before the code continues beyond this point.
echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>";
$loop = "0";
while (sizeof($string) != 0){
$img = array_pop($string);
Can I use natsort() here?
echo "<center><a href='$filePath$img' download='$filePath$img' target='$target_thumb'><img src='$filePaththumb$img' border='0' width='100%'/><BR><IMG src='img/download.png'></a><BR><BR><BR><BR></center>";
$loop = $loop + 1;
if ($loop == $loop_end) {
echo "<br>";
$loop = "0";
}
}
?>
How can I sort images in natural order?
after you have created your $string[] array you can now sort it.
It will be sorted in place i.e. you don't have to assign a result to another variable, the natsort() function will return true or false (on failure).
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
}
}
//print_r($string);
natsort($string);
//print_r($string);
// then display them in order;
foreach ($string as $img){
echo "<img ...";
...
}
Thanks everyone for you input! Your suggestions helped me greatly!
<?php
$string =array();
$dir = opendir($filePaththumb);
while ($file = readdir($dir)) {
if (eregi("\.png",$file) || eregi("\.jpg",$file) || eregi("\.gif",$file) ) {
$string[] = $file;
After the Array has been filled with .jpg/.gif/.png files, I used
natsort($string);
to arrange the image files in the $string array in Natural Order
I also used array_reverse($string);
to arrange the order of the image files from filename of greatest Natural Order value to descending Natural Order of value.
}
}
echo "<b><font size='$font_size'>".$gallery_name."</font></b><br>";
$loop = "0";
while (sizeof($string) != 0){
$img = array_pop($string);
echo "<center><a href='$filePath$img' download='$filePath$img' target='$target_thumb'><img src='$filePaththumb$img' border='0' width='100%'/><BR><IMG src='img/download.png'></a><BR><BR><BR><BR></center>";
$loop = $loop + 1;
if ($loop == $loop_end) {
echo "<br>";
$loop = "0";
}
}
?>
Thanks again everyone!