Unstoppable Domains

[PHP] Getting info from two tables and sorting it

Spaceship Spaceship
Watch

liam_d

The original NP Emo KidEstablished Member
Impact
25
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
.US domains.US domains
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
•••
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
•••
Why not store the topics and replies in the same table?
 
0
•••
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
•••
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
•••
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
•••
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
•••
Yeah i didn't have a clue either, but it works perfectly.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99

We're social

Unstoppable Domains
Domain Recover
NameMaxi - Your Domain Has Buyers
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back