[GIS] How to use a variable in Geoserver viewparams

geoserveropenlayers-2

I just managed to create a parametric view that is working OK when i define the parameter value as, for instance, viewparams:'value:31' However, when i try to pass a value that comes from a variable, for instance, viewparams:'value:variable', the wms layer just wont load.
The variable is defined as var variable= 31;
Any ideas on why this happens?
I'm using OpenLayers + Geoserver + PostGIS

EDIT 1 –

Just to explain a little bit better what I'm trying to achieve.
I have a web map application developed with Openlayers where i have a set of wms layers that are being created by Geoserver.
Despite the fact that i already have some functionalities implemented, such as feature info in pop ups, images on those pop ups, etc, i thing that it would be really important to allow users to query the layers, to allow the map to show only what they wanted to see. The query would be done through a php form, where the values selected by users would act as variables on the Parametric views created on Geoserver, and by doing so, the features showing up on the map would be only the ones selected by the user.
The parametric view i created for testing purposes works, if i "hardcode" the value for the parameter:

var rov_dives = new OpenLayers.Layer.WMS(
           "ROV Dives",
                   "http://host:8080/geoserver/wms",
                   {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:50'},
                   {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
                );

In this example the map only shows the features that comply with the condition stated on the SQL script that defines the parametric view, in this case, all features that have a number of measurements higher than 50.
If I could pass that 50 as a variable coming from the aforementioned PHP form, I could implement the solution I thought of… The definition of the variable and WMS layer I'm trying to use right now is:

var variable = 50;

var rov_dives = new OpenLayers.Layer.WMS(
           "ROV Dives",
                   "http://host:8080/geoserver/wms",
                   {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:variable'},
                   {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
                );

Despite all my efforts I'm yet to find a solution for this.

Hope this explanation helps to understand the problem!

Best Answer

That's because you're not allowed to substitute variables in JavaScript like this. I'm also assuming this it's part of a longer url string.

Try splitting that string around the variable you need to substitute

You can use the following if you're sure your variable is always going to be a number:

var variable = 50;

var rov_dives = new OpenLayers.Layer.WMS(
    "ROV Dives",
    "http://host:8080/geoserver/wms",
    {'layers': 'parametric_view', 'format':'image/png', 'transparent':'true', viewparams:'test_parameter:' + variable },
    {'opacity': 1, 'isBaseLayer': false, 'visibility': false}
);
Related Question