Jump to content

Archived

This topic is now archived and is closed to further replies.

techiefreak05

how to BOLD search terms in results??

Recommended Posts

you know google does it. but how to do it. when you search .. in your results.. it makes your search terms bold within the results.. how do i do that?

oh, heres my search script:
[code]<b>Search by Username:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Username:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="query"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtn" value="-Search-"></td></tr>
</table>
</form><br>
<b>Search by Email:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Email:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryEM"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnEM" value="-Search-"></td></tr>
</table>
</form>
<b>Search by First Name:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">First Name:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryFN"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnFN" value="-Search-"></td></tr>
</table>
</form>
<br>
<br><hr><br>
<b><u><font color=red>Your Results:</font></u></b><br>

<?
$end = round(microtime(true) - $start, 3);
echo 'Query completed in <b>' . $end . '</b> seconds';
?>

<center>
<?php
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a><hr width=50%><br>";
}
}

if($_POST[queryBtnEM]){
$sql = "SELECT * FROM users WHERE email LIKE '%$_POST[queryEM]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Email: " .$row[email]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a><hr width=50%><br>";
}
}

if($_POST[queryBtnFN]){
$sql = "SELECT * FROM users WHERE fname LIKE '%$_POST[queryFN]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "First Name:" .$row[fname]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>" .$row[username]. "</a><hr width=50%><br>";
}
}
?>[/code]

Share this post


Link to post
Share on other sites
here is an example:

[code]
<?php
$searchword = "the";
$searchresult = "the quick brown fox jumps over the fence.";

$formattedresult = eregi_replace($searchword, '<b>'.$searchword.'</b>', $searchresult);
echo $formattedresult;
?>
[/code]

Share this post


Link to post
Share on other sites
uh, you would replace $searchword with the word you searched for in your original query, and replace $searchresult with the result  you got from the search..?

Share this post


Link to post
Share on other sites
well wait! when i search for anything that has a "b" in it.. it shows my <br> tags .. and its not supposed to.

Share this post


Link to post
Share on other sites
Here is the updated code:

[code]
<b>Search by Username:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Username:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="query"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtn" value="-Search-"></td></tr>
</table>
</form><br>
<b>Search by Email:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Email:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryEM"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnEM" value="-Search-"></td></tr>
</table>
</form>
<b>Search by First Name:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">First Name:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryFN"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnFN" value="-Search-"></td></tr>
</table>
</form>
<br>
<br><hr><br>
<b><u><font color=red>Your Results:</font></u></b><br>

<?
$end = round(microtime(true) - $start, 3);
echo 'Query completed in <b>' . $end . '</b> seconds';
?>

<center>
<?php
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>".$row[username]."</b></a><hr width=50%><br>";
}
}

if($_POST[queryBtnEM]){
$sql = "SELECT * FROM users WHERE email LIKE '%$_POST[queryEM]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Email: " .$row[email]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>".$row[username]."</b></a><hr width=50%><br>";
}
}

if($_POST[queryBtnFN]){
$sql = "SELECT * FROM users WHERE fname LIKE '%$_POST[queryFN]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "First Name:" .$row[fname]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>" .$row[username]. "</b></a><hr width=50%><br>";
}
}
?>
[/code]

Share this post


Link to post
Share on other sites
yeah, but ididnt post the form.. cuz i didnt change it..

[code]<?php
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$searchword = "$_POST[query]";
$searchresult = "Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a><hr width=50%><br>";
$formattedresult = eregi_replace($searchword, '<b>'.$searchword.'</b>', $searchresult);
echo $formattedresult;
}
}

if($_POST[queryBtnEM]){
$sql = "SELECT * FROM users WHERE email LIKE '%$_POST[queryEM]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$searchword = "$_POST[queryEM]";
$searchresult = "Email: " .$row[email]. "<br>Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a><hr width=50%><br>";
$formattedresult = eregi_replace($searchword, '<b>'.$searchword.'</b>', $searchresult);
echo $formattedresult;

}
}

if($_POST[queryBtnFN]){
$sql = "SELECT * FROM users WHERE fname LIKE '%$_POST[queryFN]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "First Name:" .$row[fname]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>" .$row[username]. "</a><hr width=50%><br>";
}
}
?>[/code]

it works yes .. but when i search for "b" .. it shows my <br> tags .. and i know why.. cuz its included in the $searchresult echo .. but idk how to fix it. there is  asimple way to fix it.. stop the user from searching for less than 3 charaters.. and im about to try that.

Share this post


Link to post
Share on other sites
...crap.. when i echo the following:

[code]Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a>[/code]
and the search terms are bolded ..

the link is: [code][color=red]getInfo.php?user=<b>searchtermhere</b>[/color][/code]
[code]and thats a BAD thing! cuz the "<b>" and "</b>" are included in the url[/code]

Share this post


Link to post
Share on other sites
oh i get it. well how about doing  for instance:

[code]
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$searchword = "$_POST[query]";
$searchresult = $row['username'];
$formattedresult = eregi_replace($searchword, '<b>'.$searchword.'</b>', $searchresult);
echo "Username: <a href='getInfo.php?user=" .$formattedresult. "'>".$formattedresult."</a><hr width=50%><br>";

}
}
[/code]

Share this post


Link to post
Share on other sites
Just use the following code

