排序和 substr_count
我正在尝试为一个小网站创建一个简单的搜索功能,并在顶部提供最相关的项目,
$q = "SELECT * FROM pages as p WHERE p.content LIKE '%$searchparam%' OR p.title LIKE '%$searchparam%' LIMIT 500";
$r = @mysqli_query ($dbc, $q); // Run the query.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$count = substr_count($row['content'], "$searchparam");
我猜我需要使用 $count 中的值对 $row 进行排序,但我不确定如何执行此操作。谁能帮我解决语法问题吗?
Im trying to make a simple search function for a small site, and have the most relevant items at the top
$q = "SELECT * FROM pages as p WHERE p.content LIKE '%$searchparam%' OR p.title LIKE '%$searchparam%' LIMIT 500";
$r = @mysqli_query ($dbc, $q); // Run the query.
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
$count = substr_count($row['content'], "$searchparam");
I'm guessing I need to sort $row with the value from $count, but I'm not sure how to do this. Can anyone help me out with the syntax?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更好的解决方案是使用 MySq'ls 全文搜索 功能,因为结果是按排名返回的,并且它可能比尝试自己编写更容易、更健壮。您还可以查看其他搜索引擎,例如 Sphinx。
但是,如果您想继续使用您的方法,您应该执行以下操作:
A better solution would be use use MySq'ls full text search capabilities, as the results are returned ranked and it is probably easier and more robust than trying to write it yourself. You could also look into other search engines like Sphinx.
However, if you wanted to continue with your method, you should do something like: