Generating Image Gallery based on search results

Currently on my item browse pages I have an image gallery that shows 5 randomly selected featured images. If I am viewing the items in a specific collection the gallery only displays images limited to that collection. I created a custom function to create this gallery, the relevant portion is posted below:

function display_random_featured_item_gallery()
{
$html = '<div id="featured-gallery" class="galleryview"><ul class="gallery">';
//this creates an array of the featured items limited by collection
if ($collection = get_collection_by_id($_GET['collection'])) {
	        $limiter .= $collection->id;
		$items = get_items(array('featured'=>true, 'collection'=>$limiter),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}
//This creates an array of featured items
	else{
		$items = custom_get_random_featured_items('5');
		set_items_for_loop($items); }
//The rest is just the image gallery info....

However, I can't seem to figure out how to limit the gallery based on tags or search strings. It would be cool if I searched for a specific topic, the image gallery would only show featured items with the topic.

I figured out how to limit to tags:

if ($tags = html_escape($_GET['tags'])) {
$items = get_items(array('tags'=>$tags),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}

But still wondering if I could limit on general search strings.

You should be able to pass a search string to get_items by including it under the key 'search' in the array.

Not quite sure what you mean, but I've tried: $items = get_items(array('featured'=>true, 'search'),100); and it doesn't seem to work.

Clearly it's Friday, I've figured out what you meant, so now I think I have it.

if ($search=html_escape($_GET['search'])) {
$items = get_items(array('search'=>$search),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}

Thanks!

One quick note, since you're not displaying those GET parameters on the page, there's no need to run them through html_escape, and doing so will probably mess up your results if the tags or search string contain characters like <.

I also had to change the a few ifs to elseifs, but now it works. The gallery limits by collection, tag, or search string. Thanks again.

if ($collection = get_collection_by_id($_GET['collection'])) {
	        $limiter .= $collection->id;
		$items = get_items(array('featured'=>true, 'collection'=>$limiter),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}
elseif ($tags = html_escape($_GET['tags'])) {
$items = get_items(array('tags'=>$tags),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}

elseif ($search=html_escape($_GET['search'])) {
$items = get_items(array('search'=>$search),100);
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}

	else{
		$items = custom_get_random_featured_items('5');
		set_items_for_loop($items); }
		while(loop_items()):

Another question on this topic. Using the code above, I noticed that if I ran a search and also selected a collection, the gallery would be populated with images limited to the collection, and not the search. I want to be able to see if more than one variable has been selected. So here's what I've come up so far:

if ($collection = get_collection_by_id($_GET['collection'])) {
	     	$limiter .= $collection->id;
		if ($search=$_GET['search']){
		$items = get_items(array('search'=>$search, 'collection'=>$limiter),100);
		}
		else{
		$items = get_items(array('featured'=>true, 'collection'=>$limiter),100);
		}
		shuffle($items);
		$subset = array_chunk($items,5);
		set_items_for_loop($subset[0]);
		}

Is there a better way to determine if both fields have been searched? It's not too problematic right now, but I could see how it might be an issue if I also wanted to check if tags were also included. The code could start to get unwieldy with lots of nested ifs.