gpt4 book ai didi

php - 如何在单独的页面上显示每个(只有一个)搜索结果 mysql php

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:11 25 4
gpt4 key购买 nike

您好,我有一个名为“BookPage”的表,其中列为:“PageText”

我已经在“PageText”列上设置了搜索(请参阅下面的代码)。现在,当用户搜索关键字时,所有结果页面都会依次显示。

如果我可以通过单击某种“下一步”按钮链接将每个页面文本移动到下一页,是否可以在其各个页面上显示每个页面文本。

form.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Search</title>
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>

<body>
<?php

// include MySQL-processing classes

require_once 'mysql.php';

try{

// connect to MySQL

$db=new MySQL(array('host'=>'','user'=>'','password'=>'','database'=>''));

$searchterm=$db->escapeString($_GET['searchterm']);

$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ");

if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria
returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$row['PageText'].'</p></div>'."n";

}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>
</body>
</html>

您好,请查看下面修改后的代码无法正常工作,您能告诉我哪里出了问题吗。

修改代码

<?php

// include MySQL-processing classes

require_once 'mysql.php';

try{

// connect to MySQL

$db=new MySQL(array('host'=>'',''=>'','password'=>'','database'=>''));

//page
if (isset($_GET['page'])) {
$page_no = $_GET['page'];
$next_pg = $page_no + 1;
} else {
$page_no = 0;
$next_pg = 1;
}

$searchterm=$db->escapeString($_GET['searchterm']);
$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ORDER BY BookId ASC LIMIT 0, $page_no");


if(!$result->countRows()){

echo '<div class="maincontainer"><h2>No results were found. Go back and try a new search.</h2></div>'."n";

}

else{

// display search results

echo '<div class="maincontainer"><h2>Your search criteria returned '.$result->countRows().' results.</h2>'."n";

while($row=$result->fetchRow()){

$searchvalue = implode('<span style="color:green;font-weight:800;background-color:yellow;">'.$_GET['searchterm'].'</span>',explode($_GET['searchterm'],$row['PageText']));
echo '<div class="rowcontainer"><p><strong>Book Id:
</strong>'.$row['BookId'].'<p><p><strong>Page Id:
</strong>'.$row['PageId'].'</p><p><strong>Page Text:
</strong>'.$searchvalue.'</p></div>'."n";
}

}

echo '</div>';

}

catch(Exception $e){

echo $e->getMessage();

exit();

}

?>

最佳答案

您可以尝试将您的查询更改为:

if (isset($_GET['page'])) {
$page_no = $_GET['page'];
$next_pg = $page_no + 1;
} else {
$page_no = 0;
$next_pg = 1;
}

$result=$db->query("SELECT * FROM BookPage WHERE (PageText) like \"%$searchterm%\" ORDER BY id ASC LIMIT 1, $page_no");

然后插入如下链接:

<a href="thispage.php?page=<?php echo $next_pg; ?>">Next page</a>

这样做的目的是一次获得一个结果,并根据您所在的页面偏移结果。执行此操作时唯一重要的事情是按某种方式对结果进行排序,以便每个查询都以相同的顺序放置结果。例如,按 ID ASC 排序(根据我的示例)。

关于php - 如何在单独的页面上显示每个(只有一个)搜索结果 mysql php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11198399/

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