gpt4 book ai didi

php - HTML 和 PHP 分页无法正常工作

转载 作者:行者123 更新时间:2023-11-28 02:49:52 25 4
gpt4 key购买 nike

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

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

我怎样才能只显示包含两个结果的一页,而不是第 8 页包含两个相关结果的 11 页?谢谢

最佳答案

请更新您的代码如下。
但是尝试使用 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
}

关于php - HTML 和 PHP 分页无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39817533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com