Topic: How do MySQL queries in PunBB work?
Hello everybody,
Recently I decided to add some modification to my Punbb installation, but I had to face the fact that I don't have even a tiniest idea of how SQL queries are working in PunBB.
So far I tried everything I knew on this query which is supposed to bring first post of a topic created in a specific forum and display it as a news entry. However all I get is that it displays all posts made in that forum.
<?php
define('FORUM_ROOT', 'forum/');
require FORUM_ROOT.'include/common.php';
define('RECENT_POSTS_SHOW_POST', true); // Set to false to show topic subject only
$query = array(
'SELECT' => 'p.id, p.message, t.subject, t.poster, t.num_replies, t.posted, p.poster_id, t.num_replies',
'FROM' => 'topics AS t',
'WHERE' => 't.forum_id = 2 AND t.sticky = 0',
'JOINS' => array(
array(
'LEFT JOIN' => 'posts AS p',
'ON' => 'p.topic_id = t.id'
)
),
'ORDER BY' => 't.posted DESC',
'LIMIT' => '0,10'
);
$result = $forum_db->query_build($query) or error(__FILE__, __LINE__);
$recent_posts = array();
while ($cur_post = $forum_db->fetch_assoc($result))
$recent_posts[] = $cur_post;
// Print out posts
if (!empty($recent_posts))
{
foreach($recent_posts as $cur_post)
{
$message = str_replace("\n", "<br />", $cur_post['message']);
$date = date("j M Y", $cur_post['posted']);
echo '
<h2>'.$cur_post['subject'].'</h2>
<p class="info">Written by <a href="'. FORUM_ROOT .'profile.php?id='.$cur_post['poster_id'].'"
title="'.$cur_post['poster'].'\'s profile">'.$cur_post['poster'].'</a> on '.$date.' |
<a href="'. FORUM_ROOT .'viewtopic.php?id='.$cur_post['id'].'" title="View Replies" >'.$cur_post['num_replies'].' Comments</a></p>
<p>'.$message.'</p>';
}
}
else { echo '<p>We are sorry but no news were found in the database, if you believe this is an error, please notify the administrator.</p>'; }
?>
Could somebody explain it please? I tried searching though the WiKi but there seems to be no articles about it.
Thanks a lot in advance!