Etomite Community Forums: [Snippet] Search for documents by keyword - a bit like tagging - Etomite Community Forums

Jump to content

Read Me

This forum is posted in automatically when a snippet is added to the library, or updated. Please use the snippet topic to discuss that snippet.

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

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

#1 User is offline   PaulD 

  • Likes Etomite Forums!
  • PipPip
  • Group: Member
  • Posts: 298
  • Joined: 06-February 06
  • Gender:Male
  • Location:Lutterworth, Leicestershire

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

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users