Gossamer Forum
Home : Products : Gossamer Links : Discussions :

xml results by category?

Quote Reply
xml results by category?
Do you know how when you do an ordinary search, the results list (called by <%link_results%>) is broken down by category? For example:

Seach results:
category 1
link 1
link 2
link 3
category 2
link 1
category 3
link 1
link 2
link 3

Well do you think I could do this with the xml results feed?
I'm just beginning on this and before I hack things to pieces can anyone out there think of a easy way to approach this?

Many thanks for the cool mod,
Mike
Quote Reply
Re: [Swaylock] xml results by category? In reply to
Well I've managed to figure this out by myself. It's taken me, what...like three weeks? Oh well.

I can't promise that the methods used here are optimized at all or that they will work for you -- but they worked for me.
The following is for Links 2.1.1

In Search.pm add change the following section of sub query

Code:
# Join the link results by category if we are grouping.
my @link_results_loop;
if ($CFG->{build_search_gb}) {
foreach my $cat (sort keys %link_output) {
my $title = Links::Build::build ('title_linked', { name => $cat, complete => 1, home => 0 });
$link_results .= "<p>$title" . join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{$cat}});
$link_output{$cat}->[0]->{title_linked} = $title;
push @link_results_loop, @{$link_output{$cat}};
}
}
else {
$link_results = join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{none}});
push @link_results_loop, @{$link_output{none}};
}

to the following:

Code:

# Join the link results by category if we are grouping.
my @link_results_loop;
if ($CFG->{build_search_gb}) {
foreach my $cat (sort keys %link_output) {
my $title = Links::Build::build ('title_linked', { name => $cat, complete => 1, home => 0 });

# set $cat path == 'Full_Name'
my $cat_path = $cat;

# set $cat_name == 'Name'
my $pos = rindex($cat, '/');
my $cat_name = substr($cat, ($pos > -1 ? $pos + 1 : 0));

$link_results .= "<p>$title" . join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{$cat}});
$link_output{$cat}->[0]->{title_linked} = $title;
$link_output{$cat}->[0]->{cat_path} = $cat_path; # pass $cat_path to link_results_loop
$link_output{$cat}->[0]->{cat_name} = $cat_name; # pass $cat_name
push @link_results_loop, @{$link_output{$cat}};
}
}
else {
$link_results = join ("", map { Links::SiteHTML::display('link', $_) } @{$link_output{none}});
push @link_results_loop, @{$link_output{none}};
}

then I did the following in xml_search_results.xml
Code:

<%--
File : search_results.html
Description : This file displays the search results.
Tags : The following tags are available:
link_results => a listing of all the matching links
link_results_loop => a loop of all matching links
The following derive from a small mod to Search.pm
# includes $cat_path == 'Full_Name'
$cat_name == 'Name'
category_results => a listing of all matching category results
category_results_loop => a loop of matching categories
link_hits => a count of the number of matching links
cat_hits => a count of the number of matching categories
next => a toolbar for the next page of results: 1 2 3 > >>
query => what the user searched for unescaped.
term => what the user searched for escaped, useful for passing into
other search engines

--%>
<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="0.91">
<channel>
<%loop link_results_loop%>
<%-- pull each category from results list.
(you can do this because cat_path for each record
past the first one for a given category will be empty --%>

<%if cat_path ne ""%>
<%set current_cat = $cat_path%>
<%-- (do this so as not to pollute the next loop with this loops value of $cat_path) --%>
<%set cat_path = ""%>
<%set in_cat = 0%>
<cat_item>
<%current_cat%> -- <%cat_name%>
<%-- Now loop throught the results again
and find the results just in this category --%>
<%loop link_results_loop%>
<%if cat_path ne $current_cat and cat_path ne ""%>
<%set in_cat = 0%>
<%nextloop%>
<%elseif cat_path eq "" and in_cat == 0%>
<%nextloop%>
<%elseif cat_path eq "" and in_cat == 1%>
<link_item>
<id><%ID%></id>
<cat_path><%cat_path%></cat_path>
<current_cat><%current_cat%></current_cat>
<in_cat><%in_cat%></in_cat>
<title><%Title%></title>
<link><%URL%></link>
<description><%Description%></description>
<image1><%Image1%></image1>
</link_item>

<%elseif cat_path eq $current_cat%>
<%set in_cat = 1%>
<link_item>
<id><%ID%></id>
<cat_path><%cat_path%></cat_path>
<current_cat><%current_cat%></current_cat>
<in_cat><%in_cat%></in_cat>
<title><%Title%></title>
<link><%URL%></link>
<description><%Description%></description>
<image1><%Image1%></image1>
</link_item>
<%endif%>
<%endloop%>
</cat_item>
<%endloop%>
</channel>
</rss>
Quote Reply
Re: [Swaylock] xml results by category? In reply to
Has anyone done this for the new version to display results by each category?

Thanks
Quote Reply
Re: [socrates] xml results by category? In reply to
Not sure if this is helpful, but you can restrict your XML search results to a category if you use the following query:

Code:
/cgi-bin/search.cgi?query=;sb=ID;so=DESC;catid=1;xml_feed=1
Quote Reply
Re: [socrates] xml results by category? In reply to
Hi,

I have a plugin to do this with a small fee.

Just PM me for the details.

Cheers,

Cheers,

Dat

Programming and creating plugins and templates
Blog