HTML和PHP分页无法正常工作

时间:2016-10-02 13:31:48

标签: php html mysql

我有一些代码可以从数据库中提取所有结果,并显示与用户搜索相关的结果。我有一些代码可以计算项目数量,并根据与用户搜索相关的项目数生成一定数量的页面。问题如下。如果我全部搜索,我的代码会在11页上显示数据库中的所有内容。如果我搜索汽车,它仍将显示11页,但只有2个结果在标题中有单词car。问题是这些结果显示在第八页上,所有其他页面都是空白的。在搜索期间,所有两个结果与标题中的汽车也显示在第八页上。搜索全部基于项目在数据库中的顺序。这是我目前的代码:

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






                }



                    $totalPages = $pageNum;
                    $currentPage = $page;
                    $numPagesToShow = 10;

                    if($currentPage > $totalPages) {
                        $currentPage = $totalPages;
                    }


                    if($numPagesToShow >= $totalPages) {
                        $numMaxPageLeft = 1;
                        $numMaxPageRight = $totalPages;
                    } else {
                        $pagesToShow = ceil($numPagesToShow/2);
                        $numMaxPageLeft = $currentPage - $pagesToShow;
                        $numMaxPageRight = $currentPage + $pagesToShow;

                        if($numMaxPageLeft <= 0) {
                            $numMaxPageRight = $numMaxPageRight - $numMaxPageLeft +1;
                            $numMaxPageLeft = 1;
                        } elseif($numMaxPageRight >= $totalPages) {
                            $numMaxPageLeft -= ($numMaxPageRight - $totalPages);
                            $numMaxPageRight = $totalPages;
                        }
                    }

                    for ($i=$numMaxPageLeft; $i<=$numMaxPageRight; $i++) {
                        echo "<a id =\"pagenationlink\" href=\"searchresults.php?search=".$terms."&page=".$i."\">".$i."</a>";
                    }

如何只显示一页上有两个结果的页面,而不是第八页上显示两个相关结果的11页?感谢

1 个答案:

答案 0 :(得分:0)

请更新您的代码,如下所示 但是尝试使用mysqli_ ()作为mysql ()被删除

$cond = "";
if(!empty($_POST["search"]))
{    
   $cond = " write your search condition " ;
}
$start = (($page-1)*5);
$query  = mysql_query("SELECT  SQL_CALC_FOUND_ROWS * FROM posts where $cond LIMIT $start, 5");
$TotalDataQuery = mysql_query("SELECT FOUND_ROWS() tot;");
$rsVal = mysql_fetch_array($pagesQuery);
$pagesQuery = $rsVal['tot'];
$pageNum = ceil($pagesQuery/5);

while ($row = mysql_fetch_array($query)) {
 //continue your code
} 
相关问题