Display Last X Posts on front page
This modification is in the archives.
Hey all,
This MUST have been done, I see it was requested something like a million times by various people, and it's probably the single most common hack ever done, heh. But surprisingly, I could not for the life of me find a mod that did what I wanted. What I wanted is very simple. Display the last 10 postings on my front page. Not by thread. Not the last 10 posts of the last 10 threads, but the LAST 10 POSTS, even when they happen to be in the same thread. I achieved this by modifying a mod that already existed (I actually installed it thinking it did what I wanted, but in fact it only displayed the last 10 posts that were in different threads). It's not my intent to take full credit for this mod, the groundwork was already laid by someone else (I'd provide a link to the original, but I can't seem to find it now, grrr, perhaps somebody knows it and can provide the link). So, here is my version. It provides the time, thread name, and poster name, with links to the post and to the user profile. You can see an example of it working on my front page (http://www.rjcyberware.com). The only thing I don't like is that it uses server time in the display. I'd like to change that to the user local time, but I'll have to look over the php time commands to see if it's possible to do. Here is the code Code:
<?php /* This script shows the last X numbers of posts (titles) posted last on a non-VB page. You may customize it in any way you wish. */ ## CUSTOMIZE SETTINGS FOR YOUR SITE ## $db_host = "localhost"; // Change this if your MySQL database host is different. $db_name = "DATABASE"; // Change this to the name of your database. $db_user = "USER"; // Change this to your database username. $db_pw = "PASSWORD"; // Change this to your database password. $db_prefix="PREFIX"; //Change this to your database prefix, if any. Leave blank if you don't use a prefix. $forum_url = "http://www.YOURSITE.com/forums"; // Change this to reflect to your forum's URL. $limit = "10"; // Number of posts displayed. $titlecolor = "#000000"; // This is the color of the title (first line) $linkcolor = "#404040"; // This is the color of the linked text (second and third line) $backgroundcolor = "#ffffff"; // Set this if you want a different background color than the default, otherwise leave empty $txtlimit = "100"; // This is the character limit. ####################################### // Connecting to your database mysql_connect($db_host, $db_user, $db_pw) OR die ("Cannot connect to your database"); mysql_select_db($db_name) OR die("Cannot connect to your database"); $table_post = $db_prefix."post"; $table_thread = $db_prefix."thread"; $result = mysql_query("SELECT postid FROM $table_post WHERE visible=1"); $num_rows = mysql_num_rows($result); echo "<CENTER><U><B>Latest Forum Activity</U><BR>"; echo "Total Posts: $num_rows</CENTER></B><BR><BR>"; // Below is the beginning of a table. If you feel you don't need it, you may remove it. ?> <table border="1" cellspacing="0" bordercolordark="white" bordercolorlight="black" width="100%"> <?php if ($limit) { $limited = "LIMIT $limit"; } $post_sql = mysql_query("SELECT postid,threadid,username,userid,dateline FROM $table_post WHERE visible=1 ORDER BY postid DESC $limited"); while($post_get=mysql_fetch_array($post_sql)) { $dateline= $post_get['dateline']; $date2 = date ("d M Y h:i a" ,$dateline); $tid = $post_get['threadid']; $thread_sql = mysql_query("SELECT title FROM $table_thread WHERE threadid=$tid"); $thread_get = mysql_fetch_array($thread_sql); $title = $thread_get['title']; $title = substr($title,0,$txtlimit); $pid=$post_get['postid']; $poster = $post_get['username']; $userid = $post_get['userid']; //the output line echo "<tr'><td BGCOLOR=\"$backgroundcolor\"> <font size='2' COLOR=\"$titlecolor\">$date2<BR></font> <a href=\"$forum_url/showthread.php?p=$pid#post$pid\"><font size='2' LINK=\"$linkcolor\" VLINK=\"$linkcolor\">$title</font></a><BR> <a href=\"$forum_url/member.php?userid=$userid\"><font size='2' LINK=\"$linkcolor\" VLINK=\"$linkcolor\"><i>$poster</i></font></a> </td></tr>"; } echo "</table>"; ?> <?php include ("/YOURPATH/DisplayPosts.php"); ?> If anyone has any suggestions for improvements, feel free. Ron [EDIT v1.1: Changed code to make the database prefix a modifiable field which gets propigated into the database queries] [EDIT v1.2: Changed code such that the modifiable fields "$titlecolor" and "$linkcolor" actually do something useful, and added in "$backgroundcolor" to change the table entry backgrounds. Broke up the output line to make it a little easier to read] Download No files for download. |