dqrb4228 2012-05-24 12:41
浏览 15
已采纳

将Paging添加到PHP / mySQL列表中

I have a php/mysql list, where I can display lots of thumbnails.

My problem is that there are too many images to display in one page. I would like to divide it so that I can get some page links or something at the bottom?

Like: 1,2,3,4,5,6,7,8 or next and prev...

Here is the current code:

<?php
    if ($db_found) {
        $SQL = "SELECT * FROM myTable";
        $result = mysql_query($SQL);

        while ($db_field = mysql_fetch_assoc($result)) {
            echo '<li>';
            echo '<div class="thumb"><img src="images/thumbnails/'.$db_field['image'].'" alt="" /></a></div>';
            echo '</li>';
        }
        mysql_close($db_handle);
    }
?>

How can I add some page links here?

  • 写回答

6条回答 默认 最新

  • dp19001 2012-05-24 13:00
    关注

    it's much more easy with Jquery but i Recommends you to work with PHP for start, It can only develop your head than to add some JQuery code will get you everything. anyway simple paging with PHP goes like that, the key's for learning this code is to understand: intval,LIMIT,mysql_num_rows,mysql_fetch_assoc,mysql_fetch_row,ceil,COUNT after you will learn each off them the code will be much easy to understand and you can develop it and add some of your own Improvements. The Credit for this Excellent site : PHPGuide

     $per_page = 5; 
     $page = 1;
    
     if (isset($_GET['page'])) 
     {
      $page = intval($_GET['page']); 
      if($page < 1) $page = 1;
    }
    
    
     $start_from = ($page - 1) * $per_page; 
    
     mysql_connect(..);
     mysql_select_db(..);
    
     $current_items = mysql_query( "SELECT * FROM `test` LIMIT $start_from, $per_page");
     if( mysql_num_rows($current_items) > 0)
     {
      while($item = mysql_fetch_assoc($current_items))
      {
        echo $item['text'], '<br/>';
      }
     }
     else
     {
      echo 'this page does not exists'; 
     }
    
    
     $total_rows = mysql_query("SELECT COUNT(*) FROM `test`");
     $total_rows = mysql_fetch_row($total_rows);
     $total_rows = $total_rows[0];
    
     $total_pages = $total_rows / $per_page;
     $total_pages = ceil($total_pages); # 19/5 = 3.8 ~=~ 4
    
     for($i = 1; $i  <= $total_pages; ++$i)
     {
      echo "<a href='?page=$i'>$i</a> &nbsp;&nbsp;";
     }
    

    The Code is Very Easy to understand and very simple. and about the prev and forward page it's one of the improvements you will do after you get this code.

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

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?