Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
#1 2009-10-13 09:23:58
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
limit search to 1 section
Hi,
I published my site and just now realiced that the search form is not limited to the section I specify.
I have different searchforms around the site, and what each one to search in a different section, but all display the results of every single section on the site.
Is there a way to limit a search form to one section?
Thanks
Offline
Re: limit search to 1 section
Some time ago jm has helped us develop a similar kind of search. See it on neme.org.
If that is what you are looking for I’ll be happy to post the code here.
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#3 2009-10-13 10:08:13
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
Re: limit search to 1 section
Is not exacly that, but I am sure it can be usefull :)
The way I wanted to use it was:
restaurants page: search only in restaurants section
bars page: search only in bars section
etc…
Offline
#4 2009-10-13 12:13:10
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,306
Re: limit search to 1 section
restaurants page: search only in restaurants section
bars page: search only in bars section
Maybe this FAQ is already sufficient?
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: limit search to 1 section
well what we have is multi site/multi section search and here it is for anyone to adapt/edit.
All kudos goes to: jm
On the top of the template before the doctype
<txp:php>
if ($_POST['submit']) {
$url = $_POST['site'].$_POST['terms'];
if ($_POST['section'] && ($_POST['section'] != 'null')) $url = $url.'&s='.$_POST['section'];
header('Location: '.$url);
} else {
header('content-type: text/html; charset=utf-8');
}
</txp:php>
In the <head>
<script type="text/javascript" src="<txp:site_url />js/jumpmenu.js"></script>
In the <body>
:
<form action="<txp:php> echo $_SERVER['PHP_SELF'];</txp:php>" id="search" method="post">
<fieldset>
<legend>Search</legend>
<label for="site">Site
<select id="site" name="site">
<txp:php>
// simple way to auto-generate <options>
$sites = array(
// site title => url/?q=
'Site1' => 'http://www.site1.tld/?q=',
'Site2' => 'http://www.site2.tld/?q=',
'Site3' => 'http://www.site3.tld/?=',
);
// asort($sites);
foreach ($sites as $title => $url) {
echo '<option value="'.$url.'">'.$title.'</option>';
};
</txp:php>
</select>
</label>
<label for="terms">Terms
<input id="terms" name="terms" type="text" value="<txp:page_url type="q" />"/>
</label>
<input name="submit" type="submit" value="search" id="searchbutton" />
</fieldset>
</form>
<script type="text/javascript" src="<txp:site_url />js/search.js">
The search.js:
var search = {
// configuration
config : {
formId : 'search', // id used for <form> element
sectionId : 'section', // id used for <select> element
sectionLabel : 'Section', // label text used for <select> element
allSectionsText : 'All sections' // <option>all sections</option>
},
// preps the form elements for action
init : function() {
// does the form exist? if not, quit now
if (!document.getElementById(search.config.formId)) return;
var searchForm = document.getElementById(search.config.formId);
// loop through all the form elements
for (i=0; i<searchForm.elements.length; i++) {
var el = searchForm.elements[i];
// if the elements are <selects>, continue
if (el.nodeName == 'SELECT') {
search.ifSection(el);
search.addEvent(el, 'change', function() { search.ifSection(this); });
}
}
},
// checks the values of the sites and if applicable, creates a section input
ifSection : function(el) {
switch (el.value) {
case 'http://www.site1.tld/?q=':
search.createSection(section=[null, 'Section_1', 'Section_2', 'Section_3', 'Section_4']);
break;
case 'http://www.site2.tld/?q=':
search.createSection(section=[null, 'Section_1_site_2', 'Section_2_site_2', 'Section_3_site_3']);
break;
default:
search.removeSection();
};
},
// create's a section select and options
createSection : function(section) {
// if any section selects exist, remove them
search.removeSection();
// create label element
var label = document.createElement('label');
label.setAttribute('for', search.config.sectionId);
// create and attach label text
label.appendChild(document.createTextNode(search.config.sectionLabel));
// create select element
var select = document.createElement('select');
select.setAttribute('id', search.config.sectionId);
select.setAttribute('name', 'section');
// insert <select> into <label>
label.appendChild(select);
// build <option>s
for (i=0; i<section.length; i++) {
// create <option>
var option = document.createElement('option');
option.setAttribute('value', section[i]);
// create <option>text</option>
var optionText = document.createTextNode((section[i] === null) ? search.config.allSectionsText : section[i]);
option.appendChild(optionText);
// insert <option> into <select>
select.appendChild(option);
};
// insert <select> after first label
search.insertAfter(label, document.getElementById(search.config.formId).getElementsByTagName('label')[0]);
},
// helper function that makes up for the DOM's lack of insertAfter
insertAfter : function(newElement, targetElement) {
var parent = targetElement.parentNode;
if (parent.lastChild == targetElement) {
parent.appendChild(newElement);
} else {
parent.insertBefore(newElement, targetElement.nextSibling);
}
},
// removes any sections
removeSection : function() {
if (document.getElementById(search.config.sectionId)) {
var toRemove = document.getElementById(search.config.sectionId);
/*
The following is a little complex, but it saves a for loop. Key:
JS: toRemove---->.parentNode---->.parentNode
HTML: <select>----><label>--------><fieldset>
*/
toRemove.parentNode.parentNode.removeChild(toRemove.parentNode);
}
},
// cross-browser way to add events to objects
addEvent: function(obj, event, fn) {
// for the smart kids
if (obj.attachEvent) {
obj["e"+event+fn] = fn;
obj[event+fn] = function() { obj["e"+event+fn]( window.event ); }
obj.attachEvent( "on"+event, obj[event+fn] );
} else {
obj.addEventListener(event, fn, false);
}
}
};
// load search magic!
search.addEvent(window, 'load', search.init);
the jumbmenu.js
function jumpMenu(targ,selObj,restore){
eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
if (restore) selObj.selectedIndex=0;
}
Edit: Or you can use Uli’s suggestion:)
Last edited by colak (2009-10-13 12:27:57)
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#6 2009-10-13 14:19:19
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
Re: limit search to 1 section
Uli,
That would work if the section where the search is is the same one as where the articles are, but it isnt. :(
Thanks for the FAQ link… there is no way around that right?
Thanks Colak for the Code! I would have to study it deeply to understand what this is doing before I use it.
Thanks
Offline
#7 2009-10-13 14:40:21
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,306
Re: limit search to 1 section
mlarino wrote:
That would work if the section where the search is is the same one as where the articles are, but it isnt. :(
Well, this here
restaurants page: search only in restaurants section
bars page: search only in bars section
sounded different.
So, how exactly do you plan the search?
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
#8 2009-10-13 14:46:06
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
Re: limit search to 1 section
Well…
Yes, I guess I didnt explain it right…. sorry,
There is a section called Restaurants.
In that section Restaurants are shown from all cities.
There is another section that displays only NY-Restaurants (these restaurants are published in the Restaurants Sections, but the section NY-Restaurants filters them to only show the ones for that city)
There is where I have the search form, that is why your solution doesnt work there, because the articles are published in “Restaurants” not in “NY-Restaurants” that is the current section where the search form is.
Offline
#9 2009-10-13 15:22:13
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
Re: limit search to 1 section
The wierd thing is that when I make a search with a blank form, it will display all the content, but when I search for a word it doesnt display anything.
I guess its because of that, that the articles are publish in a section but the search form is in another diferent section.
Offline
Re: limit search to 1 section
mlarino wrote:
There is another section that displays only NY-Restaurants (these restaurants are published in the Restaurants Sections, but the section NY-Restaurants filters them to only show the ones for that city)
There is where I have the search form, that is why your solution doesnt work there, because the articles are published in “Restaurants” not in “NY-Restaurants” that is the current section where the search form is.
Coming back to it. Wouldn’t it be simpler if you had a section called Restaurants
and categories for NY-R/rants, LA-R/rants, etc. In which case you could make some clever use of article_custom tags
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
#11 2009-10-13 16:08:54
- mlarino
- Member
- Registered: 2007-06-29
- Posts: 367
Re: limit search to 1 section
mmmmm
I actually do have categories for that….
Can search be limited to a Category?
Offline
Re: limit search to 1 section
Not really but you can have the visitors checking all listings for the particular listing
maybe something like
<txp:article_custom section="restaurants" category='<txp:category />' limit="999" />
Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.
Offline
Pages: 1