Forum Thread
  Posts  
[PHP] Highlighting a search string (Forums : Coding & Scripting : [PHP] Highlighting a search string) Locked
Thread Options
May 3 2003 Anchor

ob_start();
highlight_string($search);
$colorsource = ob_get_contents();
ob_end_clean();

I tried that, and it didnt work, caused so many errors :/

Heres the actual search page code!

search.php wrote: <?

$query = mysql_query("SELECT * FROM unz_news WHERE info LIKE '%$search%' OR title LIKE '%$search%' ORDER BY id DESC LIMIT 9");
$num_rows = mysql_num_rows($query);
if($num_rows <= 0)

{print "<font face='$font' size='2'>No results for $search, try again...</font>";}

else

{while($search = mysql_fetch_object($query))
{{{$search->info = nl2br($search->info);
$search->info = parseCode($search->info);

if ($search->infolong != '')
{$fullsearch = " - <a href='index.php?section=fullnews&id=$search->id' class='navbar'>View the full story</a>";}
else
{$fullsearch = "";}

if ($search->source != '')
{$source = " - <a href='$search->source' target='_blank' class='navbar'>News Source</a>";}
else
{$source = "";}

if ($search->important == 'yes')
{$important = "<img src='images/icons/important.gif' title='Important'>";}
else
{$important = "";}

if($search->nocom > 13)
{$hot = "<img src='images/icons/flame.gif' title='Hot topic!'>";}
else
{$hot = "";}

if($search->nocom >0)
{$nocomments = "<a href ='index.php?section=fullnews&id=$search->id' class='navbar'>$search->nocom Comments</a>";}
else
{$nocomments = "<a href ='index.php?section=fullnews&id=$search->id'>Comment</a>";}}}

print "<table width='100%' border='1' align='center' cellpadding='3' cellspacing='0' bordercolor='#000000'>
<tr align='left' valign='top'>
<td colspan='2' background='images/bg/bg_grey.gif' class='standard' valign="middle" ><font color='#FFFFFF' face='$font' size='2'><strong>$search->title $hot $important</font></strong></font></td>
</tr>
<tr align='left' valign='top'>
<td colspan='2' background='images/bg/bg_table.gif' class='standard'><font face='$font' size='2'>$search->info

<strong>:: $search->username :: " . datetimeformat("j/n/y g:i", $search->date) . " ::</strong></font></td>
</tr>
<td background='images/bg/bg_grey.gif' class='standard'><font face='$font' size='2' color='#FFFFFF' >[$nocomments$source$fullsearch] Read $search->noread times</font></table>
";}}

?>


- Edited By azz0r On Sat 3rd, May 2003 @ 2:20:10pm

--

Why wont it save me?

BigBird
BigBird Mod DB Pilot
May 3 2003 Anchor

/me helps azz0r out in mIRC

--

BigBird

Mod DB Guy
Play Something Different

BigBird
BigBird Mod DB Pilot
May 3 2003 Anchor
&lt;?

$query = mysql_query(&quot;SELECT * FROM unz_news WHERE info LIKE &#039;%$search%&#039; OR title LIKE &#039;%$search%&#039; ORDER BY id DESC LIMIT 9&quot;);
$num_rows = mysql_num_rows($query);
if($num_rows &lt;= 0)

{print &quot;&lt;font face=&#039;$font&#039; size=&#039;2&#039;&gt;No results for $search, try again...&lt;/font&gt;&quot;;}

else

//BigBird - Start
function callback($buffer) {
return (ereg_replace($search, &quot;<b>&quot;.$search.&quot;</b>&quot;, $buffer)); // Bolds the search string
}
ob_start(&quot;callback&quot;);
//BigBird - End

{while($search = mysql_fetch_object($query))
{{{$search-&gt;info = nl2br($search-&gt;info);
$search-&gt;info = parseCode($search-&gt;info);

if ($search-&gt;infolong != &#039;&#039;)
{$fullsearch = &quot; - &lt;a href=&#039;index.php?section=fullnews&id=$search-&gt;id&#039; class=&#039;navbar&#039;&gt;View the full story&lt;/a&gt;&quot;;}
else
{$fullsearch = &quot;&quot;;}


if ($search-&gt;source != &#039;&#039;)
{$source = &quot; - &lt;a href=&#039;$search-&gt;source&#039; target=&#039;_blank&#039; class=&#039;navbar&#039;&gt;News Source&lt;/a&gt;&quot;;}
else
{$source = &quot;&quot;;}


if ($search-&gt;important == &#039;yes&#039;)
{$important = &quot;&lt;img src=&#039;images/icons/important.gif&#039; title=&#039;Important&#039;&gt;&quot;;}
else
{$important = &quot;&quot;;}


if($search-&gt;nocom &gt; 13)
{$hot = &quot;&lt;img src=&#039;images/icons/flame.gif&#039; title=&#039;Hot topic!&#039;&gt;&quot;;}
else
{$hot = &quot;&quot;;}


if($search-&gt;nocom &gt;0)
{$nocomments = &quot;&lt;a href =&#039;index.php?section=fullnews&id=$search-&gt;id&#039; class=&#039;navbar&#039;&gt;$search-&gt;nocom Comments&lt;/a&gt;&quot;;}
else
{$nocomments = &quot;&lt;a href =&#039;index.php?section=fullnews&id=$search-&gt;id&#039;&gt;Comment&lt;/a&gt;&quot;;}}}


print &quot;&lt;table width=&#039;100%&#039; border=&#039;1&#039; align=&#039;center&#039; cellpadding=&#039;3&#039; cellspacing=&#039;0&#039; bordercolor=&#039;#000000&#039;&gt;
&lt;tr align=&#039;left&#039; valign=&#039;top&#039;&gt;
&lt;td colspan=&#039;2&#039; background=&#039;images/bg/bg_grey.gif&#039; class=&#039;standard&#039; valign=&quot;middle&quot; &gt;&lt;font color=&#039;#FFFFFF&#039; face=&#039;$font&#039; size=&#039;2&#039;&gt;&lt;strong&gt;$search-&gt;title $hot $important&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr align=&#039;left&#039; valign=&#039;top&#039;&gt;
&lt;td colspan=&#039;2&#039; background=&#039;images/bg/bg_table.gif&#039; class=&#039;standard&#039;&gt;&lt;font face=&#039;$font&#039; size=&#039;2&#039;&gt;$search-&gt;info

&lt;strong&gt;:: $search-&gt;username :: &quot; . datetimeformat(&quot;j/n/y g:i&quot;, $search-&gt;date) . &quot; ::&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;td background=&#039;images/bg/bg_grey.gif&#039; class=&#039;standard&#039;&gt;&lt;font face=&#039;$font&#039; size=&#039;2&#039; color=&#039;#FFFFFF&#039; &gt;[$nocomments$source$fullsearch] Read $search-&gt;noread times&lt;/font&gt;&lt;/table&gt;
&quot;;

//BigBird - Start
ob_end_flush();
//BigBird - End
}}

?&gt;

- Edited By BigBird On Fri 2nd, May 2003 @ 11:16:40pm

- Edited By BigBird On Fri 2nd, May 2003 @ 11:22:51pm
- Edited By BigBird On Fri 2nd, May 2003 @ 11:33:13pm

--

BigBird

Mod DB Guy
Play Something Different

May 3 2003 Anchor

Dont work d00d

--

Why wont it save me?

BigBird
BigBird Mod DB Pilot
May 3 2003 Anchor

i'll have a look in the morning... i'm fkn tired and working on drunk :beer: so yeh :| i'll look at it tomorrow :)

--

BigBird

Mod DB Guy
Play Something Different

May 3 2003 Anchor

Aight!

--

Why wont it save me?

PsychoFarmer
PsychoFarmer modDB King
May 3 2003 Anchor

drunk coders, sounds like a good time :P

--

___________________________
Today seems like a good day to burn a bridge or two
I am the freakiest man in the world!!!!
I beg to differ, on the contrary, I agree with every word that you say

User Posted Image

BigBird
BigBird Mod DB Pilot
May 3 2003 Anchor

tis the way to go on a saturday night if i'm not at a party, i get drunk n play games :) or sometimes chat to chicks i know on icq :) but most of the time they're at partys gettin drunk :(

/me look tru the code again

--

BigBird

Mod DB Guy
Play Something Different

May 4 2003 Anchor

~Its a bitch to code~

--

Why wont it save me?

BigBird
BigBird Mod DB Pilot
May 4 2003 Anchor

tellin me :s

--

BigBird

Mod DB Guy
Play Something Different

PsychoFarmer
PsychoFarmer modDB King
May 4 2003 Anchor

so, essentially what ur doing is making a search function that highlights the thing that ur searching for, correct?

--

___________________________
Today seems like a good day to burn a bridge or two
I am the freakiest man in the world!!!!
I beg to differ, on the contrary, I agree with every word that you say

User Posted Image

May 4 2003 Anchor

Yep...

--

Why wont it save me?

PsychoFarmer
PsychoFarmer modDB King
May 4 2003 Anchor

doesnt google and such already do that?

--

___________________________
Today seems like a good day to burn a bridge or two
I am the freakiest man in the world!!!!
I beg to differ, on the contrary, I agree with every word that you say

User Posted Image

May 4 2003 Anchor

Kindof, wors abit diffrent though. Anywho I have the code, just need some help with arrangement.

--

Why wont it save me?

PsychoFarmer
PsychoFarmer modDB King
May 4 2003 Anchor

does urs work yet then? how would u highlight things anyway? i dunno the html code for it even

--

___________________________
Today seems like a good day to burn a bridge or two
I am the freakiest man in the world!!!!
I beg to differ, on the contrary, I agree with every word that you say

User Posted Image

May 4 2003 Anchor

Well its to complex, everything needed is in the first post, anywho I only want serious help now, so shush.

--

Why wont it save me?

PsychoFarmer
PsychoFarmer modDB King
May 4 2003 Anchor

roger that, pf out

--

___________________________
Today seems like a good day to burn a bridge or two
I am the freakiest man in the world!!!!
I beg to differ, on the contrary, I agree with every word that you say

User Posted Image

May 5 2003 Anchor

I need some help :(

--

Why wont it save me?

BigBird
BigBird Mod DB Pilot
May 5 2003 Anchor

[INVALIDquote=BigBird on Fri 2nd, May 2003 @ 11:14:53pm]

&lt;?

$query = mysql_query(&quot;SELECT * FROM unz_news WHERE info LIKE &#039;%$search%&#039; OR title LIKE &#039;%$search%&#039; ORDER BY id DESC LIMIT 9&quot;);
$num_rows = mysql_num_rows($query);
if($num_rows &lt;= 0)

{print &quot;&lt;font face=&#039;$font&#039; size=&#039;2&#039;&gt;No results for $search, try again...&lt;/font&gt;&quot;;}

else

//BigBird - Start
function callback($buffer) {
return (ereg_replace($search-&gt;info, &quot;<b>&quot;.$search-&gt;info.&quot;</b>&quot;, $buffer)); // Bolds the search string
}
ob_start(&quot;callback&quot;);
//BigBird - End

{while($search = mysql_fetch_object($query))
{{{$search-&gt;info = nl2br($search-&gt;info);
$search-&gt;info = parseCode($search-&gt;info);

if ($search-&gt;infolong != &#039;&#039;)
{$fullsearch = &quot; - &lt;a href=&#039;index.php?section=fullnews&id=$search-&gt;id&#039; class=&#039;navbar&#039;&gt;View the full story&lt;/a&gt;&quot;;}
else
{$fullsearch = &quot;&quot;;}


if ($search-&gt;source != &#039;&#039;)
{$source = &quot; - &lt;a href=&#039;$search-&gt;source&#039; target=&#039;_blank&#039; class=&#039;navbar&#039;&gt;News Source&lt;/a&gt;&quot;;}
else
{$source = &quot;&quot;;}


if ($search-&gt;important == &#039;yes&#039;)
{$important = &quot;&lt;img src=&#039;images/icons/important.gif&#039; title=&#039;Important&#039;&gt;&quot;;}
else
{$important = &quot;&quot;;}


if($search-&gt;nocom &gt; 13)
{$hot = &quot;&lt;img src=&#039;images/icons/flame.gif&#039; title=&#039;Hot topic!&#039;&gt;&quot;;}
else
{$hot = &quot;&quot;;}


if($search-&gt;nocom &gt;0)
{$nocomments = &quot;&lt;a href =&#039;index.php?section=fullnews&id=$search-&gt;id&#039; class=&#039;navbar&#039;&gt;$search-&gt;nocom Comments&lt;/a&gt;&quot;;}
else
{$nocomments = &quot;&lt;a href =&#039;index.php?section=fullnews&id=$search-&gt;id&#039;&gt;Comment&lt;/a&gt;&quot;;}}}


print &quot;&lt;table width=&#039;100%&#039; border=&#039;1&#039; align=&#039;center&#039; cellpadding=&#039;3&#039; cellspacing=&#039;0&#039; bordercolor=&#039;#000000&#039;&gt;
&lt;tr align=&#039;left&#039; valign=&#039;top&#039;&gt;
&lt;td colspan=&#039;2&#039; background=&#039;images/bg/bg_grey.gif&#039; class=&#039;standard&#039; valign=&quot;middle&quot; &gt;&lt;font color=&#039;#FFFFFF&#039; face=&#039;$font&#039; size=&#039;2&#039;&gt;&lt;strong&gt;$search-&gt;title $hot $important&lt;/font&gt;&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr align=&#039;left&#039; valign=&#039;top&#039;&gt;
&lt;td colspan=&#039;2&#039; background=&#039;images/bg/bg_table.gif&#039; class=&#039;standard&#039;&gt;&lt;font face=&#039;$font&#039; size=&#039;2&#039;&gt;$search-&gt;info

&lt;strong&gt;:: $search-&gt;username :: &quot; . datetimeformat(&quot;j/n/y g:i&quot;, $search-&gt;date) . &quot; ::&lt;/strong&gt;&lt;/font&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;td background=&#039;images/bg/bg_grey.gif&#039; class=&#039;standard&#039;&gt;&lt;font face=&#039;$font&#039; size=&#039;2&#039; color=&#039;#FFFFFF&#039; &gt;[$nocomments$source$fullsearch] Read $search-&gt;noread times&lt;/font&gt;&lt;/table&gt;
&quot;;

//BigBird - Start
ob_end_flush();
//BigBird - End
}}

?&gt;

[INVALIDquote]

McTry that azz0r

--

BigBird

Mod DB Guy
Play Something Different

Liothen
Liothen Dogmatic Law
May 9 2003 Anchor

Whats your list of errors?

--

Mod DB Old Timer

BigBird
BigBird Mod DB Pilot
May 9 2003 Anchor

umm it's not my code, and as far as i know azz0r is no longer around moddb...
i spose this thread shall remain open/undeleted because of it's coding relevence

--

BigBird

Mod DB Guy
Play Something Different

May 10 2003 Anchor

Parse error: parse error, unexpected T_FUNCTION in /home/www/unknownz/v2/pages/search.php on line 12

--

Why wont it save me?

Reply to thread
click to sign in and post

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.