我正在尝试为搜索结果创建分页。如果我放弃搜索功能(即不包括if (isset($_POST['submit-search']))
和LIKE %search%
),我就可以创建一个工作分页但是一旦我包含这些,如下面的代码所示,我只得到一个空白单击下一页时的页面,就好像sql术语没有保存在内存中一样。
<?php
if (isset($_POST['submit-search'])) {
$results_per_page = 2;
$search = mysqli_real_escape_string($conn, $_POST['search']);
$sql = "SELECT * FROM article WHERE a_title LIKE '%$search%' OR a_text LIKE '%$search%'";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
$number_of_pages = ceil($queryResult/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
$this_page_first_result = ($page-1)*$results_per_page;
$sql = "SELECT * FROM article WHERE a_title LIKE '%$search%' OR a_text LIKE '%$search%'LIMIT ".$this_page_first_result.",".$results_per_page." ";
$result = mysqli_query($conn, $sql);
// echoing the search results
while ($row = mysqli_fetch_assoc($result)) {
echo "<h3>".$row['title']."</h3>
<p>".$row['text']."</p>";
}
// echoing the pagination
for ($page=1;$page<=$number_of_pages;$page++) {
echo "<li><a href='search_results.php?page=".$page."'>".$page."</a></li>";
}
}
?>
HTML
<form action="search_results.php" method="POST">
<input type="text" name="search"placeholder="Search">
<button type="submit" name="submit-search">go</button>
</form>
答案 0 :(得分:0)
逻辑错误。
{
LOADED: {
'.../lib/Schedule.js': 1,
path: 2,
'.../node_modules/colors/lib/index.js': 1,
'.../node_modules/colors/lib/colors.js': 6,
'.../node_modules/colors/lib/styles.js': 1,
'.../node_modules/colors/lib/system/supports-colors.js': 1,
'.../node_modules/colors/lib/custom/trap.js': 1,
'.../node_modules/colors/lib/custom/zalgo.js': 1,
'.../node_modules/colors/lib/maps/america.js': 1,
'.../node_modules/colors/lib/maps/zebra.js': 1,
'.../node_modules/colors/lib/maps/rainbow.js': 1,
'.../node_modules/colors/lib/maps/random.js': 1,
'.../node_modules/colors/lib/extendStringPrototype.js': 1,
'.../lib/utils.js': 2,
util: 2,
events: 1,
'/.../node_modules/lodash/lodash.js': 1,
'/.../node_modules/json-stringify-safe/stringify.js': 1,
'.../node_modules/debug/src/index.js': 1,
'.../node_modules/debug/src/node.js': 1,
tty: 1,
'.../node_modules/debug/src/debug.js': 1,
'.../node_modules/ms/index.js': 1,
'/.../node_modules/supports-color/index.js': 1 },
CACHED: {
'.../node_modules/colors/lib/colors.js': 5,
'.../lib/utils.js': 1
}
}
当用户进入下一页时:
var obj = d3.nest()
.key(function(d) { return d.r1val1; })
.entries(data);
只提供了一个页码,并且由于没有发布任何内容而导致搜索参数丢失,因此第一个if语句将不会再次运行。
您可以执行以下操作:
// this code is only executed on the initial search.
if (isset($_POST['submit-search'])) {
和
echo "<li><a href='search_results.php?page=".$page."'>".$page."</a></li>";