1 (edited by 2008-08-23 12:36:05)

Topic: [HACK] Search results with highlighting and excerpts

Name: highlighted search results
Author: FI-DD
CuteNews Compatibility: 1.3.6 - * (no incompatibility reported yet)
Description: It's an improved way to show the search results.
The search term will be highlighted and shown in an excerpt of the news.
It is template driven and you will have additional quick tags, for example result's title, result's author, result's category.
Demo:  http://democute.de.funpic.de/cute/example2.php
Type "cutenews" in the textbox and press "Enter" to see the results.
Instructions:
(All lines refer to an unchanged cutenews 1.3.6 version of each file.)

Part 1: search.php

1. Find (this line 8):

require_once("$cutepath/inc/functions.inc.php");

and add below:

$cat_lines = file("$cutepath/data/category.db.php");
foreach($cat_lines as $single_line){
    $cat_arr = explode("|", $single_line);    

$cat[$cat_arr[0]] = $cat_arr[1];
    $cat_icon[$cat_arr[0]]=$cat_arr[2];
}

2. Find this (line 233):

////////// Showing Result

                    echo"
[b][url=]$item_arr[2][/url][/b] (". date("d F, Y", $item_arr[0]) .")";
////////// End Showing Result

and replace with this:

////////// Showing Result
include"$cutepath/data/search.tpl";
echo"$search_found";
$search_results = str_replace("{result-title}",$item_arr[2],$search_results);
$search_results = str_replace("{result-author}",$item_arr[1],$search_results);
$search_results = str_replace("{result-cat-id}",$item_arr[6],$search_results);
$search_results = str_replace("{result-category}",$cat[$item_arr[6]],$search_results);
$search_results = str_replace("{result-comments}",countComments($item_arr[0],$archive), $search_results);

