【问题标题】:PHP glob return array with pagination [duplicate]带有分页的PHP glob返回数组[重复]
【发布时间】:2014-02-20 16:36:36
【问题描述】:

我正在使用 php glob 在文件夹中搜索与文件名匹配的图像并将它们以数组的形式返回。我不确定如何设置退货限制并包括分页。

我目前正在使用以下内容。

    $userpix = $this->_vars['user_username'];

    foreach (glob("./modules/user_gallery/data/{$userpix}_*.*") as $thumb) {
    $resize = preg_replace("/{$userpix}_/", "resize_", $thumb);

    $newstring = substr($thumb, 28);

    echo "<DIV style='float:left;padding:4px '><a href='$thumb' rel='lightbox'><img class='dropshadow' src='./modules/user_gallery/thumbs/grab.php?src=$thumb' width='150' height='100'></a><br /></div>";

    }

【问题讨论】:

    标签: php foreach thumbnails glob pagination


    【解决方案1】:

    您需要添加一些额外的代码(从查询中传递页面参数 + 计算要显示的最小/最大图像 id),即:

    $userpix = $this->_vars['user_username'];
    
    $page = min(1, (int)$_GET['page']);
    $pageSize = 10;
    $minId = ($page - 1) * $pageSize;
    $maxId = $page * $pageSize - 1;
    
    
    foreach (glob("./modules/user_gallery/data/{$userpix}_*.*") as $id = $thumb) {
        if ($id < $minId || $id > $maxId) {
           continue;
        }
        $resize = preg_replace("/{$userpix}_/", "resize_", $thumb);
    
        $newstring = substr($thumb, 28);
    
    echo "<DIV style='float:left;padding:4px '><a href='$thumb' rel='lightbox'><img class='dropshadow' src='./modules/user_gallery/thumbs/grab.php?src=$thumb' width='150' height='100'></a><br /></div>";
    
    }
    

    【讨论】:

    • 非常感谢您的回复 zoillek 但我似乎对您示例中的 foreach 有问题。它最终什么也没返回。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2019-12-08
    • 1970-01-01
    相关资源
    最近更新 更多