Gossamer Forum
Home : Products : Gossamer Links : Version 1.x :

Link Counting Problem (Search Results)

Quote Reply
Link Counting Problem (Search Results)
Pugdog/Eliot etc.

I started a new thread for this problem because the other one was getting very long and messy! I have now made sure that $links_count is only defined once but there is still problems.

The links are still being numbered 1 to 10 on each of the search results pages. They should be 1 to 10 on the first page, 11 to 20 on the second etc.

Here is the code as it nows:


sub search {
# ---------------------------------------------------
# Performs the actual search.
#
my ($in, $dynamic) = @_;
my ($mh, $bool, $nh, $ww, $order, $query, $ignored, %seen, $next,
$catdb, $cat_hits, $category_results, $cat_count, $cat_errors, $linkdb, $link_hits, $link_results, $link_count, $link_errors);
my %in = %{&cgi_to_hash($in)};
my $links_count = '0';

# Get/Set the search options.
($in->param('mh') =~ /^(10|25|50|100)$/) ? ($mh = $1) : ($mh = 10);
($in->param('bool') =~ /^(and|or)$/i) ? ($bool = uc $1) : ($bool = 'AND');
($in->param('nh') =~ /^(\d+)$/) ? ($nh = $1) : ($nh = 1);
($in->param('substring')) ? ($ww = 0) : ($ww = 1);
($in->param('order') =~ /^(score|category)$/i) ? ($order = uc $1) : ($order = 'CATEGORY');

# Split up the search term.
$query = $in->param('query');
$query or &site_html_search_failure ( { error => "No search term entered.", %in }, $dynamic) and return;
$in{'term'} = $in->escape ($query);

# Search the category listings.
$catdb = new Links::DBSQL "$LINKS{admin_root_path}/defs/Category.def";
$cat_hits = $catdb->query ( { query => $query, mh => $mh, nh => $nh } );
$cat_count = $catdb->hits || 0;
$cat_errors = $catdb->query_errors;

# Now let's search the links table, but first figure out any filters.
$linkdb = new Links::DBSQL "$LINKS{admin_root_path}/defs/Links.def";
my %filter = ();
foreach my $col (@{$linkdb->{db_cols}}) {
if ($in->param($col)) {
$filter{$col} = $in->param($col);
}
}
$link_hits = $linkdb->query ( { query => $query, mh => $mh, nh => $nh, filter => \%filter, ww => $ww } );
$link_count = $linkdb->hits || 0;
$link_errors = $linkdb->query_errors;

# Format the errors (if any)
if (ref $link_errors eq 'HASH') {
foreach my $word (keys %$link_errors) {
next if ($seen{$word}++);
$ignored .= "<li>$word - " . $link_errors->{$word};
}
}
if (ref $cat_errors eq 'HASH') {
foreach my $word (keys %$cat_errors) {
next if ($seen{$word}++);
$ignored .= "<li>$word - " . $cat_errors->{$word};
}
}

# Return if we don't have any matches.
unless ($link_count or $cat_count) {
&log_query ($in->param('query'), 0);
&site_html_search_failure ({ error => "No matching links.", ignored => $ignored, %in }, $dynamic);
return;
}

# Only display the category output on the first page.
if ($nh == 1) {
if ($cat_hits) {
foreach my $hit (@$cat_hits) {
$hit = $catdb->array_to_hash($hit);
$category_results .= "<li>" . &search_build_linked_title ($hit->{Name}) . "\n";
}
}
}

# Build the list of link results.
if ($link_hits) {
my (%link_results, %displayed, $name);
foreach my $hit (@$link_hits) {
$hit = $linkdb->array_to_hash ($hit);
$hit-> {'$links_count'} = $links_count+1;
$hit-> {'links_count'} = ++$links_count;
$link_results .= &site_html_link ($hit, $dynamic);
}
# foreach my $hit (@$link_hits) {
# next if ($displayed{$hit->{CategoryID}}++);
# $name = &get_category_name ($hit->{CategoryID});
# $link_results .= "<p>" . &search_build_linked_title ($name) . "</p>";
# $link_results .= $link_results{$hit->{CategoryID}};
# }
}

# Log the search results.
&log_query ($in->param('query'), $link_count + $cat_count);

# If we want to bold the search terms...
if ($LINKS{search_bold}) {
my $tempquery = $query;
$tempquery =~ s/[+\-"']//g;
my @terms = split /\s/, $tempquery;
foreach (@terms) {
if (s/(.+)\*(.*)/$1/) {
push @terms, $2 if ($2);
$link_results =~ s,(<[^>]+>)|(\Q$_\E),defined($1) ? $1 : "<B>$2</B>",gie;
$category_results =~ s,(<[^>]+>)|(\Q$_\E),defined($1) ? $1 : "<B>$2</B>",gie;
}
else {
$link_results =~ s,(<[^>]+>)|(\Q$_\E),defined($1) ? $1 : "<B>$2</B>",gie;
$category_results =~ s,(<[^>]+>)|(\Q$_\E),defined($1) ? $1 : "<B>$2</B>",gie;
}
}
}

# Search toolbar.
($link_count > $mh) and ($next = $linkdb->toolbar());

# Print out the HTML results.
&site_html_search_results ( { link_results => $link_results, category_results => $category_results,
link_hits => $link_count, cat_hits => $cat_count, next => $next, ignored => $ignored, %in }, $dynamic );
}




Why is it doing this? I have followed all your instructions but it still goes to 1 to 10 on each page. Why is it not counting properly?!

Geoff


Quote Reply
Re: Link Counting Problem (Search Results) In reply to
Wow.... this took a lot. I never looked that closely at the routine.

I find it really hard to follow the code in the forum,, but...

Try doing this:

At the end of the input parameters, put:


Code:
# Get/Set the search options.
($in->param('mh') =~ /^(10|25|50|100)$/) ? ($mh = $1) : ($mh = 25);
($in->param('bool') =~ /^(and|or)$/i) ? ($bool = uc $1) : ($bool = 'AND');
($in->param('nh') =~ /^(\d+)$/) ? ($nh = $1) : ($nh = 1);
($in->param('substring')) ? ($ww = 0) : ($ww = 1);
($in->param('ww')) ? ($ww = 1) : ($ww = 0);
($in->param('order') =~ /^(score|category)$/i) ? ($order = uc $1) : ($order = 'CATEGORY');
.

ADD:


$links_count = $mh * ($nh - 1);



http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/

Quote Reply
Re: Link Counting Problem (Search Results) In reply to
Pugdog you are a hero! Smile

It worked like a dream - and believe me I had to pinch myself to see it work at last!

JeffB

Quote Reply
Re: Link Counting Problem (Search Results) In reply to
It was an obscure thing... this routine doesn't quite work the way the other ones work. It takes a passed starting point (nh) and the number of hits to return (mh) and figures out where to start processing from that.

Once I realized that, it made sense -- since you are only returning one search page at a time, and you need to know where to start each time, there is no loop counter, only a starting point, and max_hits to return.

http://www.postcards.com
FAQ: http://www.postcards.com/FAQ/LinkSQL/