首页 \ 问答 \ PHP / MySQL中的多词搜索(Multi word search in PHP/MySQL)

PHP / MySQL中的多词搜索(Multi word search in PHP/MySQL)

我努力创建搜索多个单词的搜索。 我的第一次尝试没有得到任何结果,如下所示:

  require_once('database_conn.php');
  if($_POST){
  $explodedSearch = explode (" ", $_POST['quickSearch']);


  foreach($explodedSearch as $search){
  $query = "SELECT * 
            FROM jobseeker 
            WHERE forename like '%$search%' or surname like '%$search%' 
            ORDER BY userID 
            LIMIT 5";
  $result = mysql_query($query);
}

while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";

}
}  

我也尝试了以下方法,并取得了结果,但正如您所想的那样,我输入的每个单词都有重复的结果:

if($_POST){
$explodedSearch = explode (" ", $_POST['quickSearch']);


foreach($explodedSearch as $search){
$query = "SELECT * 
          FROM jobseeker 
          WHERE forename like '%$search%' or surname like '%$search%' 
          ORDER BY userID 
          LIMIT 5";
$result .= mysql_query($query);


while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";
}
}
}

我几乎不知道如何继续这一点,任何帮助将不胜感激。

编辑:

if($_POST){
$quickSearch = $_POST['quickSearch'];
$explodedSearch = explode (" ", trim($quickSearch));


$queryArray = array();

foreach($explodedSearch as $search){
$term = mysql_real_escape_string($search);
$queryArray[] = "forename like '%" . $term .  "%' surname like '%" . $term . "%'";
}

$implodedSearch = implode(' or ', $queryArray);

$query="SELECT *
        FROM jobseeker
        WHERE ($implodedSearch)
        ORDER BY userID
        LIMIT 5";

$result = mysql_query($query);

while($userData=mysql_fetch_array($result, MYSQL_ASSOC)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];


    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";

}
}

I'm struggling to create a search that searches for multiple words. My first attempt yielded no results whatsoever and is as follows:

  require_once('database_conn.php');
  if($_POST){
  $explodedSearch = explode (" ", $_POST['quickSearch']);


  foreach($explodedSearch as $search){
  $query = "SELECT * 
            FROM jobseeker 
            WHERE forename like '%$search%' or surname like '%$search%' 
            ORDER BY userID 
            LIMIT 5";
  $result = mysql_query($query);
}

while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";

}
}  

I also tried the following, which yielded results, but as you can imagine, I was getting duplicate results for every word I entered:

if($_POST){
$explodedSearch = explode (" ", $_POST['quickSearch']);


foreach($explodedSearch as $search){
$query = "SELECT * 
          FROM jobseeker 
          WHERE forename like '%$search%' or surname like '%$search%' 
          ORDER BY userID 
          LIMIT 5";
$result .= mysql_query($query);


while($userData=mysql_fetch_array($result)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];

    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";
}
}
}

I'm pretty much at a loss as to how to proceed with this, any help would be greatly appreciated.

EDIT:

if($_POST){
$quickSearch = $_POST['quickSearch'];
$explodedSearch = explode (" ", trim($quickSearch));


$queryArray = array();

foreach($explodedSearch as $search){
$term = mysql_real_escape_string($search);
$queryArray[] = "forename like '%" . $term .  "%' surname like '%" . $term . "%'";
}

$implodedSearch = implode(' or ', $queryArray);

$query="SELECT *
        FROM jobseeker
        WHERE ($implodedSearch)
        ORDER BY userID
        LIMIT 5";

$result = mysql_query($query);

while($userData=mysql_fetch_array($result, MYSQL_ASSOC)){
    $forename=$userData['forename'];
    $surname=$userData['surname'];
    $profPic=$userData['profilePicture'];
    $location=$userData['location'];


    echo "<div class=\"result\">
    <img class=\"quickImage\" src=\"" . $profPic. "\" width=\"45\" height=\"45\"/>
    <p class=\"quickName\">" . $forename . " " . $surname . "</p>
    <p class=\"quickLocation\"> " . $location . "</p>
    </div>";

}
}

原文:https://stackoverflow.com/questions/8482842
更新时间:2021-11-18 07:11

最满意答案

为超时时段添加变量,而不是使用值4000.请注意,它必须具有全局范围。 我在这里添加了一个名为delay的变量:

var wnd;
var curIndex = 0; // a var to hold the current index of the current url
var delay;

然后,在openWindow()函数中使用new变量,当您希望暂停发生时,在if语句中将其值设置为更长的时间段。

我在这里使用了三元运算符而不是if语句,但你也可以使用if语句:

function openWindow(){
    wnd = window.open('http://' + urlList[curIndex], '', '');

    // pause for 30 seconds instead of 4 if the condition is met
    delay = (curIndex > 0 && curIndex % 3 == 0 ? 30000 : 4000)

    setTimeout(function () {
         wnd.close(); //close current window
         curIndex++; //increment the index
         if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
}, delay);
}

Add a variable for your timeout period, instead of using the value 4000. Note that it must have global scope. I've added a variable calleddelay here:

var wnd;
var curIndex = 0; // a var to hold the current index of the current url
var delay;

Then, use the new variable in your openWindow() function, setting its value to a longer period in your if statement when you want the pauses to happen.

I've used a ternary operator here for instead of an if statement, but you could use an if statement just as well:

