View last X posts in phpBB

October 18, 2006

This will query your phpBB database and display the last SHOW_HOWMANY post subjects. I couldn’t find anything that did this already, so I wrote this.

Use at your own risk.

If you are utilizing joomla, you can integrate this into your site easily with mod_moslate

<?php

$SHOW_HOWMANY=5;

$main_url=http://forums.hostname.com;
$view_topic=”/viewtopic.php”;

$user=”your_user”;
$password=”password”;
$database=”db_name”;
$db_host=”hostname”;

$conn = mysql_connect($db_host, $user, $password);

if (!$conn) {
echo “Unable to connect to DB: ” . mysql_error();
exit;
}

if (!mysql_select_db($database)) {
echo “Unable to select $database: ” . mysql_error();
exit;
}

$sql = “SELECT t2.post_subject,t2.post_id FROM phpbb_posts_text t2 JOIN (SELECT post_id FROM phpbb_posts ORDER BY post_time DESC LIMIT $SHOW_HOWMANY ) t1 ON t1.post_id = t2.post_id”;

$result = mysql_query($sql);

if (!$result) {
echo “Could not successfully run query ($sql) from DB: ” . mysql_error();
exit;
}

if (mysql_num_rows($result) == 0) {
echo “No rows found, nothing to print so am exiting”;
exit;
}

while ($row = mysql_fetch_assoc($result)) {
echo ‘<a class=”mainlevel” href=”‘, $main_url, $view_topic, ‘?t=’, $row['post_id'], ‘”>’, $row["post_subject"], ‘</a>’;
}

mysql_free_result($result);
?>

Comments

Got something to say?