dstwfcz1377 2012-07-01 11:53
浏览 97
已采纳

PHP分页400多张图片

a have a website with more than 400 pictures in a directory. I'd like to list them by 12 on every page. How can i do that? Here is my actual code:

 <!doctype html>
<html lang="hu">
    <head>
        <title>Amatőr</title>
        <meta charset="utf-8"/>
    </head>
    <body>
        <h2><a href="../php/upload_picture.php" style="font-size:15pt; color:#ff00e8; text-decoration: none;">Vannak jó képeid? Töltsd fel őket és kikerülhetnek az oldalra!</a></h2>
        <article>
            <header>
                Amatőr Lányok
            </header>
            <div id="kepek">
                <?php
                $imgdir = '../img/blog/img/amator/'; //Pick your folder
                $allowed_types = array('png','jpg','jpeg','gif'); //Allowed types of files
                $dimg = opendir($imgdir);//Open directory
                while($imgfile = readdir($dimg))
                {
                  if( in_array(strtolower(substr($imgfile,-3)),$allowed_types) OR
                      in_array(strtolower(substr($imgfile,-4)),$allowed_types) )
                /*If the file is an image add it to the array*/
                  {$a_img[] = $imgfile;}
                }


                 $totimg = count($a_img);  //The total count of all the images
                //Echo out the images and their paths incased in an li.
                 for($x=0; $x < $totimg; $x++){ echo "<a onclick='Lightbox.start(this, false, false, false, false); return false;' rel='lightbox[amator]' href='" . $imgdir . $a_img[$x] . "'><img class='kep_listaz' width='200px' height='160px' src='" . $imgdir . $a_img[$x] . "' /></a>";}
                ?>
            </div>
        </article>
    </body>
</html>

Thanks!
  • 写回答

2条回答 默认 最新

  • ds000001 2012-07-01 12:12
    关注

    So this is really more of a maths questions. You will need to get the total count of the images, then divide that by the max number of images per page (make sure to ceil() it).

    You have now a max number of pages needed to view all of these images. What you need to do now is figure whether you want page=1/page=2 etc or as a start and end. Both are relatively easy, however, with the page you'd need to do

    $page = (int)$_GET['page'];
    $start = $page * $max_items_per_page;
    $end = $start + $max_items_per_page;
    

    This way it's probably saver. Also add extra code to make sure you're not out of bounds with the page requested.

    Now it's a matter of getting an array of the files (I suggest you use glob()) and use array_slice() that from start to end.

    Finally just have a previous/next page, or list all (or some) of the pages. Getting next and previous is as simple as adding/removing 1 to $page, i.e. $next = $page + 1; and $prev = $page-1;. Again, for both of these, double check that you're not out of bounds. Probably best not to show next/previous if they are out of bounds.

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog