Jump to content

PHP Search form help


Buttero

Recommended Posts

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 password
mysql_connect("localhost","user","password");

//select which database you want to edit
mysql_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 content
while($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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[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 password
mysql_connect("localhost","user","password");

//select which database you want to edit
mysql_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 content
while($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]
Link to comment
Share on other sites

sorry, change last bit so like this:

[code]//display the row
echo "<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]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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! :)
Link to comment
Share on other sites

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 password
mysql_connect("localhost","user","password");

//select which database you want to edit
mysql_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 content
while($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>";
echo "<h3>".$date." by ".$user."</h3>";

$description = explode($search, $description);

foreach ($description as $part) {
echo $part;
echo "<b>".$search."</b>";
}

echo "<hr>";
}
?>
[/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

Sure  :)

[code]<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","pass");

//select which database you want to edit
mysql_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 content
while($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>";
echo "<h3>".$date." by ".$user."</h3>";

$description = explode($search, $description);

foreach ($description as $part) {
echo $part;
echo "<b>".$search."</b>";
}

echo "<hr>";
}
?>[/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

this is the code:

[code]<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","pass");

//select which database you want to edit
mysql_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 content
while($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>";
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 77

This was the code before I edited it:

[code]<?
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("localhost","user","pass");

//select which database you want to edit
mysql_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 content
while($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>";
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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.