我正在使用以下功能来读取目录并将图像显示为图库,效果非常好,但我需要显示按日期排序的图像,从较新到较旧。
知道如何使用此方法执行此操作?
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir("saved/2013") ) {
$lightbox = rand();
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('saved/2013') ) {
mkdir('saved/2013');
}
$strFileName = "saved/2013/".$file;
echo '<div id="imagen-t">';
echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p><a href="saved/2013/'.$file.'" rel="lightbox['.$lightbox.']"><img src=saved/2013/'.$file.' alt="" width="220" /></a></p>';
echo '</div>';
$count++;
}
}
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}
结果如下: http://www.espigoplatja.com/whale/galeria/
拜, 贡萨洛
答案 0 :(得分:0)
你看过USort:http://php.net/manual/en/function.usort.php 它允许您使用用户定义的比较函数按值对数组进行排序。
示例
<?php
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$a=array(4,2,8,6);
usort($a,"my_sort");
$arrlength=count($a);
for($x=0;$x<$arrlength;$x++)
{
echo $a[$x];
echo "<br>";
}
?>
答案 1 :(得分:0)
顺便说一下,我尝试实现您提供的示例,并显示所有图像的相同日期:
# SETTINGS
$per_page = 6;
$page = $_GET['page'];
$has_previous = false;
$has_next = false;
function getPictures() {
global $page, $per_page, $has_previous, $has_next;
if ( $handle = opendir("saved/2013") ) {
$lightbox = rand();
$count = 0;
$skip = $page * $per_page;
if ( $skip != 0 )
$has_previous = true;
while ( $count < $skip && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' )
$count++;
}
function my_sort($a,$b)
{
if ($a==$b) return 0;
return ($a<$b)?-1:1;
}
$count = 0;
while ( $count < $per_page && ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
if ( ! is_dir('saved/2013') ) {
mkdir('saved/2013');
}
$a=array($file);
usort($a,"my_sort");
//$strFileName = "saved/2013/".$file;
$strFileName = "saved/2013/".$a[$x];
echo '<div id="imagen-t">';
//echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p>' .date( "D d M Y g:i A", filemtime($strFileName)) . "</p>";
echo '<p><a href="saved/2013/'.$file.'" rel="lightbox['.$lightbox.']"><img src=saved/2013/'.$file.' alt="" width="220" /></a></p>';
echo '</div>';
$count++;
}
}
while ( ($file = readdir($handle)) !== false ) {
if ( !is_dir($file) && ($type = getPictureType($file)) != '' ) {
$has_next = true;
break;
}
}
}
}