开发者

How do use location based search in Wordpress?

开发者 https://www.devze.com 2023-01-29 09:15 出处:网络
I re开发者_如何学运维cently downloaded wordpress for a coupons website and want to give online users the option to search coupons/offers by zipcode. How can I do that?? I know I would need to have a l

I re开发者_如何学运维cently downloaded wordpress for a coupons website and want to give online users the option to search coupons/offers by zipcode. How can I do that?? I know I would need to have a list of the available zip codes, but Im not sure how I would go about doing that. Maybe there is a plugin/mod out there the I don't know about.

I would really appreciate and help on this. The site that this will be for will be driven by entries that our clients post into the site and coupon deals and other offers. Where consumers can search for deals a numbers ways by category, by type of service, by provider, by amount, and by location.

I hope this was enough information to help.

Thanks a lot.


You could use a service (such as MaxiMind or Geo-IO) to get the users location. Then you can pull coupons from the database if they're for places near the user.

This would require some custom coding on your part, but if you're setting out to build a coupons website then I would assume that you're not afraid to do a little coding :)

EDIT:

Using Geo-IO first you will get the users IP address:

$IP = $_SERVER['REMOTE_ADDR'];

Then you're going to create a request and get results from Geo-IO:

$data = file_get_contents("http://api.geoio.com/q.php?key=[Your_Key_Here]&qt=geoip&d=pipe&q=".$IP);

Then, what you'll do is break the results into an array:

$location = explode('|', $data);

Now you're ready to use the data in $location to search your database for coupons that are near the user. If I where you I would store your coupons in the DB and use Longitude and Latitude for the location data. Then you can do some fairly easy math to determine coupons that are within a specified number a miles from the user. A good 'ol google search should help you out with that.

Hope this helps!


The best way to do it would be to have your coupons stored in a db, with the relative zipcode on each record. Best to index the zipcode also.

You would then search the database for all coupons that have the same zipcode as the user entered, or possibly as the users zipcode.

CODE/DB

This is how I would approach it:

Firstly, stet up a table in your wordpress database to contain the coupons, it would depend on what info you want to store, but something along the lines of this:

Table: CouponStore
Rows:
ID | Name | Type | Description | Store | ZipCode

Then when the customer inputs their zipcode, take hold of it (possibly make use of the XMLHttpRequest object).

$ZipCode = acleaningfunction($_GET['ZipCode']);
if (isset($ZipCode) && !empty($$ZipCode)) {
    $result = mysql_query("SELECT * FROM CouponStore WHERE `ZipCode` = '".$ZipCode."'");
    if ($result) {
        while ($row = mysql_fetch_assoc($result)) {
            if (!$row) {
                die('No rows returned.');
            } else {
                return $row['Name'].', '.$row['Store'].', '.$row['Description'].', '.$row['Type'].', '.$row['ZipCode'].', '.$row['ID'];
            }
        }
    } else {
        die('Invalid query: '.mysql_error());
    }
}

No guarantee on that code being perfect, but its gives you an idea. If you want more in depth code that is exactly relative to wordpress, and works, post here and I'll hapily get onto it.

Cheers Charlie

0

精彩评论

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