[code]
<b>Search by Username:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Username:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="query"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtn" value="-Search-"></td></tr>
</table>
</form><br>
<b>Search by Email:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">Email:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryEM"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnEM" value="-Search-"></td></tr>
</table>
</form>
<b>Search by First Name:</b>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td bgcolor="#3399CC"><font color="black">First Name:</font></td></tr>
<tr><td bgcolor="#3399CC"><input type="input" name="queryFN"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="queryBtnFN" value="-Search-"></td></tr>
</table>
</form>
<br>
<br><hr><br>
<b><u><font color=red>Your Results:</font></u></b><br>

<?
$end = round(microtime(true) - $start, 3);
echo 'Query completed in <b>' . $end . '</b> seconds';
?>

<center>
<?php
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>".$row[username]."</b></a><hr width=50%><br>";
}
}

if($_POST[queryBtnEM]){
$sql = "SELECT * FROM users WHERE email LIKE '%$_POST[queryEM]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Email: " .$row[email]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>".$row[username]."</b></a><hr width=50%><br>";
}
}

if($_POST[queryBtnFN]){
$sql = "SELECT * FROM users WHERE fname LIKE '%$_POST[queryFN]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "First Name:" .$row[fname]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'><b>" .$row[username]. "</b></a><hr width=50%><br>";
}
}
?>
[/code]

Share this post


Link to post
Share on other sites
CrayonViolet:

it 50% works! it doesnt display my br tags.. BUT it still has the b tags in the url .. which brings up a PAGE CANNOT BE DISPLAYED browser error...

Share this post


Link to post
Share on other sites
joking, that code will not work. if you have the string "the quick brown fox" and you searched for "fox" and it returns the string, your code bolds "the quick brown fox" whereas the goal here is to echo the whole string, but have just "fox" bolded.

Share this post


Link to post
Share on other sites
okay repost your code again. i tried the code block i gave you and it seems to work just fine for me...

Share this post


Link to post
Share on other sites
haha crayonviolet, i was just gonna say that!! .. so, do you know ho to stop it from putting the b tags in the link.. lets say i searched for "bren".. that would display one result.. as <b>bren</b>den .. ok good, BUT the link will be: [code]getInfo.php?user=<b>bren</b>den[/code] where its supposed to be: [code]getInfo.php?user=brenden[/code]

Share this post


Link to post
Share on other sites
i still have the code you posted that DID get rid of showing the br tags in the results.. i hacent chnged anything in the code u gave me.

Share this post


Link to post
Share on other sites
see that's what i don't get. i tried that code on my own server and it seems to work just fine.  surely there must be a typo somewheres

Share this post


Link to post
Share on other sites
heres the code i have ..
[code]<?php
if($_POST[queryBtn]){
$sql = "SELECT * FROM users WHERE username LIKE '%$_POST[query]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
$searchword = "$_POST[query]";
$searchresult = $row['username'];
$formattedresult = eregi_replace($searchword, '<b>'.$searchword.'</b>', $searchresult);
echo "Username: <a href='getInfo.php?user=" .$formattedresult. "'>".$formattedresult."</a><hr width=50%><br>";

}
}

if($_POST[queryBtnEM]){
$sql = "SELECT * FROM users WHERE email LIKE '%$_POST[queryEM]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "Email: " .$row[email]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>".$row[username]."</a><hr width=50%><br>";
}
}

if($_POST[queryBtnFN]){
$sql = "SELECT * FROM users WHERE fname LIKE '%$_POST[queryFN]%'";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "First Name:" .$row[fname]. "<br>";
echo "Username: <a href='getInfo.php?user=" .$row[username]. "'>" .$row[username]. "</a><hr width=50%><br>";
}
}
?>[/code]

or you could click my giant green logo and register(its a whole 5 fields! lol) quick on my site and go to search and search for "bren" or "br" or something in "brenden"

Share this post


Link to post
Share on other sites

replace

echo "Username: <a href='getInfo.php?user=" .[b]$formattedresult[/b]. "'>".$formattedresult."</a><hr width=50%><br>";

with

echo "Username: <a href='getInfo.php?user=" .[b]$searchword[/b]. "'>".$formattedresult."</a><hr width=50%><br>";

Share this post


Link to post
Share on other sites
or my bad, replace the link's $formattedresult to the $searchresult.

[code]
echo "Username: <a href='getInfo.php?user=" .$searchresult. "'>".$formattedresult."</a><hr width=50%><br>";
[/code]

Share this post


Link to post
Share on other sites
yeah, i was gonna say :P lol.. ill try it. so.. what do you think of my site so far? not to get off topic

Share this post


Link to post
Share on other sites
mmm...aah... well green isn't really my color, but that's just me.  looks like it's comin' along good so far though..

i saw that the links seemed to work, but when i went back and tried again, i got this error:

Fatal error: Cannot use [] for reading in /home/www/zycoworld.flamelicker.com/newlayout/search.php on line 57

seems you fubared something :)

Share this post


Link to post
Share on other sites
i suggest having the login redirect to the home page instead of dumping you back at the login page. also you should condense your searchboxes into one box with checkboxed options.  if you want more advice then i suggest making a new thread in the website critiques forum.

Share this post


Link to post
Share on other sites

×

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.