首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php分页显示10个项目

使用php分页显示10个项目
EN

Stack Overflow用户
提问于 2014-06-30 16:38:10
回答 1查看 1.2K关注 0票数 0

我在数据库中有超过100000个数据,当我试图在前端显示所有数据时,加载所有数据需要5分钟以上,所以我决定使用分页。

这里我试着显示每页10条记录,我成功地显示了前10条记录,分页看起来像这样的(? previous123456789...1235012351next ?),但如果我检查下一页或选择分页中的任何页面,它刷新页面,它通过url http://localhost/var/cms/page.php?page=2中的选定页面,它不显示下10个数据,它总是显示前10个数据。

下面是我的代码,任何人都可以告诉我如何在下一页点击时显示下10个数据,谢谢。

完整代码:

代码语言:javascript
复制
<?php

$dbHost = 'localhost'; // usually localhost
$dbUsername = 'root';
$dbPassword = '1234fedf';
$dbDatabase = 'cms';
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");


    $tbl_name="contact";       //your table name
    $adjacents = 3;

    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    //print $query;
    $queryRes = mysql_query($query);

    if($queryRes === FALSE) {
    die(mysql_error()); // TODO: better error handling
}

    while($rows = mysql_fetch_array($queryRes))
{
    $total_pages=$rows['num'];
    //print $total_pages;
}




    $targetpage = "page.php";   //your file name  (the name of this file)
    $limit = 10;                                //how many items to show per page

        $start = 0;                             //if no page var is given, set start to 0

    /* Get data. */
    $sql = "SELECT DISTINCT contact.`id` , contact.`contactgroup` , contact.`media` ,contact.`media2` , contact.`email1` , contact.`nationality` , contact.`country3` , contact.`twon` , contact.`area` , contact.`gender` , contact.`married` , contact.`children` , contact.`driverslicense` FROM $tbl_name LIMIT $start, $limit";
    //print $sql;
    $result = mysql_query($sql);

    /* Setup page vars for display. */
    $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    //print $lastpage; 
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">? previous</a>";
        else
            $pagination.= "<span class=\"disabled\">? previous</span>"; 

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))    //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2)) //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))     
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next ?</a>";
        else
            $pagination.= "<span class=\"disabled\">next ?</span>";
        $pagination.= "</div>\n";     
    }
?>

    <?php

    echo "<table border='1'>
<tr>
<th>Contactgroup</th>
<th>Email</th>
<th>Nationality</th>
<th>Country3</th>
<th>Twon</th>
<th>Area</th>
<thGender</th>
<th>Married</th>
<th>Children</th>
<th>Driverslicense</th>
</tr>";
        while($rows = mysql_fetch_array($result))
        {

         echo "<tr>";
  echo "<td>" . $rows['contactgroup'] . "</td>";
  echo "<td>" . $rows['email1'] . "</td>";
  echo "<td>" . $rows['nationality'] . "</td>";
  echo "<td>" . $rows['country3'] . "</td>";
  echo "<td>" . $rows['twon'] . "</td>";
  echo "<td>" . $rows['area'] . "</td>";
  echo "<td>" . $rows['gender'] . "</td>";
  echo "<td>" . $rows['married'] . "</td>";
  echo "<td>" . $rows['children'] . "</td>";
  echo "<td>" . $rows['driverslicense'] . "</td>";
  echo "</tr>";



        }

        echo "</table>";
    ?>

    <?php echo $pagination;?>  
EN

回答 1

Stack Overflow用户

发布于 2014-06-30 16:40:52

当找不到任何页面时,将$start变量初始化为0。如果找到页面,则将页面与limit相乘,并添加1以获得$start值,如下所示

代码语言:javascript
复制
if(isset($_GET['page']) && $_GET['page']!="")
  $start = ($_GET['page'] * 10) + 1; 
else
  $start = 0; 

有关更多细节,请查看Here

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24486038

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档