Jump to content


[Snippet] Search for documents by keyword - a bit like tagging


4 replies to this topic

#1 PaulD

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 389 posts
  • Gender:Male

Posted 29 January 2009 - 01:46 PM

File Name: Search for documents by keyword - a bit like tagging
File Submitter: PaulD
File Submitted: 29 Jan 2009
File Updated: 29 Jan 2009
File Category: Miscellaneous

This snippet is a piece of code that you may find handy.

I had a folder with lots of documents, each representing an album. I used the keywords on each page to identify the musicians on each album. I wanted then to be able to gather all the documents by a specific musician (keyword).

So this is what I came up with. It works really well from a list of all the musicians. Each one has a link on it to the results page that this snippet is on like where the links are something like this href="index.php?id=56&keyword=look for this".

The snippet then takes the keyword sent to it. Checks each document in turn, checking if the wanted keyword matches a keyword associated with each document in turn.

The snippet here then outputs the id, pagetitle, description and longtitle to a simple paragraph. On my site that I am overhauling I have formatted this output to produce a nice ordered list, but you can format the info any way you like of course.

I hope this helps someone.

Paul.

// This bit of code gets passed a keyword. It then searches the children of a set folder for pages set with that keyword through the manager
// It then returns the pagetitle, longtitle, description of the pages that match to be used in a list of results.
// The output goes to $albumlist but that is because I wrote this to search albums for musician names.

// sets the id of the folder to search
$folder=91;

// get keyword from link
// ie if this snippet is on a page id=21 then the link to it would be
// <a href="index.php?id=21?keyword=search phrase">Link text</a>
// notice that the keyword to search for in this example is 'search phrase'
$searchkeyword = (isset($_REQUEST['keyword']))? $_REQUEST['keyword'] : "notKnown";

// you can do an if loop here for the case of keyword not set. I left it out to just return a blank result if keyword not set or set but not found.

// set array for children 
$children=array();

// get children data 
$children=$etomite->getAllChildren($id=$folder, $sort='menuindex', $dir='ASC', $fields='id, pagetitle, longtitle, description', $limit="100"); 

// how many children are there
$numChildren=count($children);

// loop through children one at a time
for ($i = 0; $i <= $numChildren-1; $i++) {

		// 'apageid' is the page id of the page we are looking at
		$apageid = $children[$i]['id'];

		// Get the keywords for the page we are looking at
		$keywords = $etomite->getKeywords($id=$apageid);

		// Count the number of keywords for the page we are looking at
		$keywordcount=count($keywords);

		// loop through all the keywords to match what we are looking for
		for ($x = 0; $x <= $keywordcount-1; $x++) {

				// get the keyword we are looking at
				$keyword = $keywords[$x];

				// does the keyword match
				if ($keyword == $searchkeyword){

						// output the matching details
						// this can be formatted any way you want. Here I have just output in paragraphs.
						$albumlist .= "<p>".$children[$i]['id']."<br />";
						$albumlist .= $children[$i]['longtitle']."<br />";
						$albumlist .= $children[$i]['pagetitle']."<br />";
						$albumlist .= $children[$i]['description']."<br />";
						$albumlist .= '</p>';

				} // closing the 'if keyword matches' loop

		} // closing the loop through keywords


} // closing the loop through children/albums

$output .= $albumlist;

return $output;


Click here to download this file

#2 Rickyx

    Etomite Forum Newbie

  • Member
  • 21 posts

Posted 05 May 2011 - 12:55 PM

Hi,

this is really what i'm looking for!

...but I can't get it working. Can you post an example of usage?
How to setup the research and the result page?

Thank you,
Rickyx

#3 PaulD

    Likes Etomite Forums!

  • Developers
  • PipPip
  • 389 posts
  • Gender:Male

Posted 08 May 2011 - 02:07 AM

Hi,

It was a while ago I did this. From memory, you set up a new results page in etomite, create a snippet with the above code and add a call to that snippet in your new results page.

The key is to link to the page from a search box or from fixed links with the search phrase in the link as described within the snippet.

I hope that helps in some way, it worked well on the site I was making at the time.

Paul.

#4 Guest_Jane0018_*

  • Guests

Posted 12 July 2011 - 03:52 AM

yep! absolutely agree...

View PostRickyx, on 05 May 2011 - 12:55 PM, said:

Hi,

this is really what i'm looking for!

...but I can't get it working. Can you post an example of usage?
How to setup the research and the result page?

Thank you,
Rickyx


#5 Rickyx

    Etomite Forum Newbie

  • Member
  • 21 posts

Posted 08 February 2012 - 03:36 PM

Hi, it is me again...

it would be really handy if you have some more suggestion to make it work.
Without documentation I'm not able to use it.

Thank you again,
Rickyx

ps. I think I'll try to write it to make it "static", just like ListDocuments but with keywords... what do you think?

Edited by Rickyx, 08 February 2012 - 04:05 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users