从加载同一目录中所有图像的php文件在一个页面中加载20个图像
我在一个目录中有100多个图像,我使用下面的php代码生成一个页面,在上面显示最新的图片从加载同一目录中所有图像的php文件在一个页面中加载20个图像,php,html,Php,Html,我在一个目录中有100多个图像,我使用下面的php代码生成一个页面,在上面显示最新的图片 <?php function mtimecmp($a, $b) { $mt_a = filemtime($a); $mt_b = filemtime($b); if ($mt_a == $mt_b) return 0; else if ($mt_a < $mt_b) return -1; else retu
<?php
function mtimecmp($a, $b) {
$mt_a = filemtime($a);
$mt_b = filemtime($b);
if ($mt_a == $mt_b)
return 0;
else if ($mt_a < $mt_b)
return -1;
else
return 1;
}
$images = glob($dirname."*.jpg");
usort($images, "mtimecmp");
for ($i = count($images) - 1; $i >= 0; $i--) {
$image = $images[$i];
echo '<img src="'.$image.'" height ="400"/><br />';
}
我想做的是生成一个新页面(html或php),显示最后20个页面,然后给用户一个选项来加载更多图像。这样,当他们访问页面时,他们不必加载所有100多张图片,只需加载20张
谢谢您的帮助。一般来说,任何涉及到加载后修改页面的操作都是使用php以外的工具完成的。我使用javascript,对于您尝试做的事情,我将使用JQuery。使用JQuery,它看起来像这样
<a id='load_twenty_button'>Load 20 more!</a>
<div id='where_to_put_the_images'></div>
<script>
var img_start = 20;
//when link is clicked, do the function
$('#load_twenty_button').click( function() {
var link = 'my_site/form_actions/getImages.php'
$.post(link
, { start: img_start
, end: img_start +20
}
, function (result)
{
//set the html of the div element to the html your php link return
$('#where_to_put_the_images').html(result);
img_start += 20;
}
);
});
</script>
再加载20个!
var img_start=20;
//单击链接时,执行该功能
$(“#加载按钮”)。单击(函数(){
var link='my_site/form_actions/getImages.php'
$.post(链接
,{start:img_start
,结束:img_开始+20
}
,函数(结果)
{
//将div元素的html设置为php链接返回的html
$(“#其中_to_放置_图像”).html(结果);
img_启动+=20;
}
);
});
然后,对于getIMages.php,使用$\u POST['start']和$\u POST['end']确定要在html中回显哪些图像。任何回显都将发布到div元素“where\u to\u put\u images”。如果你想在那之后再加上20个,你就得稍微做一点,但这应该能让你达到目的
另外,确保链接JQuery。只需查看一个基本的JQuery示例,它将在顶部链接。这是一个非常简单的想法,只需参考我的代码,就可以在这里使用array\u slice
函数
<?php
function mtimecmp( $a, $b ) {
$mt_a = filemtime( $a );
$mt_b = filemtime( $b );
if ( $mt_a == $mt_b ) {
return 0;
}
elseif ( $mt_a < $mt_b ) {
return -1;
}
return 1;
}
$images = glob( $dirname."*.jpg" );
usort( $images, "mtimecmp" );
$page = ( isset( $_GET["page"] ) ) ? $_GET["page"] : 1; // current page
$offset = ( $page * 20 ); // array_slice offset
$length = 20; // maximum images per page
$images = array_slice( $images, -$offset, $length ); // slice images array
if ( !empty( $images ) ) {
foreach( $images as $image ) {
echo '<img src="'.$image.'" height="400" /> ';
}
}
?>
对于分页,html看起来像这样,这是一个非常简单的方法
<div>
<a href="?page=<?php echo ( $page - 1 ); ?>">Prev</a>
<a href="?page=<?php echo ( $page + 1 ); ?>">Next</a>
</div>
您正在寻找分页或AJAX请求!谷歌那些