gpt4 book ai didi

php - 当我单击下一步时,基于条件的分页不起作用

转载 作者:行者123 更新时间:2023-11-30 01:10:59 25 4
gpt4 key购买 nike

我有一个搜索页面,用户可以根据从下拉列表中选择的类别来搜索项目。当我选择一个类别时,它会显示数据和下一页,但是当我单击下一步时,页面不会显示任何结果。请帮忙

<?php
include("scripts/connection.php");
session_start();

if (empty($_SESSION["username"])) {
header("Location:login.php");
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search Posts</title>
<link rel="stylesheet" href="style.css" type="text/css" media="all" />
</head>
<body>
<div class="main-menu">
<ul id="menu">
<li><a href="mainhome.php">Home</a></li>
<li><a href="itempost.php">New Post</a></li>
<li><a href="yourpost.php">Your Posts</a></li>
<li id="active"><a href="Search.php">Search Posts</a></li>
<li class="log"><a href="scripts/logout.php">Log Out</a></li>
<li class="log"><a href="account.php">Account</a></li>
</ul>
</div>
<div id="header"><img src="Images/header.gif"/></div>
<div id="sidebar"><h2><u>Posts</u></h2><p/>
<form method="POST" name="SearchForm" action="search.php">
<table id="mainhomeform">
<tr>
<td>Select a category to search through:<p/>
<select name="SearchCategory" class="searchlist" tabindex="1">
<option value="<?php if(isset($searchcategory)){echo $searchcategory;}else{echo "Uncategorized";}?>"><?php if(isset($searchcategory)){echo $searchcategory;}else{echo "Uncategorized";}?></option>
<option value="Clothing">Clothing</option>
<option value="Electronics">Electronics</option>
<option value="Furniture">Furniture</option>
<option value="Stationary">Stationary</option>
</select>
</td>
</tr>
<tr>
<td>
<input name="search" type="submit" class="wbutton" tabindex="3" value="Search" />
</td>
</tr>
</table>
</form>
</div>
<div id="content"><h2><img src="Images/posts.gif"/></h2>
<?php
if(isset($_POST['search'])) {
$searchcategory = $_POST['SearchCategory'];
$searchbutton = $_POST['search'];

$search = "SELECT * FROM items WHERE Category='$searchcategory' ORDER BY PostDate DESC";
$select = mysql_query($search);

$nr = mysql_num_rows($select);

if ($nr>0) {
if (isset($_GET['pn'])) {
$pn = preg_replace('#[^0-9]#i', '', $_GET['pn']);
} else {
$pn = 1;
}

$itemsPerPage = 5;
$lastPage = ceil($nr / $itemsPerPage);

if ($pn < 1) {
$pn = 1;
} else if ($pn > $lastPage) {
$pn = $lastPage;
}

$centerPages = "";

$sub1 = $pn - 1;
$sub2 = $pn - 2;
$add1 = $pn + 1;
$add2 = $pn + 2;

if ($pn == 1) {
$centerPages .= '&nbsp; <span class="pagenumbers">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';

} else if ($pn == $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagenumbers">' . $pn . '</span> &nbsp;';

} else if ($pn > 2 && $pn < ($lastPage - 1)) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub2 . '">' . $sub2 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';

$centerPages .= '&nbsp; <span class="pagenumbers">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add2 . '">' . $add2 . '</a> &nbsp;';

} else if ($pn > 1 && $pn < $lastPage) {
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $sub1 . '">' . $sub1 . '</a> &nbsp;';
$centerPages .= '&nbsp; <span class="pagenumbers">' . $pn . '</span> &nbsp;';
$centerPages .= '&nbsp; <a href="' . $_SERVER['PHP_SELF'] . '?pn=' . $add1 . '">' . $add1 . '</a> &nbsp;';
}

$limit = 'LIMIT ' .($pn - 1) * $itemsPerPage .',' .$itemsPerPage;
$select2 = mysql_query("SELECT * FROM items WHERE Category='$searchcategory' ORDER BY PostDate DESC $limit");

$select2_array = array();

$paginationDisplay = "";

if ($lastPage != "1") {
if ($pn != 1) {
$previous = $pn - 1;
$paginationDisplay .= '&nbsp; <a class="button" href="' . $_SERVER['PHP_SELF'] . '?pn=' . $previous . '"> Back</a> ';
}

$paginationDisplay .= '<span class="pagenumbers">' . $centerPages . '</span>';

if ($pn != $lastPage) {
$nextPage = $pn + 1;
$paginationDisplay .= '&nbsp; <a class="button" href="' . $_SERVER['PHP_SELF'] . '?pn=' . $nextPage . '"> Next</a> ';
}
}

for ($count=0; $row = mysql_fetch_array($select2);$count++) {
$select2_array[$count] = $row;

echo "<table id='home'>";

foreach($select2_array as $itemshome);

echo "<tr>";
echo "<td class='title'>". $itemshome ['Title']."</td>";
echo "<td class='rf'>". $itemshome ['PostDate']."<br/>".$itemshome ['Category']."</td>";
echo "<hr>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'>"."<img src='$itemshome[Image1]' class='pic' alt='userimage' />";
echo "<img src='$itemshome[Image2]' class='pic' alt='userimage' />";
echo "<img src='$itemshome[Image3]' class='pic' alt='userimage' />"."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='3'>"."<textarea name='Description' type='text' class='description' tabindex='2' readonly >".$itemshome ['Description']."</textarea>"."</td>";
echo "</tr>";
echo "</tr>";
}

echo "</table>";
echo "$paginationDisplay";

} else {
$empty = '<div class="error">That category is empty.</div>';

echo $empty;
}
}
?>

<div class="footer">
<hr/>
Copyright &copy;<br/>Designed Using XHTML 1.1
<p id="rf">Designed by Mutondo Sitemba</p>
</div>
</div>
</body>
</html>

最佳答案

您的链接有 pn,但没有 searchCategory。所以我想你的查询是:SELECT * FROM items WHERE Category='' ORDER BY PostDate DESC

当您遇到空结果问题时,第一步您应该打印您的查询并查看它是否正确组成。通常这会有所帮助。

关于php - 当我单击下一步时,基于条件的分页不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19475440/

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