MS4W Version 5.x Query From .MAP File Sample

original source:

<html><head><title>query sample</title></head><body><PRE>

// sample filter strings to search on... 'name' is field in Attribute Table for Layer
$filter_string = '("[name]" == "Canada")';              //exact (use ==) - case-sensitive
//$filter_string = '("[name]" ~* "^ca.*")';                     //starts Ga (^ for starting) not case-sensitive
//$filter_string = '("[name]" ~* ".*CA.*")';            //like Ga (.* maybe not needed?!?) not case-sensitive

$map = new mapObj("/ms4w/apps/local-demo/");

$layer = $map->getLayer(1);                                     //second layer in mapfile
$layer->setFilter($filter_string);                              //filter results
$layer->template = true;                                        //for historical reasons, the query attribute must be non-NULL for a layer to be queryable RE:

$ext = $layer->getExtent();
$query = $layer->queryByRect($map, $ext);                       //must query before getting $layer numitems
$numResults = $layer->getNumResults();

$layername = $layer->name;
$layeritems = $layer->numitems;                                 //count of items in attribute table
//echo 'Layer '.$layername.' has '.$layeritems.' items<br>';
for ($x=0;$x<$layeritems;$x++) {
        $layer_headers[$x] = $layer->getItem($x);               //for the layer, this gets the attribute field names

if($query == MS_SUCCESS) {
        echo 'Query has '.$layer->getNumResults().' Results:<br>';
        if($layer->getNumResults()>0) {
                for ($i=0; $i < $layer->getNumResults(); $i++) {
                        $result = $layer->getResult($i); //print_r($result);
                        if($result === FALSE) {
                                error_log('No result '.$i);
                        } else {
                                $shape = $layer->getShape($result); //print_r($shape);
                                //echo ("#$i  OBJECTID = ".$shape->values['OBJECTID'].'<br>');  //ms4w 3.x
                                //echo 'vals='.$shape->numvalues;
                                echo ("#$i  OBJECTID = ".$shape->getValue(0));                  //ms4w 4.x
                                $vals = array();
                                echo '<blockquote>';
                                for ($x=0;$x<$shape->numvalues;$x++) {
                                        //echo $x.'='.$shape->getValue($x).'<br>';
                                        $vals[$layer_headers[$x]] = $shape->getValue($x);
                                //echo 'WKT='.$shape->toWKT().'<br>';
                                echo '</blockquote>';
} else {
        echo('Query Returned No Results');
echo '<hr>END<hr>'; ?>
Last modified 2 years ago Last modified on 2021-12-21T17:35:09-04:00
Note: See TracWiki for help on using the wiki.