I want to be able to sort and display the images by saved date. My code isn't sorting the images? I want the newest images first...
我希望能够按保存的日期对图像进行排序和显示。我的代码没有对图像进行排序?我想要最新的图像......
function my_sort($a,$b){
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
if( isset($_POST['submit']) ){
$folder = htmlentities($_POST['val1']);
}
if( isset($folder) ) {
$filetype = '*.*';
$filex = glob($folder.$filetype);
$count = count($filex);
$sortedArray = array();
for ($i = 0; $i < $count; $i++) {
$sortedArray($filex[$i]);
}
$div= '';
usort($sortedArray,'my_sort');
foreach (glob($folder.$filetype) as $files) {
$div .= '<div class="typeS">';
$div .= '<li><div class="itemTypes"><input type="image" src="'.$files.'"/><gt_descA>"'.substr($files,strlen($folder),strpos($files, '.') - strlen($folder)).'"</gt_descA></div></li>';
$div .= '</div>';
}
echo $div;
}
2 个解决方案
#1
1
In your foreach
, you're doing a glob
again, rather than looping over your sorted array. Change it to foreach ($sortedArray as $files) {
在你的foreach中,你再次做一个glob,而不是循环遍历你的排序数组。将其更改为foreach($ sortedArray为$ files){
#2
0
I made five changes to your code 1. your array was being populated using parentheses. I changed it to populate using square brackets. 2. your array had no index to reflect the foreach loop. I added an index. 3. you were sorting by the file name. I changed the array to have a last saved date index, and sorted by last saved date 4. after sorting, your final foreach loop used the original unsorted array. I replaced the original array with your sorted array. 5. you had one too many closing braces. I removed the closing brace on the last line.
我对您的代码进行了五处更改1.您的数组正在使用括号填充。我改为使用方括号填充。 2.你的数组没有反映foreach循环的索引。我添加了一个索引。 3.您按文件名排序。我将数组更改为具有最后保存的日期索引,并按上次保存的日期排序4.排序后,最终的foreach循环使用原始未排序的数组。我用你的排序数组替换了原始数组。你有一个太多的关闭括号。我在最后一行删除了右括号。
function my_sort($a,$b){
if ($a['filesavetime']==$b['filesavetime']) return 0;
return ($a['filesavetime']<$b['filesavetime'])?-1:1;
}
if( isset($_POST['submit']) ){
$folder = htmlentities($_POST['val1']);
}
if( isset($folder) ) {
$filetype = '*.*';
$filex = glob($folder.$filetype);
$count = count($filex);
$sortedArray = array();
for ($i = 0; $i < $count; $i++) {
$sortedArray[$i]['filename'] = $filex[$i];
$sortedArray[$i]['filesavetime'] = filemtime($filex[$i]);
}
$div= '';
usort($sortedArray,'my_sort');
foreach ($sortedArray as $file) {
$div .= '<div class="typeS">';
$div .= '<li><div class="itemTypes"><input type="image" src="'.$file['filename'].'"/><gt_descA>"'.substr($file['filename'],strlen($folder),strpos($file['filename'], '.') - strlen($folder)).'"</gt_descA></div></li>';
$div .= '</div>';
}
echo $div;
#1
1
In your foreach
, you're doing a glob
again, rather than looping over your sorted array. Change it to foreach ($sortedArray as $files) {
在你的foreach中,你再次做一个glob,而不是循环遍历你的排序数组。将其更改为foreach($ sortedArray为$ files){
#2
0
I made five changes to your code 1. your array was being populated using parentheses. I changed it to populate using square brackets. 2. your array had no index to reflect the foreach loop. I added an index. 3. you were sorting by the file name. I changed the array to have a last saved date index, and sorted by last saved date 4. after sorting, your final foreach loop used the original unsorted array. I replaced the original array with your sorted array. 5. you had one too many closing braces. I removed the closing brace on the last line.
我对您的代码进行了五处更改1.您的数组正在使用括号填充。我改为使用方括号填充。 2.你的数组没有反映foreach循环的索引。我添加了一个索引。 3.您按文件名排序。我将数组更改为具有最后保存的日期索引,并按上次保存的日期排序4.排序后,最终的foreach循环使用原始未排序的数组。我用你的排序数组替换了原始数组。你有一个太多的关闭括号。我在最后一行删除了右括号。
function my_sort($a,$b){
if ($a['filesavetime']==$b['filesavetime']) return 0;
return ($a['filesavetime']<$b['filesavetime'])?-1:1;
}
if( isset($_POST['submit']) ){
$folder = htmlentities($_POST['val1']);
}
if( isset($folder) ) {
$filetype = '*.*';
$filex = glob($folder.$filetype);
$count = count($filex);
$sortedArray = array();
for ($i = 0; $i < $count; $i++) {
$sortedArray[$i]['filename'] = $filex[$i];
$sortedArray[$i]['filesavetime'] = filemtime($filex[$i]);
}
$div= '';
usort($sortedArray,'my_sort');
foreach ($sortedArray as $file) {
$div .= '<div class="typeS">';
$div .= '<li><div class="itemTypes"><input type="image" src="'.$file['filename'].'"/><gt_descA>"'.substr($file['filename'],strlen($folder),strpos($file['filename'], '.') - strlen($folder)).'"</gt_descA></div></li>';
$div .= '</div>';
}
echo $div;