Selecting a record with the next closest date using SQL


I have been recently working on a trip planner application where I have had to display the next trip that a user has coming up. This involved having to make a comparison between the starting date of the trip and the current date to find the trip that met this criteria. I thought I would share the basic query of how you can do this should anyone need to do accomplish a similar task.

 

Given the Following:
- My table name is trip_list
- The starting date of the trip (which we will compare) is named date_from
- The dates stored into the date_from field are stored in the format of dd-mm-yyyy

SELECT * FROM trip_list WHERE trip_list.date_from =
(SELECT MIN(trip_list.date_from) FROM trip_list WHERE
trip_list.date_from >= DATE_FORMAT(NOW(),'%d-%m-%Y'))

What I am doing here is selecting all columns in the table where the starting date is equal to the lowest date that is after the current date. At the end of the query I format the output of the current date to be dd-mm-yyyy for comparison since this is an application for the Australia / New Zealand region and that’s their date structure.

 

That’s all there is to it.

 

- Jonathan LeBlanc

  • Share/Bookmark


Automatically Updating the Small View for All Users in a YAP Application


In my last post I provided code to update the small view for an application on the Yahoo! Application Platform. The only current dropzone for these applications is on http://my.yahoo.com. In this tutorial I want to provide a method for developers to automatically update the small view for every user of their application without their interaction. With this script in place, you can run it within a cron job nightly so you can let technology do all the work for you.

 

Here’s a little overview of the application that I tied this functionality to (to see the application in action go to: http://apps.yahoo.com/-Oob7ZC78). I built out a weather application that allows users to set their city and get a simple weather forecast. On the small view I display a 4 day forecast and the current weather for the day. Since most users will only set their city on an infrequent basis and only interact with the small view regularly, I needed a way to update that cached small view on a daily basis. This would normally be done when a user goes to the large view of the application, but that was not acceptable in this case.

 

I have a basic mysql table that contains the GUID of the user to identify them, as well as their city search criteria to rebuild the user views with their selected city criteria. The basic table looks like this:

Table Structure

 

Now let’s take a look over the code that updates the small views based on the table data above. Here are the steps I’m taking:

  • Include the PHP SDK (Yahoo.inc), define your keys and application id, then instantiate a 2-legged OAuth session using new YahooApplication(…)
  • Set your database information, initialize the mysql database connection, set the SQL query and make the query using the mysql_query method
  • Set up the while loop to keep capturing the specific row as long as there are still hows to capture
  • Create your small view HTML using the data from the database – store it to a variable ($html)
  • Set the small view for the user GUID using the created small view HTML. Echo out a status message for some detail.

Let’s take a look at the code for this:

 

<?php
require_once('apis/Yahoo.inc');

//session information
define('API_KEY', 'KEY HERE');
define('SHARED_SECRET', 'KEY HERE');
define('APP_ID', 'APP ID HERE');

//instantiate session
$session = new YahooApplication(API_KEY, SHARED_SECRET, APP_ID);

//db information
$dbhostname = 'YOUR INFO';
$dbusername = 'YOUR INFO';
$dbpassword = 'YOUR INFO';
$dbname = 'YOUR INFO';

//for each user in the database, update the small view
mysql_connect($dbhostname, $dbusername, $dbpassword) OR DIE ('Unable to connect to database. Please try again later.');
mysql_select_db($dbname);
$query = "SELECT * FROM user_store";
$result = mysql_query($query);
while (($row = mysql_fetch_assoc($result)) !== false) {
    if ($row['uid'] && $row['search_type'] && $row['search_value']){
        //***********************************
        //Create HTML for the small view here
        //***********************************

        //set the small view of the current user $row['uid']
        //with the small view HTML stored in $html
        if (!$session->setSmallView($row['uid'], $html)){
            echo "NO SESSION SET FOR {$row['uid']}\n";
        } else {
            echo "SESSION SET FOR {$row['uid']}\n <div style='width:300px'>$html</div>";
        }
    }
}
?>

 

Now that we have the script ready, just set up a cron job to run through this script whenever you want to update the user content and that’s it…you now have an automated script that will keep the small view for all of your users up to date.

 

– Jonathan LeBlanc

  • Share/Bookmark