Buttero Posted December 3, 2006 Share Posted December 3, 2006 Hey all,I'm new to PHP, and I've just found this PHP search script. I've edited so it works fine. But I was wondering if anyone knew how to actually highlight the search term once the articles containing this search word are found. For example, If you search for "Hello" then the php finds all the mysql entries with the word "Hello" in the $description feild. However, I would like to highlight the word "Hello" in the found articles. The php code is below:[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","password"); //select which database you want to editmysql_select_db("database"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the row echo "<h2>$title</h2><h3>$date by $user</h3>$description <hr>";}?>[/code]Thanks for your time Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/ Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Perhaps one approach would be to explode the returned string containing the description into an array so that the keyword would end up as individual entries. You could then loop through the array to display the values but check on each return if it matches the original search keyword and change the formatting if it matches.ie.if you have the description as "blah blah blah blah blah search blah blah search blah", you could explode this that you ended up with an array:array[0] = "blah blah blah blah blah "array[1] = "search"array[2] = " blah blah "array[3] = "search"array[4] = " blah"Then you could loop checking for the value matchign against original search. If you need help further scripting this, let us know. Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134290 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 Thanks for the quick reply Chiprivers. If this approach works that would be great. The thing is, I would not know where to start when It comes to implementing this into the origional code. :(. Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134294 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Just had a quick look at how to use explode() and it would be slightly different to what I said. When exploding a string you set a delimiter for which it searches for and uses to split up the original string into the array. In doing this, it removes the delimiter altogether so you would not end up with any values in the array equal to the search keyword, this actually makes it a bit simpler as you know that between every two values in the array, there should be your keyword.so try this:[code]$description = explode($search, $description);foreach ($description as $part) {echo $part;echo $search; // add any highlighting in this section}[/code]This will all need to fit in where you have displayed $description. It may need some slight alteration, it is not something i have done before. Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134297 Share on other sites More sharing options...
bljepp69 Posted December 3, 2006 Share Posted December 3, 2006 Here's a method I've used to show the search string in bold text:[code]<?php function formatSearch($text,$term) { return preg_replace("/{$term}/i",'<strong>${0}</strong>',$text); //note the ${0} in the replacement term keeps the original string that matched the search term } //you call the function like $formatted_result = formatSearch($search_results,$search_for); //or echo it directly echo formatSearch($search_results,$search_for);?>[/code]Hope that works for you Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134302 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 [quote author=chiprivers link=topic=117195.msg477881#msg477881 date=1165161471]Just had a quick look at how to use explode() and it would be slightly different to what I said. When exploding a string you set a delimiter for which it searches for and uses to split up the original string into the array. In doing this, it removes the delimiter altogether so you would not end up with any values in the array equal to the search keyword, this actually makes it a bit simpler as you know that between every two values in the array, there should be your keyword.so try this:[code]$description = explode($search, $description);foreach ($description as $part) {echo $part;echo $search; // add any highlighting in this section}[/code]This will all need to fit in where you have displayed $description. It may need some slight alteration, it is not something i have done before.[/quote]I put this in the echo where I display the search results. however nothing is highlighted.You can see at www.seski.co.uk and search for "Billy".Here is the code when i added the new snippet[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","password"); //select which database you want to editmysql_select_db("database"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the row echo "<h2>$title</h2><h3>$date by $user</h3>$description = explode($search, $description);foreach ($description as $part) {echo $part;echo $search; // add any highlighting in this section}<hr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134306 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 sorry, change last bit so like this:[code]//display the rowecho "<h2>".$title."</h2>";echo "<h3>".$date." by ".$user."</h3>";$description = explode($search, $description);foreach ($description as $part) {echo $part;echo $search; // add any highlighting in this section}echo "<hr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134310 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 Thanks for the help so far.Now I sort of understand whats going on in the last snippet of code that I changed. But no highlighting is happening still ???. You can see the problem if you search on seski for the word Billy for example. At the bottom of the page it just says "billy" then some random code appears :-\ I dont know how to get around this Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134317 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Sorry again, I didn't actually add any script to highlight the script, not sure on what to do to make it highlighted, but to check what we have done so far you could make the word bold:[code]echo "<br>".$search."<br>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134321 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 ah, now its working much better.i replaced the old echo $search with the new one to make it bold. Now all the terms that were searched in the post appear bold. However, if you look at the bottom of the page once you search, there is the search term repeated for no reason, in this case "Billy" and this random selection of symbols ""; } ?>" Maybye they somehow escaped the PHP?Thanks for making this work so far! :) Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134324 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 post your completed script here and I'll check whats putting them funny bits on the end Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134325 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 ahh, my bad, i know what was making the random symbols. They were out of the php, probarbly just some of the old code that didnt get fully deleted. Here is the code:[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","password"); //select which database you want to editmysql_select_db("database"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the rowecho "<h2>".$title."</h2>";echo "<h3>".$date." by ".$user."</h3>";$description = explode($search, $description);foreach ($description as $part) {echo $part;echo "<b>".$search."</b>";}echo "<hr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134326 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 try this to highlight text:[code]exho "<span style=\"background-color: yellow\">".$search."</span>";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134329 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 This seems to screw it up :( It dosent matter if its bold, as long as its different in style. However the search term keeps appearing at the bottom :o. Its because after all the search terms are made bold there is still:[code]echo "<b>".$search."</b>";[/code]At the bottom of the script, witch repeats the search term. Is there any way to get around this without stopping the search term being bold in the actual post?Thanks Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134337 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Did you replace echo "<b>".$search."</b>";withecho "<span style=\"background-color: yellow\">".$search."</span>"; Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134346 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 yea but this makes it so that only the repeated search term at the bottom is highlighted :-\ Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134349 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 could you post your current script Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134353 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 Sure :)[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","pass"); //select which database you want to editmysql_select_db("db"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the rowecho "<h2>".$title."</h2>";echo "<h3>".$date." by ".$user."</h3>";$description = explode($search, $description);foreach ($description as $part) {echo $part;echo "<b>".$search."</b>";}echo "<hr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134358 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 is this the script that is or is not working? Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134361 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 this script works perfect thanks for the help. But all thats wrong is the repeated search term at the bottom ??? if i search any random word, the word is nicely made bold in the post where it appears, but then the word just reappears at the bottom :(Thanks for making it work better Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134365 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 Oh right, I understand. What we have done is loop through all the array values and each time we display it we are adding on the search term before the next value is shown. What we have to do is work out when we have reached the last value in the array and not display the search term. Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134367 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 Ah ok. I can understand the problem now, so we need to get the php to realise the last value and not to display the term. Since I dont know how to code php from scratch im guessing this could be a problem? :'( ::) Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134369 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 $num = count($description)for ($i=0; $i < $num; $i++) {echo $part[$i];if ($i != ($num-1)) {echo "<b>".$search."</b>";}} Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134375 Share on other sites More sharing options...
Buttero Posted December 3, 2006 Author Share Posted December 3, 2006 this is the code:[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","pass"); //select which database you want to editmysql_select_db("blah"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the rowecho "<h2>".$title."</h2>";echo "<h3>".$date." by ".$user."</h3>";$description = explode($search, $description);foreach ($description as $part) {echo $part;$num = count($description)for ($i=0; $i < $num; $i++) {echo $part[$i];if ($i != ($num-1)) {echo "".$search."";}}}echo "<hr>";}?>[/code]But i get this error Parse error: syntax error, unexpected T_FOR in /home/jcardus1/public_html/search.php on line 77This was the code before I edited it:[code]<?//connect to mysql//change user and password to your mySQL name and passwordmysql_connect("localhost","user","pass"); //select which database you want to editmysql_select_db("blah"); $search=$_POST["search"];//get the mysql and store them in $result//change whatevertable to the mysql table you're using//change whatevercolumn to the column in the table you want to search$result = mysql_query("SELECT * FROM frontpage_news WHERE description LIKE '%$search%'");//grab all the contentwhile($r=mysql_fetch_array($result)){ //the format is $variable = $r["nameofmysqlcolumn"]; //modify these to match your mysql table columns $title=$r["title"]; $description=$r["description"]; $user=$r["user"]; $date=$r["date"]; $id=$r["id"]; //display the rowecho "<h2>".$title."</h2>";echo "<h3>".$date." by ".$user."</h3>";$description = explode($search, $description);foreach ($description as $part) {echo $part;echo "<b>".$search."</b>";}echo "<hr>";}?>[/code]thanks for all the help so far Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134379 Share on other sites More sharing options...
chiprivers Posted December 3, 2006 Share Posted December 3, 2006 last bit that is currently like this:[code]$description = explode($search, $description);foreach ($description as $part) {echo $part;$num = count($description)for ($i=0; $i < $num; $i++) {echo $part[$i];if ($i != ($num-1)) {echo "".$search."";}}}echo "<hr>";}?>[/code]should be like this:[code]$description = explode($search, $description);$num = count($description)for ($i=0; $i < $num; $i++) {echo $part[$i];if ($i != ($num-1)) {echo "".$search."";}}echo "<hr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29304-php-search-form-help/#findComment-134387 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.