Gossamer Forum
Home : Products : Links 2.0 : Customization :

Extra search option

Quote Reply
Extra search option
Hi there,
I have added some extra fields to my search engine like "language" and "country". It all works great but now I want to provide the user the option to specify the search with a choosable language. You could compare it with the option called "number of results" in the "more search options" section. I already searched your forum for this topic but I did not find a clear answer. My question to you is: can I add this extra search field with the 3 Shestopalov mods or do I have to make some other mods and how do they have to look like?
Sincerely, Nedlad Run

Quote Reply
Re: Extra search option In reply to
Links 2.0 by default can search any field in your LINKS database.

What you need to do is the following:

1) Add the number of your Language field in the @search_fields array in the links.def file.

2) Then simply add the following codes in your search.cgi:

Code:

if ($in{'language'} && $in{'language'} ne "All") {
($values[$db_language] =~ /^$in{'language'}/) or next LINE;
}


Make sure that you have something like $db_language in the IMPORTANT FIELD SECTION and that it is configured for your "language" field.

3) Then add the following codes in your search.html and search_error.html template files:

Code:

<select name="language">
<option value="All">All Languages</option>
<option value="English">English</option>
<option value="French">French</option>
</select>


Change "French" and "English" with the exact values you have in the Û_select_fields hash in the links.def file.

That should do it.

Note: These are codes that Widgetz wrote for the Category search, which BTW has been adapted for searching States...and that Thread has recently been brought up towards the top of the forum.

*hint* *hint* Wink

"Thinking out of the box (codes) is fun and effective"

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi there,
I followed your advice, but my extra language search field seems to work only for only one option (option All).
When I select an other language, I receive the following error message:

Error Message : fatal error: Label not found for "next LINE" at search.cgi line 70.
Script Location : search.cgi

I will show you now all the changes that I made in the several templates and cgi's and I hope that you can tell me what have I done wrong

Changes:
"1) Add the number of your Language field in the @search_fields array in the links.def file."


I did this:
# Field names you want to allow visitors to search on:
@search_fields = (1,2,5,14);

where language is nr. 14 in: # Definition of my database file.


"2) Then add the following codes in your search.cgi:
if ($in{'language'} && $in{'language'} ne "All") { ($values[$db_language] =~ /^$in{'language'}/) or next LINE;}"


I did this:
# Choose a Language.
if ($in{'Language'} && $in{'Language'} ne "All") { ($values[$db_Language] =~ /^$in{'Language'}/) or next LINE;}



"Make sure that you have something like $db_language in the IMPORTANT FIELD SECTION and that it is configured for your "language" field."


I did this:
# where the first field equals 0.
$db_category = 4; $db_modified = 3; $db_url = 2;
$db_hits = 8; $db_isnew = 9; $db_ispop = 10;
$db_contact_name = 6; $db_contact_email = 7; $db_title = 1;
$db_votes = 12; $db_rating = 11; $db_mail = 13; $db_Language = 14;


"3) Then add the following codes in your search.html and search_error.html template files:
<select name="language"><option value="All">All Languages</option><option value="English">English</option><option value="French">French</option></select>
Change "French" and "English" with the exact values you have in the Û_select_fields hash in the links.def file.
That should do it."


I did this:
In search.html:
<form action="<Û_cgi_url%>/search.cgi" method="GET">
<div class="margin">
<table border="0" cellpadding="0" cellspacing="0">
<tr><td>Search: <input type="TEXT" name="query" size="30"> <input type="Submit" value="Search"></td></tr>
<tr><td>Number of Results: <SELECT name="mh"><OPTION>10<OPTION SELECTED>25<OPTION>50<OPTION>100</SELECT></td></tr>
<tr><td>Language: <select name="language"><option value="All">All Languages</option>
<OPTION VALUE="All">All States</option>
<OPTION VALUE="Chinese">Chinese</option>
<option value="Danish">Danish</option>
<option value="Dutch">Dutch</option>
<option value="English">English</option>
<option value="French">French</option>
<option value="German">German</option>
<option value="Greek">Greek</option>
<option value="Italian">Italian</option>
<option value="Japanese">Japanese</option>
<option value="Norwegian">Norwegian</option>
<option value="Other">Other</option>
<option value="Portugese">Portugese</option>
<option value="Spanish">Spanish</option>
<option value="Swedish">Swedish</option>
</select></td></tr>
<tr><td>As Keywords: <input type="RADIO" name="type" value="keyword" CHECKED> As Phrase: <input type="RADIO" name="type" value="phrase"></td></tr>
<tr><td>AND connector: <input type="RADIO" name="bool" value="and" CHECKED> OR connector: <input type="RADIO" name="bool" value="or"></td></tr>
</table>
</div>
</form>


