[英]Make the PHP MySql Search Engine and Pagination work
I don't know how to make the search through another table. 我不知道如何通过另一个表进行搜索。 how should i do that?
我该怎么办? the table name is comments and i want to search for all the post stored in the column name kom
表名是注释,我想搜索存储在列名kom中的所有帖子
Another thing is that i cant get the pagination start working... I started the pagination within an else statment because i only need it when i get more than 1 result. 另一件事是我无法使分页开始工作...我在其他语句中开始了分页,因为我仅在得到多个结果时才需要它。 I can get the page links showing and limit the search posting showing but when i click on one off the links i cant get to the next page Heres the code
我可以获取显示的页面链接并限制搜索发布的显示,但是当我单击链接上的一个时,我无法进入下一页这是代码
<?php
$search = $_POST["search"];
$field = $_POST["field"];
if($_POST["submit"] && $search)
{
echo "<div id='result'>";
echo "<h2>Resultat</h2>";
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$query = "SELECT * FROM blogTable WHERE title LIKE '%$search%'
UNION
SELECT * FROM blogTable WHERE post LIKE '%$search%'";
$result = mysql_query($query, $conn) or die(mysql_error());
$matches = mysql_num_rows($result);
if($matches == 0)
//code if serch didnt result any results
else if($matches == 1)
//code if the matches only 1
else
{
$per_page = 4;
$pages = ceil($matches / $per_page);
$page = (isset($_GET['page'])) ? (int)$_GET['page']: 1;
$start = ($page - 1) * $per_page;
$query2 = "SELECT * FROM blogTable WHERE title LIKE '%$search%'
UNION
SELECT * FROM blogTable WHERE post LIKE '%$search%' LIMIT $start, $per_page";
$result2 = mysql_query($query2, $conn) or die(mysql_error());
echo "<font size='-1'>Sökningen $search gav $matches resultat</font><br/>";
while ($r2 = mysql_fetch_array($result2))
{
$id = $r["id"];
$title = $r["title"];
$post = $r["post"];
$time = $r["time"];
echo "<br/><strong><a href='comment.php?id=$id'>$title</a></strong><br/>";
echo "<font size='-1'>".substr($post, 0, 60)."</font><br/>";
echo "<font size='-1'>".substr($post, 60, 70)."</font><br/>";
echo "<font size='-3'>$time</font><br/>";
}
//theese are showin but cannot click of any of them
if($pages >= 1 && $page <= $pages)
{
for($nr = 1; $nr <= $pages; $nr++)
{
if($nr == $page)
echo "<a href='?page=".$nr."' style='font-size:20px;'>$nr</a>";
else
echo "<a href='?page=".$nr."' style='font-size:15px;'>$nr</a> ";
}
}
}
}
?>
Is there a specific reason you are using a UNION
? 您使用
UNION
是否有特定原因?
If not, you can change: 如果没有,您可以更改:
$query = "SELECT * FROM blogTable WHERE title LIKE '%$search%'
UNION
SELECT * FROM blogTable WHERE post LIKE '%$search%'";
to: 至:
$query = "SELECT * FROM blogTable WHERE (title LIKE '%$search%') OR (post LIKE '%$search%')";
Anyway, I would never execute the same query twice, just get the first x results if no start parameter was given (for example a page number in the query string) and calculate the start point when a start parameter was given. 无论如何,我永远不会执行两次相同的查询,如果未指定起始参数(例如查询字符串中的页码),则只会获得前x个结果,并在指定起始参数时计算起始点。
And if you want the total, use a COUNT(*)
query or change your query: 如果需要总数,请使用
COUNT(*)
查询或更改查询:
$query = "SELECT SQL_CALC_FOUND_ROWS * FROM blogTable WHERE (title LIKE '%$search%') OR (post LIKE '%$search%')";
One thing that catches the eye is that the code you show is vulnerable to SQL injection . 令人瞩目的一件事是,您显示的代码容易受到SQL注入的攻击。
Get rid of the strip_tags()
(if it's for security, in which case it's useless) and do a mysql_real_escape_string()
on every value you use in the search queries, or check whether the value is actually a number when using int
columns. 删除
strip_tags()
(如果出于安全性考虑,在这种情况下它是无用的),然后对您在搜索查询中使用的每个值执行mysql_real_escape_string()
,或者在使用int
列时检查该值是否实际上是一个数字。
Another thing is that the <font>
tag is outmoded. 另一件事是
<font>
标记已过时。 The cool CSS way of styling text is having an external CSS stylesheet, and defining in it something like CSS样式样式的酷方法是拥有一个外部CSS样式表,并在其中定义类似
span.small { font-size: 12px; color: green }
and then using it in the HTML like so: 然后像这样在HTML中使用它:
<span class="small">Text goes here</span>
that said, this probably belongs on CodeReview.SE.... 就是说,这可能属于CodeReview.SE。
首先,我始终建议对搜索和过滤器使用GET方法而不是POST方法,其次,也许此分页php类可以为您提供帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.