Simple PHP code for searching
From Ka-Map Wiki
this was contribted by Pēteris Brūns:
Here is simple serch code for ka-map!
<?
include_once( '../include/config.php' );
if (!extension_loaded('MapScript'))
{
dl( $szPHPMapScriptModule );
}
$results = array();
$oMap = ms_newMapObj($szMapFile);
//$map=$oMap; //jus for test
$aLayers = $oMap->getAllLayerNames(); //get all layers in map file
$tot = $oMap->numlayers; // total layers-count
//------------------------------------------------------------------------------
foreach($aLayers AS $layer) {
$oLayer = $oMap->getLayerByName($layer);
/* detect if group should be searchable In yor mapfile sercahable layer will be detected just like "queryable" "true" but her use "searchfield" "field_name"*/
$szSearchfield = $oLayer->getMetaData('searchfield');
if ($oLayer->getMetaData( "searchfield" ) != "") {
$szSearchfield = $szSearchfield;
//----------------------------------------------------
//postgis connect (NOT TESTED IF is possible to test it culd be nice to know works it or not)
if($oLayer->connectiontype == MS_POSTGIS){
$searchstring = $szSearchfield . ' ~* \ . $searchstring .'\' ';
} else { // Shapefile
$numclasses = $oLayer->numclasses;
for($i = 1 ; $i < $numclasses; $i++){
}
// Second HACK: it work
if($numclasses > 1){
$class = $oLayer->getClass(0);
// Match all
$class->setExpression('/.*/');
}
}
#----------------------------------------------------------------------------------------------
if(@$oLayer->queryByAttributes($szSearchfield, $searchstring, MS_MULTIPLE) == MS_SUCCESS ){ //MS_SUCCESS
$oLayer->open();
// Add element to results array for each result row
for ($j=0; $j < $oLayer->getNumResults(); $j++)
{
// get next shape row
$result = $oLayer->getResult($j);
$shape = $oLayer->getShape($result->tileindex, $result->shapeindex);
//var_dump($shape);
// push the row array onto the results array
$aTmp = $shape->values;
//Calculate centroid
$x_c = ($shape->bounds->minx + $shape->bounds->maxx) / 2; //get x average value
$y_c = ($shape->bounds->miny + $shape->bounds->maxy) / 2; //get y average value
//$aTmp = array_merge( $aTmp , array('x' => $x,'y' => $y, 'id' => $result->shapeindex));
$xmin = $shape->bounds->minx;
$xmax = $shape->bounds->maxx;
$ymin = $shape->bounds->miny;
$ymax = $shape->bounds->maxy;
$aTmp = array_merge( $aTmp , array('xmin' => $xmin,'xmax' => $xmin,'ymin' => $ymin,'ymax' => $ymax,'x_c'=>$x_c,'y_c'=>$y_c, 'id' => $result->shapeindex));
$results[$layername][] = $aTmp;
// end for loop
}
//echo implode
printer($results,$szSearchfield,$layer); //printing
}
else{
print('No results returned!
Searched string:' . "\n" . $searchstring);
}
$oLayer->close();
//----------------------------------------------------
$results=flush($results);
}
else{
$szSearchfield = "Not defined searchfield";
}
}
function search($szSearchfield,$searchstring){
//----------------------------------------------------
}
//-------------------------------------create print -----------------------------
function printer($results,$szSearchfield,$layer){
foreach($results as $key_val => $value) {
echo "
Returned results for layer:".$layer."
"; // top for each layer
foreach($value as $key_val => $v) {
$m = iconv("latin1", "UTF-8", $v[strtoupper($szSearchfield)]);
$field=$v[strtoupper($szSearchfield)];
//In $m place you can use $v[strtoupper($szSearchfield)] this is used for iconv etc..
print "<a href=# onClick=\"myKaMap.zoomToExtents(".$v["xmin"].",".$v["ymin"].",".$v["xmax"].",".$v["ymax"].")\" onMouseOver=\" toolTip.moveGeo(".$v["x_c"].",".$v["y_c"]."), toolTip.setText('".$m."')\" onMouseOut=\"toolTip.move()\">".$m."</a>
\n";
}
}
}
?>