In searh_error.html:
No changes! I did not see any such changes in the State example you told me about, that's why I made no changes. If I do have to make some changes please let me know.


In links.def:
Û_select_fields = (
isNew => 'Yes,No',
isPopular => 'Yes,No',
ReceiveMail => 'Yes,No',
Language => 'All,American,Chinese,Danish,Dutch,English,French,German,Greek,Italian,Japanese,Norwegian,Other,Portuguese,Spanish,Swedish'
);


I DID chmod search.cgi to 755!
Please tell me what I did wrong, because I really don't see the wrong part!
Sincerely, Nedlas Run!

Quote Reply
Re: Extra search option In reply to
As stated in the States thread...you need to match the cases of the select name and the codes in the search.cgi file.

Look at the following codes you've added:

Code:

<select name="language">


AND

Code:

# Choose a Language.
if ($in{'Language'} && $in{'Language'} ne "All") {
($values[$db_Language] =~ /^$in{'Language'}/) or next LINE;
}


Do you see the problem now??? If not...I have bolded the problem codes!

Regards,



Eliot Lee
Quote Reply
Re: Extra search option In reply to
I understand what you mean. I changed all the Language-parameters to a capital.
It looks like this:
select name="Language">

AND

#Choose a Language.if ($in{'Language'} && $in{'Language'} ne "All") { ($values[$db_Language] =~ /^$in{'Language'}/) or next LINE;}

But I keep the error message.
How can I fix it?
Sincerely Nedlas




Quote Reply
Re: Extra search option In reply to
The problem is that you are NOT copying the codes as they have been written and now you have a syntax error in your search.cgi script.

Copy the codes as they have been posted.

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi,
I finally got it working. Thanks a lot!!!!!!!!!!!!

Quote Reply
Re: Extra search option In reply to
Good....

And you're welcome.

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi again,
My language-search option is working great, but I discovered that only categories show up when I search for "All languages". I do not have this problem with the other options.
How come?
Sincerely Nedlas

Quote Reply
Re: Extra search option In reply to
Probably...because you put the codes in the wrong place in the search.cgi file -OR- you inadvertantly deleted some codes like $link_results in the search.cgi file.

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi again,
I still cannot select all languages succesful. I did not delete anything so it may be the other thing you told about.
I will give you a part of my search.cgi, because I think it's not very usefull to have no option to show all languages.
I have also tried to make it possible to select several languages by including the multiple tag in the html code, but this also doesn't work: when I select several languages I only see a number of found categories but not a number of found links and the info about those links. The dropdownlist works only for one language. How can I change this?
I really hope you can help me with the all-language problem.


