简体   繁体   English

HTML和PHP生成分页链接

[英]HTML and PHP generate pagination links

I have some PHP and html code that loads in results from my database. 我有一些PHP和html代码可从数据库中加载结果。 It shows five results per page. 每页显示五个结果。 Let's pretend I have 1000 pages. 假设我有1000页。 The links for all those pages would go off the screen. 所有这些页面的链接都将在屏幕上消失。 Google had this problem but they fixed it by only displaying the current link as well as 5 links back and 5 links forward. Google遇到了此问题,但他们仅通过显示当前链接以及向后5个链接和向前5个链接来解决此问题。 I want to do something like this. 我想做这样的事情。 I don't want to display 100 links to the various pages. 我不想显示指向各个页面的100个链接。 Pretend the user is on page 100. I want to display the links for page 100 as well as link 95 to 105. How can I do this? 假设用户在第100页上。我想显示第100页的链接以及从95到105的链接。我该怎么做? Here is my code so far: 到目前为止,这是我的代码:

$page = $_GET["page"];
$pagesQuery  = mysql_query("SELECT count(id) FROM(`posts`)");
$pageNum = ceil(mysql_result($pagesQuery, 0)/5);
$start = (($page-1)*5);

$currentname = mysql_query("SELECT * FROM posts LIMIT $start, 5");  
while ($row = mysql_fetch_array($currentname)) {
        //recieve relevant data.
        $title = $row[0];
        $desc = $row[13];
        $ID = $row[6];
        $views = $row[3];
        $user = $row[7];
        //fetch the last id from accounts table.
        $fetchlast1 = mysql_query("SELECT * FROM allaccounts WHERE id=(SELECT MAX(id) FROM allaccounts)");
        $lastrow1 = mysql_fetch_row($fetchlast1);
        $lastid1 = $lastrow1[6];
        //acquire the username of postee.
        for ($i1=1; $i1 <= $lastid1; $i1++) { 
            $currentname1 = mysql_query("SELECT * FROM allaccounts WHERE id=$user");
            while ($row1 = mysql_fetch_array($currentname1)) {
                $username1 = $row1[0];
            }
        }

        //Format Title, description and view count.
        $title2 = rtrim($title);
        $donetitle = str_replace(" ", "-", $title2);
        $url = "articles/".$ID."/".$donetitle."";

        $donetitle = strlen($title) > 40 ? substr($title,0,40)."..." : $title;
        $donedesc = '';

        if(strlen($desc) > 150) {
            $donedesc = explode( "\n", wordwrap( $desc, 150));
            $donedesc1 = $donedesc[0] . '...';                          
        }else{
            $donedesc1 = $desc;                         
        }
        $finviews = number_format($views, 0, '.', ',');

        //Give relevant results
        if(stripos($title, $terms) !== false || stripos($desc, $terms) !== false || stripos($username1, $terms) !== false){
                if($row[10] == null){
                    $SRC = "img/tempsmall.jpg";
                }else{
                    $SRC ="generateThumbnailSmall.php?id=$ID"; 
                }
                echo "<div id = \"feature\">

                       <img src=\"$SRC\" alt = \"article thumbnail\" />
                      </div>
                        <div id = \"feature2\">
                             <a href= \"$url\" id = \"titletext\" alt = \"article title\">$donetitle</a>
                             <p id=\"resultuser\" >$username1</p>
                             <p id=\"resultp\">$donedesc1</p>
                             <a href = \"sendflag.php?title=$title&url=$url&id=$ID&userid=$user\" id = \"flag\" alt = \"flag\"><img src=\"img/icons/flag.png\"/></a><b id=\"resultview\">$finviews views</b> 

                        </div>
                      <div id = \"border\"></div>";
            }                   
}

for ($j=1; $j < $pageNum; $j++) { 
    echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}

What you want is to change this: 您要更改此内容:

for ($j=1; $j < $pageNum; $j++) { 
 echo "<a id =\"\" href=\"searchresults.php?search=".$terms."&page=".$j."\">".$j."</a>";
}

Here you list all links $j < $pageNum and you want to list just 10: $j <= 10 starting $j = $currentPage or $j = $currentPage - 5 if $currentPage is > 5 在这里,您列出所有链接$ j <$ pageNum,并且只列出10:$ j <= 10起始$ j = $ currentPage或$ j = $ currentPage-如果$ currentPage> 5,则为5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM