PDA

View Full Version : Display Last X Posts on front page


RonHiler
04-01-2005, 10:00 PM
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 (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

<?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>";

?>

To use it, save it as a file (as say "DisplayPosts.php", and where you want to display the posts, just add in the line:
<?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]

Guy G
04-02-2005, 06:07 PM
Im sure you could save all that mysql code with vbulletin's built in functions.

RonHiler
04-02-2005, 07:38 PM
Did you mean connecting to the database and retrieving all the various things out of the database? Yeah, probably very true. On the other hand, it's easier for me to write it from scratch than to try to look up all the various functions inside the vb package. It's not very much code, after all :)

Ron

b4ne
04-02-2005, 07:52 PM
will this show posts of "secret board" ?

b4ne
04-02-2005, 08:02 PM
I get this error message any idea what I could have done wrong ?

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/b4ne/www/apb/board/DisplayPosts.php on line 24
Latest Forum Activity
Total Posts:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/b4ne/www/apb/board/DisplayPosts.php on li

tnguy3n
04-02-2005, 08:20 PM
Interesting, but like Guy G said, I'd prefer using vb built-in functions. :)

hendri
04-02-2005, 09:19 PM
Interesting, but like Guy G said, I'd prefer using vb built-in functions. :)
nothing show up ????

Selene
04-02-2005, 09:20 PM
i got same error

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in (my url) on line 24

Latest Forum Activity
Total Posts:

hendri
04-02-2005, 09:31 PM
i got this erroe msg

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in (my url) on line 24

Latest Forum Activity
Total Posts:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (my url) on line 37

tnguy3n
04-02-2005, 10:05 PM
in line 34, replace vb_post with post if you don't use table prefix.
also in line
$post_sql = mysql_query("SELECT postid,threadid,username,userid,dateline FROM vb_post WHERE visible=1 ORDER BY postid DESC $limited");
and
$thread_sql = mysql_query("SELECT title FROM vb_thread WHERE threadid=$tid");

TCattitude
04-02-2005, 11:01 PM
I saw this hack-mod once.
Is for a non-VB page, that's why it get all that data directly from DB.
And that's why it wont work if you put it direct into a vb-page (in a template for example)

RonHiler
04-02-2005, 11:28 PM
Ah, sorry 'bout that. I forgot to mention about prefixes. Tnguy3n is correct, you have to substitute your own database prefix (if any). My prefix for my database tables is "vb_". If you don't use a prefix, change these lines as follows:
$result = mysql_query("SELECT postid FROM vb_post WHERE visible=1");
to
$result = mysql_query("SELECT postid FROM post WHERE visible=1");

$post_sql = mysql_query("SELECT postid,threadid,username,userid,dateline FROM vb_post WHERE visible=1 ORDER BY postid DESC $limited");
to
$post_sql = mysql_query("SELECT postid,threadid,username,userid,dateline FROM post WHERE visible=1 ORDER BY postid DESC $limited");

and

$thread_sql = mysql_query("SELECT title FROM vb_thread WHERE threadid=$tid");
to
$thread_sql = mysql_query("SELECT title FROM thread WHERE threadid=$tid");

(pretty much just what tn said).


will this show posts of "secret board" ?

I don't *think* so (the "WHERE visible=1" should prevent non-public posts from showing up, if I understand that database entry correctly), but I haven't actually tested it. Let me know if it doesn't work as intended, and I'll see what I can do. I'll work on the code as well so there's an entry in the "modifiables" section for the database prefix so it's more obvious.

Ron

Selene
04-02-2005, 11:50 PM
ok thxn its working now, how can i change bg color and stuff?

Selene
04-03-2005, 01:06 AM
i have tried to use it as a postnuke block, so the block on right side displays the latest forums posts.
but it gives error. the page is working fine from direct url, but when i use it as block it doesnt work. Any suggetions

RonHiler
04-03-2005, 01:51 AM
Updated the code to make the database prefix issue more user friendly.

Selene: What error are you getting with postnuke?

To answer your question about the table color, there's a few ways you can change it. To change the background color, put an entry in the tr field. For the font color, unfortunately, because we are dealing with static text, links, and (possibly) visited links, it's a bit more complex. Also, I've set it up so that you can control the fonts for each field individually, so we have three different fields to deal with (because I have < font > in three seperate places, I don't think you can set the three entries globally, as the < /font > would kill them, so you have to do them one at a time, if I'm not mistaken).

So let's say you wanted black background with white text, the resulting output line would look like this:

echo "<tr bgcolor='black'><td><font size='2' color='white' link='white' vlink='white'>$date2<BR></font><font size='2' color='white' link='white' vlink='white'><a href=\"$forum_url/showthread.php?p=$pid#post$pid\">$title</a></font><font size='2' color='white' link='white' vlink='white'><i><BR><a href=\"$forum_url/member.php?userid=$userid\">$poster</a></i></font></td></tr>";

Ugly, I know. But it should work :) I haven't tested it though, so I make no promises. You could shorten it a little bit by being more specific for particular lines (for instance, we know the first line (the date) is static text, so there's really no need to set the link or vlink colors there, I'm doing it just for consistency sake).

Hope that helps.

Ron

Selene
04-03-2005, 01:54 AM
Hi Ron, gr8 ! i will try that out so thats sorted! well its just when i try to make the latest forums page open as block using php in postnuke i get error:

u can check http://67.19.62.198/post/

i also opened a thread on postnuke site : http://forums.postnuke.com/index.php?name=PNphpBB2&file=viewtopic&p=154711


Otherwise gr8 and useful hack! and good work :)

hendri
04-03-2005, 09:12 AM
Hi Ron, gr8 ! i will try that out so thats sorted! well its just when i try to make the latest forums page open as block using php in postnuke i get error:

u can check http://67.19.62.198/post/

i also opened a thread on postnuke site : http://forums.postnuke.com/index.php?name=PNphpBB2&file=viewtopic&p=154711


Otherwise gr8 and useful hack! and good work :)
done no problem already !

hendri
04-03-2005, 10:05 AM
done no problem already !
www.aroclubindonesia.com

arowana club indonesia !

Selene
04-03-2005, 11:43 AM
hendri i still cant get it to work with postnuke as block. can u see the url in my previous post and check the error. wud be nice if I can get it to work in postnuke

jugo
04-03-2005, 06:31 PM
I've had something like this on my site for a while: http://www.s3squad.com

Check these links out:
https://vborg.vbsupport.ru/showthread.php?t=62823
https://vborg.vbsupport.ru/showthread.php?t=62624


But noine that use the built in vB functions. Would be nice.

RonHiler
04-03-2005, 07:20 PM
Jugo, thanks for the links! That Mod was my starting point that I changed to make mine. I was hoping someone would post it :) I don't know WHY I couldn't find it again. RobinHood deserves a good deal of the credit for my contribution.

It doesn't dispaly the last X posts, which is why I had to modify it. It displays the last 10 posts that are in different threads, which is not what I wanted. It's a subtle difference, but an important one for me :)

Selene: I wish I could help, but I know next to nothing about postnuke. Probably your best bet is to post the code for the mod in the postnuke forum (just make sure to remove your real database name, database user, password, and path information first). They would probably have a better idea of what is going wrong than the people here.

New version 1.2: Based on Selene's request, added values for the text color, links colors, and background color to the modifiables section. All you need to do is set these values, and the output should appear in the desired colors.

[BTW: Can a moderator/admin move this thread into the vB3 section? I have no idea how it ended up in vB2 mods, I must have screwed up :) - Thanks]

Ron

jugo
04-04-2005, 06:44 PM
hendri i still cant get it to work with postnuke as block. can u see the url in my previous post and check the error. wud be nice if I can get it to work in postnuke


SELENE

You have to remove the:

<?php and ?> Lines from the code since postnuke will insert the code into it's existing functions.

jwashburn
04-25-2006, 07:50 PM
What would I change to make it show it shows only the first post of each of the last x number of threads.