开发者

Splitting value of drop down selection into two variables? (PHP/HTML)

So I've got a website with two HTML drop-down lists- one is a list of US states, the other is a list of the cities/towns in the selected state. As of right now, I have an HTML file for each state, and they all have the options listed as such:

<option value="44.729932, -72.381758">Albany</option>
<option value="44.976728, -73.30257">Alburg</option>

The values are the coordinates (latitude, longitude) of each town. When I hit "search", the form data is sent to a PHP script for SimpleGeo (a location database company- amazing, btw)- the purpose of selecting the town is to provide values for $lat and $lon in this part of the PHP script, which is sent to SimpleGeo for every query:

$q = $_GET['q'];
$lat = 44.729932;
$lon = -72.381758;

$args = array('q' => $q, 'num' => 25, 'category' => Restaurant, 'radius' => 25);
$results = $client->getPlaces($lat, $lon, $args);

The part that can't change is the "getPlaces($lat, $lon, $args);" part because for some reason when I replace $lat and $lon with $coordinates and then replace their values with:

$coordinates = $_GET['city']; ('city' is the name and id of my city/town lists)

It doesn't see it as a valid query. Meanwhile, if you replace "$lat, $lon" with numbers as the coordinates, it understands perfectly.

... I just need to split up my values, either in the HTML files themselves, or in the PHP script by recognizing that ", " means the latitude value has ended and the longitude value has started. I don't know how it should be done, I just know that $lat and $lon have to equal the values set by my HTML files- one file for every state, about 500 options on average per file... How do I make it recognize something like "44.729932, -72.381758" as $lat and $lon?! Or alternatively, how do I make my drop down list pass on the values for lat and lon if I make the开发者_Go百科 options look like this:

<option lat="44.976728" lon="-73.30257">Alburg</option>

Thanks for reading, sorry it's so long! It's 2:50AM, my eyes are bleeding! :P Help is MUCH appreciated...


You can split your option's value to get the latitude and longitude.

Validate your field with a regex like -?[0-9]+\.[0-9]+, -?[0-9]+\.[0-9]+, then explode it and get the two values :

list($lat, $lon) = explode(", ", $_GET['city']);

Here the separator is , including the space, to remove it btw.


You could try this...

if (isset($_GET['location']) AND strpos($_GET['location'], ', ') !== FALSE) {
   list($latitude, $longitude) = explode(', ', $_GET['location']);
}

...though this will also split successfully if someone sends you something funny like hello -4.3, 3.1 bye!.

Ideone.


Alternatively, you could use this regex to validate it and extract the values.

if ($location = filter_input(INPUT_GET, 'location')) {
   preg_match('/^(?P<latitude>-?\d+\.\d+), (?P<longitude>-?\d+\.\d+)\z/', $location, $matches);

   $latitude = $matches['latitude'];
   $longitude = $matches['longitude'];
}

Ideone.

The issue above of extra data either side won't be an issue with the regex.


I'd advise that you use a unique ID as the option's value attribute and then load the latitude and longitude for the selected town from your database based on the ID the user selects.

If you insist in doing it this way, then you can use explode() to split the returned value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