2

Hi I have a geolocation function that Im using for my wordpress based city that I want to use show location related posts based on a web visitor's location.

The function I'm is using this:

<form action="" method="POST" name="u_select_radius" id="u_select_radius">
     <select>
        <option value="5">5 Miles</option>
        <option value="10">10 Miles</option>
        <option value="20">20 Miles</option>
        <option value="50">50 Miles</option>
        <option value="100">100 Miles</option>
     </select>
        <input type="submit" value="Search" name="Search" class="blue" />
     </form>


    <?php //begin geoposty demo 

    $localCoupons = new WP_Query('post_type=post&posts_per_page=-1&orderby=meta_value&meta_key=City&order=ASC');

    if ($localCoupons->have_posts()): while ($localCoupons->have_posts()): $localCoupons->the_post();


        if (function_exists('geoDistanceFrom') && $city != '' && $state != '') {

          ///get user radius selection
          if ( isset($_POST['u_select_radius'])) {
            $radius = intval($_POST['u_select_radius']);
          } else {
            $radius = 100;
          } 

          if (geoDistanceFrom($city . ',' . $state) < $radius /* Mile radius */) {

                echo '<h3>' . get_the_title() . '</h3>';
                echo '<p>' . $city . ', ' . $state . '</p>';
                // the_content();

            }

       } 

    endwhile; endif; 

    // end geoposty demo
    ?>       

    </div>

This function checks if the plugin function (geoDistanceFrom) exists and then check for certain criteria to base the posts on (city and state). Where it shows "100" (the mile radius), I want to allow users to choose the radius they want (Ex. 5, 10, 20, 50, 100, etc)

I think I would need to create an array with predefined values (5, 10, 20, 50, etc) to choose from. I need some help. How do I allow users to choose values for this function?

Please let me know if there is any additional required for this solution.

Thanks a lot!

2 Answers 2

2

My suggestion would be to create a simple form with drop down list box that has the values you want the user to be able to choose. Check that it's action set to the script which does the geolocation, and then set a variable for the radius to whatever it is the user has chosen.

<form action="geolocation_script.php" method="POST" name="u_select_radius" id="u_select_radius">
 <select>
    <option value="5">5 Miles</option>
    <option value="10">10 Miles</option>
    <option value="20">20 Miles</option>
    <option value="50">50 Miles</option>
    <option value="100">100 Miles</option>
</select>

geolocation_script.php And then have some code which grabs their choice

<?php

if (function_exists('geoDistanceFrom') && $city != '' && $state != '') { 

  //get user radius selection
  if ( isset($_POST['u_select_radius'] ) {
    $radius = intval($_POST['u_select_radius']);
  } else {
    $radius = 100;
  }

  if (geoDistanceFrom($city . ',' . $state) < $radius /* Mile radius */) {

...

Mind you, this code isn't perfect, but should give you something to work with.

4
  • Thanks, but I would like to process the form on the same page (SELF)...What thing I noticed is that there's no submit button (which I added), but when I submit the form it takes me to the based domain. And for some reason the page loads but the posts don't load. I updated this post with more code details. Any ideas?
    – gdinari
    Commented May 6, 2011 at 19:52
  • I'm pretty sure if you leave the forms "action" blank it will submit to itself. Otherwise try <form action="<?php echo $_SERVER['PHP_SELF']; ?>" ..> I'm not a pro with Wordpress, but maybe try to "echo $localCoupons->the_post();" within your while loop to display the posts. Can't be sure though sorry.
    – nahanil
    Commented May 6, 2011 at 22:29
  • <?php the_content(); ?> or <?php the_post(); ?> should print it out from what I've found in Google.. It may seem like a silly question, but you do realise that the_content(); is commented out in the code you've posted above...
    – nahanil
    Commented May 6, 2011 at 22:39
  • yes I realized this. Im just testing the code first. I'll include everything once I get it working. I'll try your suggestion and see what I come up with. Thanks a lot for your advice :D
    – gdinari
    Commented May 6, 2011 at 23:07
0

Presumably you'd be presenting this option in a form, so just use a select:

<select name="distance">
   <option value="5">5 miles</option>
   <option value="10">10 miles</option>
   etc...
</select>

How you generate that is up to you. It could simply be hard coded into the page, or you could store the allowable distances somewhere and build the options dynamically.

Then simply retrieve the value with the usual form handling tools when it's submitted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.