if(!empty($item_arr[4])){$item_arr[4] = replace_news("show",$item_arr[4]);
$search_results = str_replace("{result-text}",str_replace("\n",'
',formattext($story,$item_arr[4])),$search_results);
}
else{$item_arr[3] = replace_news("show",$item_arr[3]);
$search_results = str_replace("{result-text}",str_replace("\n",'
',formattext($story,$item_arr[3])),$search_results);
}

$search_results = str_replace("[result-link]", "[url=]",$search_results); //You may add &$user_query here.
$search_results = str_replace("[/result-link]","[/url]", $search_results);
echo"$search_results";
////////// End Showing Result

Part 2: functions.inc.php

Add this at the end of the file before the last ?>

////////////////////////////////////////////////////////
// Function:    formattext

function formattext($whatfind, $html)
{
  $text = strip_tags($html);

  $pos = @strpos(strtoupper($text),strtoupper($whatfind));
  $otstup = 200; //set the number of letters to display for each result
  $result = '';
      if ($pos !== false) 
      {
    if ($pos < $otstup) 
    {
        $result = substr($text, 0, $otstup*2); 
        $result = eregi_replace ($whatfind,'<span style="color:red; background-color: #E9E9E9">'.$whatfind.'</span>',$result)."...";
    }
    else
    {
        $start=$pos-$otstup;
        $result = "...".substr($text, $pos-$otstup, $otstup*2)."...";
        $result = eregi_replace ($whatfind,'<span style="color:red; background-color: #E9E9E9">'.$whatfind.'</span>',$result);
    }
      }
      else
    {
    $result=substr($text, 0, $otstup*2);
    }
  return $result;
}

Part 3: options.mdu

1. Find this (line 370):

    [b]Edit Template Parts[/b]<table width="100%"><form method=post action="'.$PHP_SELF.'">
<tr> <!- start active news -->

and replace with this:

    [b]Edit Template Parts[/b]<table width="100%"><form method=post action="'.$PHP_SELF.'">';

if($do_template == 'search')
{
echo'
    <tr>
    <td height="7"  bgcolor=#F7F6F4 colspan="2">
    [b][url=j]search results[/url][/b]</tr>
    <tr id=\'active-news1\' '.$tr_hidden.'>
    <td height="9" width="200" valign="top">
    [b]{result-title}

    {result-author}

    {result-category}

    {result-cat-id}

    {result-comments}

    {result-text}

    [result-link] [/b] [b] [/result-link]

    <td height=\"9\"  valign=\"top\" width=430>
    - The title of the result

    - The author of the result

    - The category of the result 

    - The category ID of the result

    - Number of comments for the result

    - Excerpt of the news containing the search term

    - Link to the full story

    </tr>
    <tr id=\'active-news2\' '.$tr_hidden.'>
    <td height="8" colspan="2">
    <textarea rows="9" cols="98" name="search_results_act">'.$search_results.'</textarea>
    

    </tr>
    <tr>
    <td height="8"  colspan="2">
    <input type=hidden name=mod value=options>
    <input type=hidden name=action value=dosavetemplates>
    <input type=hidden name=do_template value="'.$do_template.'">
    
<input type=submit value="   Save Changes   " accesskey="s">
    </tr></form>
    </table>';
}
else {
echo '
<tr> <!- start active news -->


2. Find this:

        echofooter();
}
// ********************************************************************************
// Do Save Changes to Templates
// ********************************************************************************
elseif($action == "dosavetemplates")
{
           if($member_db[1] != 1){ msg("error", "Access Denied", "You don't have permissions for this type of action"); }
    $templates_names = array("edit_active", "edit_comment", "edit_form", "edit_full", "edit_prev_next", "edit_comments_prev_next");
    foreach($templates_names as $template)
    {
                $$template = stripslashes($$template);
    }

    if($do_template == "" or !$do_template){ $do_template = "Default"; }
    $template_file = "./data/${do_template}.tpl";

        $handle = fopen("$template_file","w");
    fwrite($handle, "<?PHP\n///////////////////// TEMPLATE $do_template /////////////////////\n");
    fwrite($handle, "\$template_active = <<<HTML\n$edit_active\nHTML;\n\n\n");
        fwrite($handle, "\$template_full = <<<HTML\n$edit_full\nHTML;\n\n\n");
    fwrite($handle, "\$template_comment = <<<HTML\n$edit_comment\nHTML;\n\n\n");
    fwrite($handle, "\$template_form = <<<HTML\n$edit_form\nHTML;\n\n\n");
    fwrite($handle, "\$template_prev_next = <<<HTML\n$edit_prev_next\nHTML;\n");
    fwrite($handle, "\$template_comments_prev_next = <<<HTML\n$edit_comments_prev_next\nHTML;\n");
    fwrite($handle, "?>\n");


   
    and replace with this:
   

}
        echofooter();
}
// ********************************************************************************
// Do Save Changes to Templates
// ********************************************************************************
elseif($action == "dosavetemplates")
{
           if($member_db[1] != 1){ msg("error", "Access Denied", "You don't have permissions for this type of action"); }
    $templates_names = array("edit_active", "edit_comment", "edit_form", "edit_full", "edit_prev_next", "edit_comments_prev_next");
    foreach($templates_names as $template)
    {
                $$template = stripslashes($$template);
    }

    if($do_template == 'search')
    {
    $template_file = "./data/${do_template}.tpl";
    $handle = fopen("$template_file","w");
    fwrite($handle, "<?PHP\n///////////////////// TEMPLATE $do_template /////////////////////\n");
    fwrite($handle, "\$search_results = <<<HTML\n$search_results_act\nHTML;\n");
    fwrite($handle, "?>\n");
    }

    else
    {

    if($do_template == "" or !$do_template){ $do_template = "Default"; }
    $template_file = "./data/${do_template}.tpl";

        $handle = fopen("$template_file","w");
    fwrite($handle, "<?PHP\n///////////////////// TEMPLATE $do_template /////////////////////\n");
    fwrite($handle, "\$template_active = <<<HTML\n$edit_active\nHTML;\n\n\n");
        fwrite($handle, "\$template_full = <<<HTML\n$edit_full\nHTML;\n\n\n");
    fwrite($handle, "\$template_comment = <<<HTML\n$edit_comment\nHTML;\n\n\n");
    fwrite($handle, "\$template_form = <<<HTML\n$edit_form\nHTML;\n\n\n");
    fwrite($handle, "\$template_prev_next = <<<HTML\n$edit_prev_next\nHTML;\n");
    fwrite($handle, "\$template_comments_prev_next = <<<HTML\n$edit_comments_prev_next\nHTML;\n");
    fwrite($handle, "?>\n");
}

<b>Part 4: search.tpl[/b]

In cutenews/data create a file called "search.tpl" and place inside something like this:

<?PHP
///////////////////// TEMPLATE search /////////////////////
$search_results = <<<HTML
<div align=left>[result-link]{result-title}[/result-link]</div>
<div align=left><p>{result-text}</p></div>
<table width=100%><tr><td width=50% align=left>[b]{result-category}[/b]</td><td width=50% align=right>[result-link]Read more...[/result-link]</td></tr></table><hr>

HTML;
?>

Re: [HACK] Search results with highlighting and excerpts

Oooh man, this is a useful hack! Thanks a lot

Re: [HACK] Search results with highlighting and excerpts

Very Nice.. I shall move this to the hacks area.