duanfoumi5620 2013-04-05 21:56
浏览 12
已采纳

PHP - 如何按订单搜索文件夹图像

I have the following code to search the folder /images/ for images and echo them. However, it displays the images from a random order everytime I refresh the page. The images are named 1, 2, 3, 4 and so on. Any way to make it so that the last number (ex: 4) is the first one being displayed and so on?

<?php
    $dirname = "images";
    $images = scandir($dirname);
    shuffle($images);
    $ignore = Array(".", "..");
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
        }
    }               
?>

Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dongshai2022 2013-04-05 21:58
    关注

    This is due to your shuffle. You are randomizing your array. Let me introduce you to: http://php.net/manual/en/function.array-reverse.php which is

    <?php
        $dirname = "images";
        $images = scandir($dirname);
        $images = arsort(array_reverse($images, true));
        $ignore = Array(".", "..");
        foreach($images as $curimg){
            if(!in_array($curimg, $ignore)) {
                echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
            }
        }               
    ?>
    

    Update:

    $dirname = "Images";
    $images = scandir($dirname);
    sort($images,SORT_NUMERIC);
    krsort($images);
    $ignore = Array(".", "..");
    foreach($images as $curimg){
        if(!in_array($curimg, $ignore)) {
            echo "<img src=\"". $dirname . '/' . $curimg ."\"> 
    " ;
        }
    }
    

    What I have been working with:

    Without the sort(); and krsort(); i return:

    <img src="Images/1.png"> 
    <img src="Images/10.png"> 
    <img src="Images/11.png"> 
    <img src="Images/2.png"> 
    <img src="Images/3.png"> 
    <img src="Images/4.png"> 
    <img src="Images/5.png"> 
    <img src="Images/6.png"> 
    <img src="Images/7.png"> 
    <img src="Images/8.png"> 
    <img src="Images/9.png"> 
    

    With the krsort and sort.. I return:

    <img src="Images/11.png"> 
    <img src="Images/10.png"> 
    <img src="Images/9.png"> 
    <img src="Images/8.png"> 
    <img src="Images/7.png"> 
    <img src="Images/6.png"> 
    <img src="Images/5.png"> 
    <img src="Images/4.png"> 
    <img src="Images/3.png"> 
    <img src="Images/2.png"> 
    <img src="Images/1.png"> 
    

    Which I presume is what you are looking for.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源