HTML和PHP生成分页链接

时间:2016-10-02 09:25:46

标签: php html

我有一些PHP和html代码加载到我的数据库的结果中。它显示每页五个结果。我们假装有1000页。所有这些页面的链接都会离开屏幕。 Google遇到了这个问题,但他们通过仅显示当前链接以及5个链接和5个链接来修复它。我想做这样的事情。我不想显示100个指向各个页面的链接。假装用户在第100页。我想显示第100页的链接以及链接95到105.我该怎么做?到目前为止,这是我的代码:

$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>";
}

1 个答案:

答案 0 :(得分:0)

你想要的是改变这个:

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

在此列出所有链接$ j&lt; $ pageNum,你只想列出10:$ j&lt; = 10开始$ j = $ currentPage或$ j = $ currentPage - 5如果$ currentPage是&gt; 5