Andrew Plummer - Design Development Translation

Example 15: Relative Dates Using Select Elements and Data Formatting

Summary

This is the final example and builds on everything we have done so far. Although still only a small portion of TableGear's potential, this example is to give you an idea of what is possible by creatively using different options that interact with each other.

When working with incremented select elements, numeric data is typically what springs to mind. But by applying data formatting here, we can achieve some very cool results. Here, we are creating select elements that are relative to dates in the database.

Example

View Example

PHP Code

$tg = new TableGear(array( "database" => array( "username" => "USERNAME", "password" => "PASSWORD", "database" => "DATABASE", "table" => "TABLE" ), "sortable" => "all", "editable" => "all", "selects" => array( "date" => "increment[step=86400,range=432000]" ) "loading" => array( "tag" => "img", "attrib" => array( "src" => "/images/icons/loading.gif", "alt" => "Loading", "class" => "loading" ) ), "formatting" => array( "price" => "currency[prefix=$,pad], "date" => "date" ), "totals" => array("price", "quantity") );

You will notice in this example that our increment parameter has some very large numbers in it. This is because in the database our date column is actually a timestamp, which is a numeric format that represents seconds. By specifying a step value of 86400, we are telling TableGear that we want each option element in our selects to step by 1 day, or 60 seconds/min * 60 minutes/hr * 24 hours/day = 86400. Our range parameter is 86400 * 5 = 432000, which says that we want a range of 5 days.

That's fine, but our select element will still be an incomprehensible series of numbers, right? Well, the values will still be. After all, we want our data to remain in timestamp format in the database. However, by specifying the date format for our column, all select elements in that column will automatically receive the formatting as well. The result: select elements that contain days instead of numbers and have an option element for each day both 5 days before and after the cell's current data. In the example each option is incremented by one day, but this could be anything – weeks, months, years.

Hopefully this example makes it clear the power that TableGear makes available. When we start using options together, we can achieve a lot of interesting results. Play around with the different options, and you might be suprised with what you can accomplish.