[Mapserver-users] queryByAttributes on TWO OR MORE fields

Armin Burger armin.burger at gmx.net
Thu Jul 8 18:04:28 EDT 2004


Daniele,

you could achieve this via

  $queryString = "('[street]' = 'Main Street' AND [civic] = 15)";
  $fldName = "street";
  @$Layer->queryByAttributes($fldName, $queryString, MS_SINGLE);

note:
- $fldName can be any valid field name in the shapefile,
  (even if it's not used in the query),
  but I get the impression that this is not always the case,
  so you better use one of the search fields anyway
- field names in the search string must be in brackets []
- the whole query expression should be put in parentheses ()
- I think that once the field names had to be in uppercase for
  shapefiles, but this doesn't seem to be necessary any more
- for fields of type character, the field name should be put
  in single quotes


If you have free entering of search values you might want to search the shapefile case in-sensitive. You could do that that via regular expressions, like

  $queryString = "('[street]' =~ /(M|m)(A|a)(I|i)(N|n) (S|s)(T|t)(R|r)(E|e)(E|e)(T|t)/  AND [civic] = 15 )";

or just

  $queryString = "('[street]' =~ /(M|m)(A|a)(I|i)(N|n) (S|s)(T|t)/  AND [civic] = 15 )";

which would match 'Main Street' if the user entered e.g. the string 'main str' or 'Main street' or 'MAIN ST'

you can convert the entered value to the necessary regular expression e.g. like

  $search_expr = "main str";
  $search_expr_caseinsensitive = preg_replace ("/[a-z]/ie", "'('. strtoupper($0) . '|' . strtolower($0) .')'", $search_expr);

which produces:  (M|m)(A|a)(I|i)(N|n) (S|s)(T|t)(R|r)


Using regex you have also the advantage to decide if you want to search for a full expression or just part of it. So using the above regex solution the search of a user for a street entering 'garib' would find 'v. G. Garibaldi', 'Piazza Garibaldi' and 'Viale Garibaldi' and make search more flexible for the user.

I hope this helps and is not too confusing

Armin



> I have a shapefile and I want to query two fields:
>  - 'street'  (the name of the street)
>  - 'civic' (the civic number of the address)

> For example, in SQL I'd have: SELECT * FROM table WHERE street='Main
> Street' AND civic=15;

> How can I do it with PHP/MapScript?

> I tried:
>     ...
>     $StreetName = 'Main Street';
>     $CivicNumber = 15;
>     if
> ((@$Layer->queryByAttributes('street'&'civic',$StreetName.$CivicNumber,M
> S_SINGLE) {
>     ...
> but this obviously returns an error.

> Can you help me?

> Thank you,
>     Daniele









More information about the mapserver-users mailing list