Selection from a mysql/ogr layer in php mapscript

A little phpsnippet for filtering out items from a layer. I have a layer “mobil” which may be on or off. This layer consists of a number of dataset collected at various times. There is one field ‘visible’ that is used to indicate if the dataset is of general interest (eg testing and calibration datasets are set with visible to false) The datasets do also belong to a project, indicated with a projectid in the table. There are also going to be some more items to filter on. Since I don’t know a priori how many items there will be filtered on, I am putting the filter expressions in an array and combining them with ‘and’ before I set the filter on the layer.

if($_GET['mobil']=='ON'){ // no reason to do this if the layer is not visible
	if(!($_GET['invisible']=='ON')){$filters[]="visible = 1";}
	if($_GET['project']*1){$filters[]="projectid = ".$_GET['project']*1;}
	if(count($filters)){ // if there are any items to filter on
		$qLayer = $map->getlayerbyname('mobil');
		$filter="where ".implode(" and ",$filters);
		$qLayer->setfilter($filter);
	}
}

This works for sql-based layers and maybe some others, for some types of layers, one needs to set filter and filteritem.

This entry was posted in mapserver. Bookmark the permalink.