I need to echo and order data from several different SQL tables. I cannot use UNION as all the tables are very different (there are 6). I have ordered each individual table by timestamp but need to order them all by timestamp so the most recent event from all the tables is at the top of the echo.
Is there a simple php solution or an AJAX or jquery solution?
Here’s my current code:
<?php
$result = mysql_query("
SELECT * FROM news
UNION ALL
SELECT * FROM feature ORDER BY timestamp DESC LIMIT 2
")or die(mysql_error());
while($row = mysql_fetch_array($result)){echo'News: <a href="index.html">'.$row['title'].'</a><br>' ;}
?>
<?php
$result = mysql_query("
SELECT * FROM members WHERE artist='Y'
ORDER BY timestamp DESC LIMIT 2
")or die(mysql_error());
while($row = mysql_fetch_array($result)){echo'New Artist: <a href="artists/artist.php? artist='.$row['artistname'].'">'.$row['artistname'].'</a><br>' ;}
?>
<?php
$result = mysql_query("
SELECT * FROM gigs
ORDER BY timestamp DESC LIMIT 2
")or die(mysql_error());
while($row = mysql_fetch_array($result)){echo'New Gig: <a href="artists/gigs.php? artist='.$row['artistname'].'">'.$row['gigname'].' + '.$row['artistname'].'</a><br>' ;}
?>
You could possibly execute a
.sort()function with PHP on the returned timestamps –http://php.net/manual/en/function.sort.php
Gather all of your data together, query by query and push them into one singular array, then perform the sort & reverse or better yet Darhazer’s suggestion and use
.rsort()(reverse sort) to manipulate your data in to (reverse) sequential order.