For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > February 2005 > UNION statement to sort search results









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author UNION statement to sort search results
Jan Eden

2005-02-14, 8:55 am

Hi,

I wrote a search script which can search only the page titles and author n=
ames for articles on my site or the titles/authors and the pages' content.

To get the results for the two types in two sets, I used a UNION construct:

if ($search_string) {

my $where_author_title =3D qq{(authors.last_name LIKE '\%$search_string=
\%' OR authors.first_name LIKE '\%$search_string\%' OR pages.title LIKE '\%=
$search_string\%')};

my $where_content =3D qq{MATCH(content) AGAINST ('$search_string' IN BO=
OLEAN MODE)};
=20
my @wheres =3D ( $where_author_title );
push @wheres, $where_content if $volltext;

my $query =3D join 'UNION', map(qq{(SELECT pages.page_id, pages.title, =
authors.first_name, authors.last_name, pages.visible, pages.user_id FROM pa=
ges JOIN authors USING (author_id) WHERE $_ AND site_id =3D $sitemode ORDER=
BY pages.title)}, @wheres);

Later on, I test each row returned by the UNION statement if the $search_st=
ring is either in the title or in the author's name. If not, I print a divi=
ding line and a subheader once.

The result looks ok, something like:

Search results for 'Perl'

Title/author
=2E..
=2E..
=2E..

Content
=2E..
=2E..
=2E..

Now is there a better/more elegant way to do this? I am sure there is, and =
I would be grateful for a pointer.

Thanks,

Jan
--=20
The day Microsoft makes something that doesn't suck is the day they start s=
elling vacuum cleaners.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com