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
最满意答案
为超时时段添加变量,而不是使用值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 called
delay
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 yourif
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 anif
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); }
相关问答
更多-
如何从浏览器编辑JS?(How can I edit JS from browser?)[2022-03-10]
第一次扭曲: 编辑 我认为你在其他地方有问题。 ... 获取此代码并将其保存在文件中。 这就是你得到的 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 ...
-
暂停monad(The Pause monad)[2022-08-06]
当然; 你只要让任何一个计算结果结束,或者暂停自己,给出一个恢复使用的动作,以及当时的状态: 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. ...
-
在MouseOver上暂停不使用Cross Browser Marquee II(Pause on MouseOver not working with Cross Browser Marquee II)[2022-01-13]
这已经修复,完全是跨浏览器和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).
-
Nightwatch,afterEach,browser.pause不是一个功能(Nightwatch, afterEach, browser.pause is not a function)[2022-04-15]
由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 ... -
节点js刷新浏览器(Node js refresh browser)[2021-12-30]
是的,在您的情况下,您应重新启动应用程序以应用更改。 但是您可以将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 ...