array_slice页码 [英] array_slice page numbering

查看:106
本文介绍了array_slice页码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该脚本文件夹中的每个图像并显示在网页上。有没有一种方法,以显示像像1,2,3,4,5每10张左右的简单的页面数?因此,我想一切都远远不能正常工作。

 < PHP        #为prevent浏览器错误输出
        标题(内容类型:text / JavaScript的;字符集= UTF-8');
        #路径图像文件夹
        $ imagefolder ='IMG /';
        #只显示图像文件夹中这些文件类型
        $ imagetypes ='{。*。JPG格式,JPG *,* JPEG,PNG *,* PNG,GIF *,* GIF};
        #添加图像阵列
        $图像=水珠($ imagefolder $ imagetypes,GLOB_BRACE);
        #排序基于其最后修改时间戳图像
        $ sortedImages =阵列();
        $数= COUNT($图像);
        为($ I = 0; $ I< $计数; $ I ++){
            $ sortedImages [日期('YmdHis,filemtime($图像[$ i]))$ I] = $图像[$ i];
        }        #设置为假,如果你想最老的图像第一次出现
        $ newest_images_first = TRUE;
        在阵列#排序图像
        如果($ newest_images_first){
            krsort($ sortedImages);
        }其他{
            ksort($ sortedImages);
        }        #生成HTML输出
        的WriteHTML('< UL类=INS-IMGS>');
        的foreach($ sortedImages为$图像){
            #获取图像的名称,从图像文件夹路径和文件类型扩展名剥离
            $ NAME ='形象的名字:.substr($形象,strlen的($ imagefolder),strpos($形象,) - 的strlen($ imagefolder)'。');
            #获取最后修改时间戳,使其可读
            $ LAST_MODIFIED ='(最后修改:.date(F DÿH:I:S',filemtime($图片))');
            #开始加入
            的WriteHTML('<李班=INS-IMGS丽>');
            的WriteHTML('< D​​IV CLASS =INS-IMGS标签>'。$名称''。$ LAST_MODIFIED。'< / DIV>');
            的WriteHTML('< D​​IV CLASS =INS-IMGS-IMG><名称='。$形象的href =#。$形象。'>');
            的WriteHTML('< IMG SRC ='。$形象。'ALT ='$名字。'称号='$名字。'>');
            的WriteHTML('< / A>< / DIV>');
            的WriteHTML('< /李>');
        }
        的WriteHTML('< / UL>');
        的WriteHTML('<链接rel =stylesheet属性类型=文/ CSS的href =INS-imgs.css>');
        #转换为HTML JS
        功能的WriteHTML($ HTML){
            回声的document.write('$ HTML。'); \\ N的;        }    ?>


解决方案

既然你说你是不是一个 PHP 主我会告诉我的解决方案,并解释步骤一步。

我希望你能发现它有用。

下面是循环我使用的分页:

 为($ I =(($页面-1)* $ perPage); $ I<分(($页* $ perPage),$总计); $ I ++) {
    }

我将要去使用它来创建具有的元素 11-20 21-30 等。

一个新的数组

所有我移除了首先,$ I $ sortedImages 数组的索引( 15 您的code的行)

 为($ I = 0; $ I< $计数; $ I ++){
    $ sortedImages [日期('YmdHis,filemtime($图像[$ i]))] = $图像[$ i]; #第15行
}

,因为它使指数有点乱(这是下一步需要)。

然后,我创建一个新的数组 0到N 索引这使得code整洁(我这样做是为了改变你的code尽可能少),然后我来填充它与元素 $ sortedImages 阵列。

  $ k = 0; #新的索引
$ newArray =阵列(); #新的数组
的foreach($ sortedImages为$ soImg){
    $ newArray [$ K] = $ soImg;
    $ķ++;
}

最后分页的实现:

  $页= $ _GET [页];
$ perPage = 3;
$总额= $计数;为($ I =(($页面-1)* $ perPage); $ I&下;分钟(($页面* $ perPage),$总); $ I ++){
    $ newSortedImages [$ i] = $ newArray [$ i];
}

变量:


  • $页= $ _GET [页]; 是从URL retrived页码( $ _ GET [] 是一个全局数组

  • $ perPage 是元素的数量每页显示的

  • $总额= $计数; $图像数量阵列(13号线)

循环:


  • $ I =(($页面-1)* $ perPage)是循环的开始,如果页面的 1 的循环应该从 0 ,然后除权pression (($页面-1)* $ perPage)使得它的工作。
  • $ I<分钟(($页* $ perPage),$计)是循环的结束,的 MIN()函数找到它的参数之间的最低值,这是有益的,当如最后一页包含4个元素,同时6的预期。

然后你只需要通过在code的29排从 $ sortedImages 改变阵列循环 $ newSortedImages

有关分页控件使用这样的:

  $下一页= $页面+ 1;
$prevPage = $页面 - 1;

下面是新的code实现的:

 #为prevent浏览器错误输出
标题(内容类型:text / JavaScript的;字符集= UTF-8');
#路径图像文件夹
$ imagefolder ='IMG /';
#只显示图像文件夹中这些文件类型
$ imagetypes ='{。*。JPG格式,JPG *,* JPEG,PNG *,* PNG,GIF *,* GIF};
#添加图像阵列
$图像=水珠($ imagefolder $ imagetypes,GLOB_BRACE);
#排序基于其最后修改时间戳图像
$ sortedImages =阵列();
$数= COUNT($图像);
为($ I = 0; $ I< $计数; $ I ++){
    $ sortedImages [日期('YmdHis,filemtime($图像[$ i]))$ I] = $图像[$ i];
}#设置为假,如果你想最老的图像第一次出现
$ newest_images_first = TRUE;
在阵列#排序图像
如果($ newest_images_first){
    krsort($ sortedImages);
}其他{
    ksort($ sortedImages);
}#现在我给从0到N到新数组的索引,使其与分页循环工作
$ K ​​= 0; #新的索引
$ newArray =阵列(); #新的数组
的foreach($ sortedImages为$ soImg){
    $ newArray [$ K] = $ soImg;
    $ķ++;
}$页= $ _GET [页];
$ perPage = 3;
$总额= $计数;
为($ I =(($页面-1)* $ perPage); $ I&下;分钟(($页面* $ perPage),$总); $ I ++){
    $ newSortedImages [$ i] = $ newArray [$ i];
}#生成HTML输出
的WriteHTML('< UL类=INS-IMGS>');
的foreach($ newSortedImages为$图像){
    #获取图像的名称,从图像文件夹路径和文件类型扩展名剥离
    $ NAME ='形象的名字:.substr($形象,strlen的($ imagefolder),strpos($形象,) - 的strlen($ imagefolder)'。');
    #获取最后修改时间戳,使其可读
    $ LAST_MODIFIED ='(最后修改:.date(F DÿH:I:S',filemtime($图片))');
    #开始加入
    的WriteHTML('<李班=INS-IMGS丽>');
    的WriteHTML('< D​​IV CLASS =INS-IMGS标签>'。$名称''。$ LAST_MODIFIED。'< / DIV>');
    的WriteHTML('< D​​IV CLASS =INS-IMGS-IMG><名称='。$形象的href =#。$形象。'>');
    的WriteHTML('< IMG SRC ='。$形象。'ALT ='$名字。'称号='$名字。'>');
    的WriteHTML('< / A>< / DIV>');
    的WriteHTML('< /李>');
}
的WriteHTML('< / UL>');
的WriteHTML('<链接rel =stylesheet属性类型=文/ CSS的href =INS-imgs.css>');
#转换为HTML JS
功能的WriteHTML($ HTML){
    回声的document.write('$ HTML。'); \\ N的;
}

有关编号,你必须知道总的元素和 $ perPage 元素把它,显然其结果必然是一个整数,所以你要使用的页面< A HREF =htt​​p://php.net/manual/en/function.ceil.php相对=nofollow> CEIL()功能

php.net


  

在有需要四舍五入值返回下一个最高的整数。


  $页面= CEIL($计数/ $ perPage);

然后用它显示peges编号:

 为($ i = 1; $ I&LT; = $页; $ I ++){
    的WriteHTML('&LT; A HREF =?页='$我。'&GT;'$ I'&LT; / A&GT;');
}

This script takes every image in a folder and shows it on a webpage. Is there a way to show like an simple page number like 1,2,3,4,5 every 10 images or so? everything I tried thus far is not working.

   <?php

        # To prevent browser error output
        header('Content-Type: text/javascript; charset=UTF-8');
        # Path to image folder
        $imagefolder = 'img/';
        # Show only these file types in the image folder
        $imagetypes = '{*.jpg,*.JPG,*.JPEG,*.png,*.PNG,*.gif,*.GIF}';
        # Add images to array
        $images = glob($imagefolder.$imagetypes, GLOB_BRACE);
        # Sort the images based on its 'last modified' time stamp
        $sortedImages = array();
        $count = count($images);
        for ($i = 0; $i < $count; $i++) {
            $sortedImages[date ('YmdHis', filemtime($images[$i])).$i] = $images[$i];
        }

        # Set to 'false' if you want the oldest images to appear first
        $newest_images_first = true;
        # Sort images in array
        if($newest_images_first) {
            krsort($sortedImages);
        } else {
            ksort($sortedImages);
        }

        # Generate the HTML output
        writeHtml('<ul class="ins-imgs">');
        foreach ($sortedImages as $image) {
            # Get the name of the image, stripped from image folder path and file type extension
            $name = 'Image name: '.substr($image,strlen($imagefolder),strpos($image, '.')-strlen($imagefolder));
            # Get the 'last modified' time stamp, make it human readable
            $last_modified = '(last modified: '.date('F d Y H:i:s', filemtime($image)).')';
            # Begin adding
            writeHtml('<li class="ins-imgs-li">');
            writeHtml('<div class="ins-imgs-label">'.$name.' '.$last_modified.'</div>');
            writeHtml('<div class="ins-imgs-img"><a name="'.$image.'" href="#'.$image.'">');
            writeHtml('<img src="'.$image.'" alt="'. $name.'" title="'. $name.'">');
            writeHtml('</a></div>');
            writeHtml('</li>');
        }
        writeHtml('</ul>');
        writeHtml('<link rel="stylesheet" type="text/css" href="ins-imgs.css">');
        # Convert HTML to JS
        function writeHtml($html) {
            echo "document.write('".$html."');\n";



        }

    ?>

解决方案

Given that you said you are not a PHP master I'll show my solution and explain it step by step.
I hope you can find it helpful.

Here is the loop I use for pagination:

for($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
    }

I'll gonna use this to create a new array with the elements 11-20, 21-30 and so on.

First of all I removed the .$i in your $sortedImages array's index (15th row of your code)

for ($i = 0; $i < $count; $i++) {
    $sortedImages[date ('YmdHis', filemtime($images[$i]))] = $images[$i]; #15th Row
}

because it makes the index a bit messy (it is necessary for a next step).

Then I create a new array with 0 to N indexes which makes the code tidy (I do it to change your code as less as possible) and then I populate it with the elements of $sortedImages array.

$k = 0; # The new index
$newArray = array(); # The new array
foreach($sortedImages as $soImg) {
    $newArray[$k] = $soImg; 
    $k++;
}

Finally the pagination's implementation:

$page = $_GET["page"];
$perPage = 3;
$total = $count;

for ($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
    $newSortedImages[$i] = $newArray[$i];
}

Variables:

  • $page = $_GET["page"]; is the page number retrived from the url ($_GET[] is a superglobal array)
  • $perPage is the number of elements to show per page
  • $total = $count; is the number of $images array (13th line)

The loop:

  • $i = (($page-1)*$perPage) is the start of the loop, if the page is 1 the loop should start from 0 then the expression (($page-1)*$perPage) makes it work.
  • $i < min(($page*$perPage), $total) is the end of the loop, min() function finds the lowest value between its arguments, it is helpful when e.g. the last page contains 4 elements while 6 are expected.

Then you just need to change the array to loop through in 29th row of your code from $sortedImages to $newSortedImages.

For pagination controls use this:

$nextPage = $page + 1;
$prevPage = $page - 1;

Here is the new code implementation:

# To prevent browser error output
header('Content-Type: text/javascript; charset=UTF-8');
# Path to image folder
$imagefolder = 'img/';
# Show only these file types in the image folder
$imagetypes = '{*.jpg,*.JPG,*.JPEG,*.png,*.PNG,*.gif,*.GIF}';
# Add images to array
$images = glob($imagefolder.$imagetypes, GLOB_BRACE);
# Sort the images based on its 'last modified' time stamp
$sortedImages = array();
$count = count($images);
for ($i = 0; $i < $count; $i++) {
    $sortedImages[date ('YmdHis', filemtime($images[$i])).$i] = $images[$i];
}

# Set to 'false' if you want the oldest images to appear first
$newest_images_first = true;
# Sort images in array
if($newest_images_first) {
    krsort($sortedImages);
} else {
    ksort($sortedImages);
}

# Now I give an index from 0 to N to the new array to make it work with pagination loop
$k = 0; # The new index
$newArray = array(); # The new array
foreach($sortedImages as $soImg) {
    $newArray[$k] = $soImg;
    $k++;
}

$page = $_GET["page"];
$perPage = 3;
$total = $count;
for ($i = (($page-1)*$perPage); $i < min(($page*$perPage), $total); $i++) {
    $newSortedImages[$i] = $newArray[$i];
}

# Generate the HTML output
writeHtml('<ul class="ins-imgs">');
foreach ($newSortedImages as $image) {
    # Get the name of the image, stripped from image folder path and file type extension
    $name = 'Image name: '.substr($image,strlen($imagefolder),strpos($image, '.')-strlen($imagefolder));
    # Get the 'last modified' time stamp, make it human readable
    $last_modified = '(last modified: '.date('F d Y H:i:s', filemtime($image)).')';
    # Begin adding
    writeHtml('<li class="ins-imgs-li">');
    writeHtml('<div class="ins-imgs-label">'.$name.' '.$last_modified.'</div>');
    writeHtml('<div class="ins-imgs-img"><a name="'.$image.'" href="#'.$image.'">');
    writeHtml('<img src="'.$image.'" alt="'. $name.'" title="'. $name.'">');
    writeHtml('</a></div>');
    writeHtml('</li>');
}
writeHtml('</ul>');
writeHtml('<link rel="stylesheet" type="text/css" href="ins-imgs.css">');
# Convert HTML to JS
function writeHtml($html) {
    echo "document.write('".$html."');\n";
}

For page numbering you have to know the total of elements and divide it by $perPage elements, obviously the result must be an integer, so you'll gonna use ceil() function

From php.net

Returns the next highest integer value by rounding up value if necessary.

$pages = ceil($count / $perPage);

And then use this to display peges numbers:

for($i = 1; $i <= $pages; $i++) {
    writeHtml('<a href="?page=' . $i . '">' . $i . '</a> ');
}

这篇关于array_slice页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