I’ve got a PHP script that integrates with MySQL to send a .plist file out to an iPhone app that contains two types of items (the items are songs):
- items that should already be displayed in the app
- items that should be displayed at some point in the future
I grab past and future items (I want variable quantities of each) in the desired order using two queries and some PHP, which is completely workable. Mostly because I couldn’t find or come up with a single query solution, though, I’m curious how you all would solve this. Is there a single query to get the rows in the order I want? Is there a single query to get the rows in some other order?
Anyhow, here’s the PHP and SQL that grabs the result rows I want and puts them in the order I’d like them to appear in in the resulting .plist. I use UNIX timestamps mostly because it’s easier, so I hope I won’t be chastised–I don’t think it particularly affects my particular question.
$readbehind = mysql_query(sprintf('SELECT SongID, RemoteSongLocation, SongTitle, UNIX_TIMESTAMP( ScheduledActivationDateTime ) AS ActivateAfterUnixTime FROM `thesongs` WHERE UNIX_TIMESTAMP( ScheduledActivationDateTime ) < UNIX_TIMESTAMP( ) ORDER BY ScheduledActivationDateTime DESC LIMIT 0, %d', MAX_READBEHIND_SONGS));
$readahead = mysql_query(sprintf('SELECT SongID, RemoteSongLocation, SongTitle, UNIX_TIMESTAMP( ScheduledActivationDateTime ) AS ActivateAfterUnixTime FROM `thesongs` WHERE UNIX_TIMESTAMP( ScheduledActivationDateTime ) > UNIX_TIMESTAMP( ) ORDER BY ScheduledActivationDateTime ASC LIMIT 0, %d', MAX_READAHEAD_SONGS));
$songsForClient = array();
while ($row = mysql_fetch_assoc($readbehind)) {
$songsForClient[] = $row;
}
while ($row = mysql_fetch_assoc($readahead)) {
$songsForClient[] = $row;
}
you can connect both statements using union
http://dev.mysql.com/doc/refman/5.5/en/union.html
or you just select all and exclude the one currently played using
WHERE SongID != ' . $idyou may have to check if one of the SongIDs is used multiple times maybe or use another sql column for that