function openWindow(){
    wnd = window.open('http://' + urlList[curIndex], '', '');

    // pause for 30 seconds instead of 4 if the condition is met
    delay = (curIndex > 0 && curIndex % 3 == 0 ? 30000 : 4000)

    setTimeout(function () {
         wnd.close(); //close current window
         curIndex++; //increment the index
         if(curIndex < urlList.length) openWindow(); //open the next window if the array isn't at the end
}, delay);
}

相关问答

更多
  • 第一次扭曲: 编辑 我认为你在其他地方有问题。 ... 获取此代码并将其保存在文件中。 这就是你得到的 myvar是假的(开始) 现在将其设置为true: 然后单击并查看: fir ...
  • 具有媒体播放器元素的每个元素都有一个播放器属性定义。 这是所有方法所在的地方。 您可以使用以下任一方法访问它: $('video')[0].player.pause(); // Be sure the video element exists. $('video').each(function(){this.player.pause()}) // Safe. Each element with a media player element has a player property defined. T ...
  • 当然; 你只要让任何一个计算结果结束,或者暂停自己,给出一个恢复使用的动作,以及当时的状态: data Pause s a = Pause { runPause :: s -> (PauseResult s a, s) } data PauseResult s a = Done a | Suspend (Pause s a) instance Monad (Pause s) where return a = Pause (\s -> (Done a, s)) m >>= ...
  • 我想你问的是pause()方法 : 暂停队列执行特定的时间 I think you are asking about the pause() method: pauses queue execution for a specific amount of time
  • 如果要检测,单击了页面的浏览器选项卡,当用户在另一个选项卡上时,则可以使用window.onfocus window.onfocus = function() { alert('Browser tab clicked'); }; If you want to detect, that the browser tab with your page was clicked, when user was on aother tab, then you can use window.onfocus window. ...
  • 这已经修复,完全是跨浏览器和Twitter bootstrap友好。 你可以在这里查看新编辑的jsfiddle。 修正了javascript如下: function marqueescroll(o) { marqueePause(o.id); o.srt += o.ss; if ((o.ss < 0 && o.srt > o.sz) || (o.ss > 0 && o.srt < (o.w ? -o.sz : o.psz))) { o.s.style[o.m] = ...
  • 为超时时段添加变量,而不是使用值4000.请注意,它必须具有全局范围。 我在这里添加了一个名为delay的变量: var wnd; var curIndex = 0; // a var to hold the current index of the current url var delay; 然后,在openWindow()函数中使用new变量,当您希望暂停发生时,在if语句中将其值设置为更长的时间段。 我在这里使用了三元运算符而不是if语句,但你也可以使用if语句: function openWin ...
  • 是的,您可以使用Sys.sleep()暂停执行。 所以,等待一秒钟: Sys.sleep(1) 。 Yes, you can pause execution by using Sys.sleep(). So, to wait for one second: Sys.sleep(1).
  • 由beatfactor提供的关于链接的GitHub问题的答案是 使用afterEach时,如果要使用浏览器对象,则需要始终使用done回调参数。 这是为了向后兼容。 所以你需要做任何一件事: afterEach(browser, done) { // ... done(); } I've written on GitHub as issue and I've got a solution: https://github.com/nightwatchjs/nightwatch/issues/92 ...
  • 是的,在您的情况下,您应重新启动应用程序以应用更改。 但是您可以将HTML代码放在某个html文件中并将该文件发送到客户端,如果您在html文件中进行更改,则无需重新启动应用程序。 还可以考虑使用Express模块来提供静态文件。 Yes, in your case you should restart application to apply changes. But you can put your HTML code in some html file and send that file to cl ...

相关文章

更多

最新问答

更多
  • 如何检索Ember.js模型的所有属性(How to retrieve all properties of an Ember.js model)
  • maven中snapshot快照库和release发布库的区别和作用
  • arraylist中的搜索元素(Search element in arraylist)
  • 从mysli_fetch_array中获取选定的值并输出(Get selected value from mysli_fetch_array and output)
  • Windows Phone上的可用共享扩展(Available Share Extensions on Windows Phone)
  • 如何在命令提示符下将日期设置为文件名(How to set file name as date in command prompt)
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • 从iframe访问父页面的id元素(accessing id element of parent page from iframe)
  • linux的常用命令干什么用的
  • Feign Client + Eureka POST请求正文(Feign Client + Eureka POST request body)
  • 怎么删除禁用RHEL/CentOS 7上不需要的服务
  • 为什么Gradle运行测试两次?(Why does Gradle run tests twice?)
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在android中的活动之间切换?(Switching between activities in android?)
  • Perforce:如何从Depot到Workspace丢失文件?(Perforce: how to get missing file from Depot to Workspace?)
  • Webform页面避免运行服务器(Webform page avoiding runat server)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 内存布局破解(memory layout hack)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • 我们可以有一个调度程序,你可以异步添加东西,但会同步按顺序执行吗?(Can we have a dispatcher that you can add things todo asynchronously but will be executed in that order synchronously?)
  • “FROM a,b”和“FROM a FULL OUTER JOIN b”之间有什么区别?(What is the difference between “FROM a, b” and “FROM a FULL OUTER JOIN b”?)
  • Java中的不可变类(Immutable class in Java)
  • bat批处理文件结果导出到txt
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • 德州新起点计算机培训学校主要课程有什么?
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • “latin1_german1_ci”整理来自哪里?(Where is “latin1_german1_ci” collation coming from?)