The concept of a tip-of-the-day screen is old hat for many desktop applications, but oft-forgotten by web developers. However, as our web applications grow more complex than ever, we find ourselves in need of better ways to educate our users, introduce new features, and remind them about old ones.
Sure it’s easy enough to code a database-powered tip of the day script, but you don’t have to. Simply bookmark this page, and avoid the hassle.
First things first. Download this library: DidYouKnow 1.0
Aside: My mind was a little foggy when I wrote it, which is probably apparent in the code but it does indeed work. If you make any fixes or enhancements I’d love to include them, so send me an email.
To use this library unmodified, you must have a global ADOdb object in your script called $db (I know, I know…). After installing ADOdb and DidYouKnow on your server, do something like:
require_once('adodb/adodb.inc.php');'
require_once('tip_of_the_day.php');
$db = NewADOConnection('mysql');
$db->Connect($db_server, $db_user, $db_password, $db_name);
$dyk = new Did_You_Know(Name of your table);
Everything we need is in place now, and we can begin creating our custom tip of the day, joke of the hour, or whatever else you’re planning on doing. On that last line, you can input any table name you like so long as it doesn’t already exist - it will be created for you.Adding items to the queue is simple:
// From a string
$dyk->add('Example tip');
// From an array
foreach ($list as $item) {$dyk->add($item);}
// From a CSV file
$list = explode(',', file_get_contents($filename));
foreach ($list as $item) {
$dyk->add(trim($item));
}
Now that you’ve got some data to work with, let’s look at some of the ways you can retrieve it. The table created by DidYouKnow contains a “cursor” column. If you have no need to maintain state, you can safely disregard this, opting to fetch items by their id’s directly. However, choosing to use the cursor affords you some interesting options:
Tip of the Day
display.php
// Display the tip
$dyk = new Did_You_Know("tf_DidYouKnow");
$DidYouKnow = $dyk->fetch();
echo $DidYouKnow->fields[fact];
advance.php
$dyk = new Did_You_Know("tf_DidYouKnow");
$dyk->cursor_advance();
cron (run at midnight every day)
0 0 * * * advance.php
Random tip every pageload
$dyk = new Did_You_Know("tf_DidYouKnow");
$DidYouKnow = $dyk->fetch();
echo $DidYouKnow->fields[fact];
$dyk->cursor_randomize();
I know there are some problems with this incarnation of the DidYouKnow library, but hopefully this will save someone a few hours of painfully boring code, despite the obvious shortcomings. The methods available to you are:
add($fact) Adds an item to the table, where $fact is a string.
remove([$fact_id]) Removes an item from the table. If $fact_id is not supplied it will remove the item containing the cursor and advance the cursor once.
fetch([$fact_id]) Fetches the current item, or an item specified by the optional $fact_id
cursor_advance() Advances the cursor one row
cursor_retreat() Retreats the cursor one row
cursor_randomize() Moves the cursor to a random item
cursor_goto($fact_id) Moves the cursor to an item specified by the required argument $fact_id (even if it does not exist, so be careful)
As always, if you make any useful changes or additions, I’d love to hear from you, so let me know. If you’re looking for direction, I’d suggest creating a seperate table for the cursor so it’s state can be stored once, instead of once per row.
Thanks and good luck!