Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

[PHP] Getting info from two tables and sorting it

SpaceshipSpaceship
Spacemail by SpaceshipSpacemail by Spaceship
Namecheap AuctionsNamecheap Auctions
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
Do you mean the last 5 posts in general or by a specific user?

You could do this with a LEFT JOIN either case.
 
0
Close
Threw this together really quick, untested, but a general idea:
PHP:
<table>
<tbody>
<tr>
	<th>Thread</th>
	<th>Views</th>
	<th>Replies</th>
	<th>Last Reply</th>
</tr>
<?php

$posts = mysql_query("
	SELECT t.topic_id, t.author, t.title, t.date, t.replies, t.views, r.reply_id, r.date AS last_reply_date, r.author AS last_replier
	FROM topic_table AS t
	LEFT JOIN reply_table AS r ON (t.topic_id = r.reply_topic_id)
	ORDER BY r.date DESC
	LIMIT 5
");

while ($post = mysql_fetch_assoc($posts))
{
	echo "
<tr>
	<td><a href=\"http://www.forum.com/showthread.php?t=$post[topic_id]\" title=\"$post[title]\">$post[title]</a><br /><em>by $post[author]</em> on <em>$post[date]</em></td>
	<td>$post[views]</td>
	<td>$post[replies]</td>
	<td>by <a href=\"http://www.forum.com/showthread.php?t=$post[topic_id]#post$post[reply_id]\">$post[last_replier]</a> on <em>$post[last_reply_date]</em></td>
</tr>
";
}

?>
</tbody>
</table>
 
0
Close
Why not store the topics and replies in the same table?
 
0
Close
mholt said:
Why not store the topics and replies in the same table?

he's most likely using a 3rd party software so doesn't have the choice to do that.
 
0
Close
Peter said:
he's most likely using a 3rd party software so doesn't have the choice to do that.
Okay, that's understandable. :)
 
0
Close
This is what i was looking for:
PHP:
	SELECT text, date, title, topic_id
	FROM (
	
	SELECT text, date, title, topic_id
	FROM topics
	WHERE author = {$member['member_id']}
	UNION ALL
	SELECT r.text AS text, r.date AS date, t.title AS title, t.topic_id AS topic_id
	FROM replies r
	INNER JOIN topics t ON r.reply_topic_id = t.topic_id
	WHERE r.author = {$member['member_id']}
	) AS d
	ORDER
	BY date DESC
	LIMIT 5

Would have replied sooner but namepros only decided to email me the last reply made not the first one?
 
0
Close
woah i didn't know SQL statements were nestable like that. cool, thanks - i'll definitely remember that one. could be very useful in the future.
 
0
Close
Yeah i didn't have a clue either, but it works perfectly.
 
0
Close
Appraise.net

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back