sub main {
# --------------------------------------------------------
%in = &parse_form();

# Display the form if called with no input.
(keys %in <= 0) and &site_html_search_form() and return;

# Choose a Language.
if ($in{'language'} && $in{'language'} ne "all") { ($values[$db_language] =~ /^$in{'language'}/) or next LINE;}


# Set maximum hits -- default to 25.
local $maxhits = 25;
if ($in{'mh'} && (($in{'mh'} == 10) || ($in{'mh'} == 25) || ($in{'mh'} == 50) || ($in{'mh'} = 100))) {
$maxhits = $in{'mh'};
}


# Set search type -- either phrase or keyword. Also build keyword list to search on.
my @search_terms = ();
($in{'type'} eq 'phrase') ?
(@search_terms = ($in{'query'})) :
(@search_terms = split (/\s/, $in{'query'}));

# Set boolean connector and next hits page.
my $bool = $in{'bool'} || 'and';
my $nh = $in{'nh'} || 1;

# Store the search results here.
local (%link_results, @category_results);

# Do the actual search.
my $status = &search (\@search_terms, $bool);
if ($status ne "ok") { &site_html_search_failure ($status); return; }

# Return unless we have results.
((keys %link_results > 0) or ($#category_results >= 0)) or
&site_html_search_failure ("no matching records") and return;

# The HTML used in the output is stored here.
local ($cat_hits, $link_hits, $category_results, $link_results, $next);

# Build the HTML for the category results and store it in "$category_results". Only build the html
# if we are on the first set of link results.
foreach $category (@category_results) {
if ($nh == 1) {
$cat_clean = &build_clean($category);
$linked_title = &build_linked_title ($category);
$category_results .= qq|<li>$linked_title\n|;
}
$cat_hits++;
}
$cat_hits ||= 0;
$lowrange = ($nh-1) * $maxhits + 1;
$highrange = $nh * $maxhits;

# Go through each category of links returned, and build the HTML. Store in hash %link_output.
SETOFLINKS: foreach $setoflinks (sort keys %link_results) {
my $hits = ($#{$link_results{$setoflinks}} + 1) / ($#db_cols+1);
LINK: for ($i = 0; $i < $hits; $i++) {
$link_hits++;
if (($link_hits <= $highrange) && ($link_hits >= $lowrange)) {
%tmp = &array_to_hash ($i, @{$link_results{$setoflinks}});
$link_output{$setoflinks} .= &site_html_link (%tmp) . "\n";
}
}
}

# Go through the hash just built, and build the complete link output. Store in $link_results.
foreach $setoflinks (sort keys %link_output) {
$cat_clean = &build_clean ($setoflinks);
$title_linked = &build_linked_title ($setoflinks);
$link_results .= qq|<P>$title_linked\n|;
$link_results .= $link_output{$setoflinks};
}
# If we want to bold the search terms...
if ($search_bold) {
foreach $term (@search_terms) {
# This reg expression will do the trick, and doesn't bold things inside <> tags such as
# URL's.
$link_results =~ s,(<[^>]+>)|(\Q$term\E),defined($1) ? $1 : "<STRONG>$2</STRONG>",gie;
$category_results =~ s,(<[^>]+>)|(\Q$term\E),defined($1) ? $1 : "<STRONG>$2</STRONG>",gie;
}
}

# If we have to many hits, let's build the next toolbar, and return only the hits we want.
my ($next_hit, $prev_hit, $next_url, $left, $right, $lower, $upper, $i);

if ($link_hits > $maxhits) {

# Remove the nh= from the query string.
$next_url = $ENV{'QUERY_STRING'};
$next_url =~ s/\&nh=\d+//;
$next_hit = $nh + 1; $prev_hit = $nh - 1;

# Build the next hits toolbar. It seems really complicated as we have to do
# some number crunching to keep track of where we are on the toolbar, and so
# that the toolbar stays centred.

# First, set how many pages we have on the left and the right.
$left = $nh; $right = int($numhits/$maxhits) - $nh;
# Then work out what page number we can go above and below.
($left > 7) ? ($lower = $left - 7) : ($lower = 1);
($right > 7) ? ($upper = $nh + 7) : ($upper = int($link_hits/$maxhits) + 1);
# Finally, adjust those page numbers if we are near an endpoint.
(7 - $nh >= 0) and ($upper = $upper + (8 - $nh));
($nh > ($link_hits/$maxhits - 7)) and ($lower = $lower - ($nh - int($link_hits/$maxhits - 7) - 1));
$next = "";

# Then let's go through the pages and build the HTML.
($nh > 1) and ($next .= qq~<a href="$build_search_url?$next_url&nh=$prev_hit">[<<]</a> ~);
for ($i = 1; $i <= int($link_hits/$maxhits) + 1; $i++) {
if ($i < $lower) { $next .= " ... "; $i = ($lower-1); next; }
if ($i > $upper) { $next .= " ... "; last; }
($i == $nh) ?
($next .= qq~$i ~) :
($next .= qq~<a href="$build_search_url?$next_url&nh=$i">$i</a> ~);
(($i * $maxhits) >= $link_hits) and last; # Special case if we hit exact.
}
$next .= qq~<a href="$build_search_url?$next_url&nh=$next_hit">[>>]</a> ~ unless ($nh == $i);
}
# Print out the HTML results.
&site_html_search_results;
}




Quote Reply
Re: Extra search option In reply to
*sigh*

You have put the Language codes in the WRONG sub. They need to go in the sub search routine AFTER the following codes:

Code:

$grand_total++;


Look at Widgetz's Category Search Mod...the codes for the Languages need to go in the same spot in the sub search routine.

Good luck!

Regards,


Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi,
First of all thanks for your patience with me.
I changed the location of this code:
#Choose a Language.
if ($in{'Language'} && $in{'Language'} ne "all") { ($values[$db_Language] =~ /^$in{'Language'}/) or next LINE;}
and cut&pasted it right After this code:
$grand_total++;
in search.cgi
I don't know why but it doesn't work (at least it does not work for the "all" option, the rest works ok).
I tried to find the mod you told me about.
Is it this one: (because there are so many category mod- comments from widgetz).
http://www.gossamer-threads.com/perl/forum/showthreaded.pl?Cat=&Board=L2Cust&Number=43493&Search=true&Forum=All_Forums&Words=category search mod&Match=Entire Phrase&Searchpage=0&Limit=25&Old=allposts
I am wondering why my mod is still not working, is it because I am NOT using the categorie search mod or is that irrelevant?
Sincerely, Nedlas



Quote Reply
Re: Extra search option In reply to
Code:

I am wondering why my mod is still not working, is it because I am NOT using the categorie search mod or is that irrelevant?


That is irrelevant...And I do not know why the code hack I provided does not work...unless you are misspelling the values of States in the select field options in your search.html template file. Remember, they have to match EXACTLY as you have in the links.def file.

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi,
I visited http://www.joboffers.com and there a language variant (states in this case) WAS working! Strange, I really don't see my mistake. Maybe because my script is also generating the results for the number of categories that are found (for which I DO get the results) and the script of that person is NOT. I tried to contact him for help but he gave no reaction (alas).
Anyway, I thought about the problem and I came to the conclusion that using radiobuttons might solve my problem.
My search engine DID find all languages when there was no language option. (in other words: all relevant results showed up when I did a normal search) and I found the sites written in a specific language when I did the advanced search. The only problem (I already told you about) is that I can NOT find all languages in the advanced search.
I thought about this and my conlusion was that the language-search-specification-part had to be functioning in the right way when I was able to avoid that there is a language selection other then a specific language. I mean, I had to avoid tags like,
<OPTION VALUE="">All Languages or
<OPTION VALUE="all">All Languages
to be sure the all part would work.
I can do this by using radiobuttons where there is no button preselected.
For example if I added two radiobuttons with language specification for English and Dutch AND a reset button, I could make the user choose between:
-all languages (this means NO radiobutton selected) just like the normal serach
-dutch (the radiobutton with name and value dutch)
- or english (the radiobutton with name and value english).
By using reset the user could go back from one language to all languages.
In this way I would be able to avoid my existing problem.
I gave it a try but after I had build my pages again and clicked the advanced search link an enormous html-mess showed up. Allmost all links where gone (the a href code had turned into href and was shown up on the screen. The a had dissapeared!).
Later I realised that I needed another code to make the language-part working for radiobuttons instead of a drop-down menu. My question to you is if I do need another code to make the script work for radiobuttons and what is does look like?.
Sincerely Nedlas

Quote Reply
Re: Extra search option In reply to
In Reply To:
I gave it a try but after I had build my pages again and clicked the advanced search link an enormous html-mess showed up.
Uh...you do not need to re-build your index to see changes in dynamic files (like all the .cgi files, e.g., search.cgi).

In Reply To:
Later I realised that I needed another code to make the language-part working for radiobuttons instead of a drop-down menu. My question to you is if I do need another code to make the script work for radiobuttons and what is does look like?.
Uh...no you don't need another Mod...all you simply do is add the following codes:

Code:

<input type="radio" name="Language" value="All" SELECTED>All Languages
<input type="radio" name="Language" value="English">English
<input type="radio" name="Language" value="French">French


Regards,


Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi,
I cut&pasted your code into my seach.html and gave it a try.
The results were the same as I already told you. Everything works except the "all-function". I deleted this option and have added a reset button. Although you told me that in this case it is not necessary to rebuild the pages in the admin-menu I gave it a try because things messed up last time. I don't know why it was not functioning last time, but this time it worked very well. Finally I got the language-option working! (although in a bit different way but that does not matter)
Thanks for all your help and patience!
Sincerely, Nedlas

Quote Reply
Re: Extra search option In reply to
In Reply To:
Although you told me that in this case it is not necessary to rebuild the pages in the admin-menu I gave it a try because things messed up last time.
It shouldn't because all you need to do is access the search.cgi to see the changes you've made in the search.html.

Well...anyway...whatever works.

You're welcome.

Regards,

Eliot Lee
Quote Reply
Re: Extra search option In reply to
Hi,
Recently I made a search with my engine by selecting "or" and selecting "english". I now got results of ALL the sites that had one of the entered seach terms or sites that were written in english (even those that did NOT contain the desired search terms). This is wrong. It's my intention to let people search for keywords "A" OR "B" of which both sites(or better said and of which ALL sites) are written in English language. What do I have to change about the scipt to get the desired search results?
Sincerely, Nedlas Run