我正在尝试在我的网站上实现搜索功能。我有以下类型的数据。
Title Descr
----- -----
World News World news, news from across the world, all world news events covered
World Sports World sports news, for all of sports people, sports is a famous world现在我想如果用户搜索“世界新闻”,那么结果将显示如下,
World News (title)
... all world news events covered ... (descr)基本上我想在随机的基础上显示一些短语。就像当用户搜索“世界新闻”时,有时他会得到如上的结果,有时会得到下面的结果,
World News (title)
World news, news from across the world, ... (descr)请告诉我如何使用mysql查询获得这种功能,或者如果不能直接通过mysql查询,那么如何使用PHP。
谷歌有时会做同样的事情,谷歌显示中间的短语,有时显示开始的文本。
谢谢。
发布于 2009-05-07 23:14:39
不确定是否有MySQL方法来实现你想要的东西。我将从数据库中提取字段,然后使用PHP的explode()函数随机化一个短语:
$desc_field_result = "World news, news from across the world, all world news events covered"; // The "Desc" field from your MySQL Query
$desc_field_array = explode(",",$desc_field_result);
$selected_desc = $desc_field_array[rand(0,sizeof($desc_field_array)-1)];
echo $selected_desc;发布于 2009-05-07 23:31:08
谷歌并不只是返回一些随机的句子。它返回一个(或两个)包含所搜索单词的代码片段。MySQL不能为您做到这一点。您需要在PHP中手动执行此操作(在返回的数据库行中手动搜索关键字并显示相关的代码片段),或者使用可以为您执行此操作的搜索引擎(例如Xapian、Sphinx或Apache Lucene)。
发布于 2009-04-28 11:29:34
如果您在mysql表中存储了标题和描述,请尝试如下所示:
SELECT descr FROM yourTable WHERE title='World News' ORDER BY RAND()ORDER BY RAND()条件将从标题条件为World news匹配的行中随机选择
https://stackoverflow.com/questions/797359
复制相似问题