I have a simple problem and I do not want to write 24 if statements.

I am creating a URL string which requires precise formatting to work. This URL is getting created on the fly based on variables ($Layer1.','.$Layer2.','.$Layer3)

I pass in to create the URL string. The URL string requires a format like this:

http://test/map.php5?LAYERS=Layer1,Layer2,Layer3&QUERY_LAYERS=Layer1,Layer2,Layer3

The problem lies when I only pass lets say two of the variables. My URL now looks like:

http://test/map.php5?LAYERS=,Layer2,Layer3&QUERY_LAYERS=,Layer2,Layer3

The application accepting this URL is certainly not happing with a leading comma.

FYI - Variables declared as follows:

$Layer1="Layer1";
$Layer2="Layer2";
$Layer3="Layer3";

Any idea how I can get this to work without testing every possible combination?

Thanks in advance,

Mapper

you can do...

//declare which layers you want.
//if one layer is not declared then no problem cause it will not be added.
$Layer1="Layer1".",";
$Layer2="Layer2".",";
$Layer3="Layer3".",";

//compile to one variable and get rid of ending comma.
$query = substr($Layer1.$Layer2.$Layer3,0,-1);

does that work for you?

Thanks...

Lets see....This will work for the last case, that is, there is a layer1 and layer2 defined, but no layer3:

$query resolves to "Layer1,Layer2"

However, if you define layer2, and layer3:

$query resolves to",layer2,layer" (should be"layer2,layer3")

or if just layer3 is defined:

$query resolves to",,layer" (should be"layer3")

Cheers,

Mapper

$query resolves to

Member Avatar for diafol

This all sounds very odd Mapper. How are you defining your $layer variables? Jr's solution should work as far as I can see.

Thanks...

Lets see....This will work for the last case, that is, there is a layer1 and layer2 defined, but no layer3:

$query resolves to "Layer1,Layer2"

However, if you define layer2, and layer3:

$query resolves to",layer2,layer" (should be"layer2,layer3")

or if just layer3 is defined:

$query resolves to",,layer" (should be"layer3")

Cheers,

Mapper

$query resolves to

Your not really following what I stated. When you declare the value of a variable you're suppose to add the , (comma). So no matter what value is inside each layer each will have a trailing comma. The last function will remove any extra commas. Again, YOUR problem with MY code is when YOU DECLARE the variables. (PS. Not mad just want to make it clear. hahaha)

Note:

//See the comma?
$Layer1="Layer1".",";
$Layer2="Layer2".",";
$Layer3="Layer3".",";

I have a simple problem and I do not want to write 24 if statements.

I am creating a URL string which requires precise formatting to work. This URL is getting created on the fly based on variables ($Layer1.','.$Layer2.','.$Layer3)

I pass in to create the URL string. The URL string requires a format like this:

http://test/map.php5?LAYERS=Layer1,Layer2,Layer3&QUERY_LAYERS=Layer1,Layer2,Layer3

The problem lies when I only pass lets say two of the variables. My URL now looks like:

http://test/map.php5?LAYERS=,Layer2,Layer3&QUERY_LAYERS=,Layer2,Layer3

The application accepting this URL is certainly not happing with a leading comma.

FYI - Variables declared as follows:

$Layer1="Layer1";
$Layer2="Layer2";
$Layer3="Layer3";

Any idea how I can get this to work without testing every possible combination?

Thanks in advance,

Mapper

Solution: pass your variables as an array, then use implode(',' $arr);
the implode function will automatically join the array pieces into a string separated by ',' and will not add the leading comma
http://us3.php.net/implode


Another solution is do whatevery you are doing now, then use ltrim($url, ',');

This will remove the leading ',' from the string - ltrim means trim the left side of string, you pass the string itself and second argument is collection of chars you want to trim, in your case its a comma

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.