Review App

Internet Entrepreneurs Blog

Archive for May, 2006

Easy show/hide CSS/Javascript code

Sunday, May 28th, 2006

I was looking for some quick and simple code for building a hideable div and I found a number of examples that were junk. One of the most popular pages I found contained some really cumbersome code that was visibly slow in both Firefox and Safari. Then I found the code on White Wave Designs and it is the shizzle. Works fast, is simple, and was easy to understand and implement. Now I’m using the code on singletracks and it works great!

Online calendar: recurring events pseudo-code

Thursday, May 18th, 2006

I recently needed some help building recurring event functionality into a web calendar I was building for Goodvan and I just couldn’t find what I was looking for online. I came up with a few different ways to do it but in the end decided the following is the best method.

Say you already have a very simple calendar table (called Cal) that has an id, event title, and a date. The next step is to create an identical table for recurring events (let’s call it CalRepeat) with the same fields as Cal. Now we need to add a field to Cal called RepeatID to link the two tables (Cal.RepeatID -> CalRepeat.id).

Now we need a separate form for users to enter recurring events, and I’m assuming you already have a form for adding regular events to Cal. When a user adds a recurring event they will enter how often the event is repeated (once a month, every Sunday, etc.) and when the series ends (it will need to be finite for reasons that will become apparent shortly).

Upon submission your script will first need to add a new entry into CalRepeat. Then using the CalRepeat id, loop your script through all the dates that match the criteria (say the 15th of every month through Jan 2008) and add them as new entries into Cal with RepeatID equal to the CalRepeat id.

Now if you need to edit a single event (say just this month’s meeting was moved to the 16th) you only need to update a single record in Cal. Updating an entire series is almost as easy but you’ll need to have kept track of frequency and end date information to make this work. To update the entire series, first delete all the events with RepeatID equal to the Repeat event in question then add them again using the new criteria.

This is a very general description of making a recurring event calendar and you may still find it challenging to set up the necessary loops for adding multiple events or for storing information about frequency in your CalRepeat table. I hope this article can at least get you started on making your very own online calendar!

Zip code radius calculation algorithm

Sunday, May 7th, 2006

Here’s a neat little method I figured out for finding geographic points (waypoints) located within a certain radius of a “master point.” To make this a bit more concrete, let’s say you want to find all the zip codes within a 10 mile radius of your zip code, 27701 (Durham, NC). Most good zip code databases will have the latitude and longitude coordinates (waypoints) of each zip code, but this is alot of data (my database has over 79,000 zip code entries). It is far too much data to calculate a distance between the master point and each zip code so you need to find a way to quickly zoom in on the points that are most likely to be within your radius.

I’ve included the image above to help in this illustration. In this image X marks my master point, Durham, NC. The various green stars represent other zip codes around Durham. The first step is to determine the boundaries of our radius. A good rule of thumb to use is to assume 1 degree of longitude is about 70 miles wide while a degree of latitude is about 50 miles (this isn’t exact but we’ll improve this estimate when we calculate the actual radius).

In this case, our target zip codes will be between longitudes ‘a’ and ‘b’ while the latitudes will be between ‘c’ and ‘d.’ These can be calculated easily by adding and subtracting our radius ‘r’ from our center point X to get each limit. We can now query our database to return just those zipcodes where the latitudes are between ‘c’ and ‘d’ and longitudes are between ‘a’ and ‘b’ (example: SELECT * FROM zip WHERE latitude >= ‘c’ AND latitude <= ‘d’ AND longitude >= ‘a’ AND longitude <= ‘b’)This query returns 8 points but a few of these are outside our 10 mile radius we originally set. But now we’ve narrowed our consideration set down from 79,000 to just 10 points. We can calculate the distance between X and each of these points to see if they lie within the radius ‘r,’ leaving those points within the shaded circle as our result.The equation I use to calculate distances between two waypoints is:

$radius = 3959 * acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($long2 - $long1)); where $radius is in miles

There are a number of ways to calculate these distances that can be as complicated as you like but again, this should be close enough for most applications. Realize, however, that if you try to make this more precise by using a more complicated equation, you’ll severely increase the processing time required to calculate distances. Also note that increasing the radius under consideration can exponentially increase processing times depending on the nature and size of your dataset.

Many companies offer scripts for sale that calculate zip code radiuses and distances but with a little work and using this framework, you can create your own application. Take a look at an example of my implementation at: singletracks.