Jump to content

how to BOLD search terms in results??


techiefreak05

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]
Link to comment
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]
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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]
Link to comment
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"
Link to comment
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 :)
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.