Latest Posts on site home page

Looking for a MOD? Have a MOD request? Post here for help. (Note: This forum is community supported; phpBB does not have official MOD authors)
Scam Warning
pbcd2000
Registered User
Posts: 7
Joined: Mon Mar 24, 2008 8:33 pm

Re: Latest Posts on site home page

Post by pbcd2000 »

Put the following three lines on your web page where you want to display the posts.

<?
include("newposts.php");
?>


In a separate file called 'newposts.php' I have the below code. You will need to replace 'fedforum' to your own forum path

Code: Select all

<?php
    /** 
    * newest_posts - raw dump of newest posts from forum
    *
    * @copyright (c) 2008 ameeck / Vojtech Vondra - phpBB.cz
    * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
    */
    define('IN_PHPBB', true);
    $phpbb_root_path = 'fedforum/'; 
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);

    // Start session management
    //$user->session_begin();
    //$auth->acl($user->data);
    //$user->setup();

    // Number of posts and grabbing permissions
    // Pocet prspevku pro zobrazen a oprvnen
    $topic_limit = request_var('topic_limit', 5);
    $forums = array_unique(array_keys($auth->acl_getf('f_read', true))); //ignored in sql statement to include all topics despite permissions

// Select the last topics based on user permissions
/* $sql = 'SELECT p.post_id, p.topic_id, p.forum_id, p.post_subject, p.post_time, u.username
                    FROM ' . POSTS_TABLE . ' p , ' . USERS_TABLE . ' u
                    WHERE post_approved = 1
                        AND ' . $db->sql_in_set('forum_id', $forums) . '
                        AND u.user_id = p.poster_id
                    ORDER BY post_time DESC
                    LIMIT 0,' . $topic_limit;
    $result = $db->sql_query($sql); */

    // Select the last topics ignoring permissions
    // Vybrat posledn tmata ke kterm mme oprvnen
    $sql = 'SELECT p.post_id, p.topic_id, p.forum_id, p.post_subject, p.post_time, u.username
                    FROM ' . POSTS_TABLE . ' p , ' . USERS_TABLE . ' u
                    WHERE post_approved = 1
                        AND  u.user_id = p.poster_id
                    ORDER BY post_time DESC
                    LIMIT 0,' . $topic_limit;
    $result = $db->sql_query($sql);

    // Proper header since output not buffered
    // Poslat hlavicky pro sprvn kdovn, protoe vpis obsahu je postupn
//    header('Content-Type: text/html; charset=utf-8');

    // Now let's output the content
    // A ted vypsat obsah
//    echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Last Topics</title></head><body><div id="post_content"><strong>Last Topics:</strong><br/><ul>';
    while ($row = $db->sql_fetchrow($result))
    {
        $url = generate_board_url() . "/viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}"; //added fedforum to url

//old line $url = generate_board_url() . "viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}";
        echo '<tr><td colspan=\"2\"><a target="_blank" href="' . $url . '">' . $row['post_subject'] . '</a>, ' . ucwords($row['username']) . ', ' . date("d/m Hi",$row['post_time']).'hrs</td></tr>';
    }
    echo '</div></body></html>';
    ?>


Hope that helps.

Paul
Last edited by stickerboy on Mon Aug 18, 2008 7:33 pm, edited 1 time in total.
Reason: Use code tags in future plzkthxbai
Bluesplayer
Registered User
Posts: 8
Joined: Sun Nov 18, 2007 12:02 pm

Re: Latest Posts on site home page

Post by Bluesplayer »

Hi

What do I need to add to this code to show the forum title along with the link? I have used the above code to show the latest posts on the side panel of my mkportal. I altered the last part of the code to this:

Code: Select all

// Now let's output the content
// A ted vypsat obsah
// echo '';
while ($row = $db->sql_fetchrow($result))
{
$url = generate_board_url() . "/viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}"; //added fedforum to url

//old line $url = generate_board_url() . "viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}";
echo '<img src="/mainforum/styles/brushed_metal_II/imageset/icon_topic_latest.gif"></img> <a class="uno" target="_top" href="' . $url . '">' . $row['post_subject'] . '</a>, ' . ucwords($row['username']) . ', ' . date("d/m Hi",$row['post_time']).'hrs<br />';
}
echo '';
?> 
The mkportal is displaying the latest posts through the creation of a php block. I am using ajax to include the code in the block and that is why I have removed all the references to header etc. You can see the result on my homepage. All that is lacking now is the reference to the forum where the last post was made and the url for it.

Regards
Bluesplayer
juxair
Registered User
Posts: 6
Joined: Thu Aug 07, 2008 7:16 pm

Re: Latest Posts on site home page

Post by juxair »

hello

the code on page 1 works nicely on my site with the session.php change

Now is there a way to make it work WITHOUT that change ?

ty

regards

Juxair

PS:without the change i get the same header error as others posted
juxair
BlueSalamander
Registered User
Posts: 4
Joined: Fri Aug 08, 2008 10:52 am

Re: Latest Posts on site home page

Post by BlueSalamander »

Hi, I have used the code found on page 1 in my site, http://www.heroicfantasygames.com.

Initially I had the same problem with this error:

[phpBB Debug] PHP Notice: in file /includes/session.php on line ...: Cannot modify header information - headers already sent by

The error is due to the fact that when we start a phpbb session, phpbb creates a cookie if there is no existing cookie. A cookie can be created only at the very beginning of HTML code, it's a protocol thing. If you use HTML or PHP to display anything and then create a cookie it shows the above error.

The way I fixed that was to put this block:

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = 'Forums/'; // Path to phpbb folder
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
// Grab user preferences
$user->setup();
?>
in a separate php file that I include at the very beginning of my other pages.

I also noticed that the code does not properly set $unread_topic. That can be annoying if you wanted to display a star to show an unread topic. To set it properly I added this:

Code: Select all

$topic_tracking_info = get_complete_topic_tracking($t['forum_id'],$t['topic_id'],$global_announce_list = false);
just before:

Code: Select all

$unread_topic = (isset($topic_tracking_info[$t['topic_id']]) && $t['topic_last_post_time'] > $topic_tracking_info[$t['topic_id']]) ? true : false;
Vinyard_X43q
Registered User
Posts: 19
Joined: Wed Jun 06, 2007 8:14 am

Re: Latest Posts on site home page

Post by Vinyard_X43q »

Nice catch on the missing code BlueSalamander. I must have deleted it when I cleaned it up. I'll edit my first post in a bit.

This part is not actually needed as I believe its defaulted to false.

Code: Select all

$global_announce_list = false
I can't reproduce the error all of you seem to be getting I guess my server always buffers the output. You can try to buffer the output by using ob_start at the beginning of the script and turn off output buffering when the script is done with ob_end_flush.

Code: Select all

    ob_start();  // start the output buffer here.

    define('IN_PHPBB', true);
    $phpbb_root_path = './forum/'; // Path to phpbb folder
    $phpEx = substr(strrchr(__FILE__, '.'), 1);
    include($phpbb_root_path . 'common.' . $phpEx);
    include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
    
    // Start session management
    $user->session_begin();
    $auth->acl($user->data);

    //..... the rest of the script.

    ob_end_flush();  // turn off output buffering & send the output.
You might have to use it at the beginning & end of your page. I haven't tried this but it should remove the errors.
juxair
Registered User
Posts: 6
Joined: Thu Aug 07, 2008 7:16 pm

Re: Latest Posts on site home page

Post by juxair »

Vinyard_X43q wrote:
I can't reproduce the error all of you seem to be getting I guess my server always buffers the output. You can try to buffer the output by using ob_start at the beginning of the script and turn off output buffering when the script is done with ob_end_flush.
Hello Vinyard

i tried the code on a test.php and all worked fine !
the mess started when i added the code to the index.php ( mydomain.com)

gonna set your last code and see what s gonna happen..

ty :)
juxair
mitchristina
Registered User
Posts: 31
Joined: Wed Jun 25, 2008 11:33 pm

Re: Latest Posts on site home page

Post by mitchristina »

When I use the code I get the following error:

Fatal error: Call to a member function on a non-object in /homepages/29/d244682546/htdocs/ExDelicti/topic.php on line 9

Any ideas what is causing it to complain about the call to a member function???
mitchristina
Registered User
Posts: 31
Joined: Wed Jun 25, 2008 11:33 pm

Re: Latest Posts on site home page

Post by mitchristina »

When I use the below code is get a Call to undefined function: request_var()

Anyone know why it is complaining about the request_var function???

Code: Select all

<?php

define('IN_PHPBB', true);
$phpbb_root_path = './phpbb3/';
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);


$topic_limit = request_var('topic_limit', 5);
$forums = array_unique(array_keys($auth->acl_getf('f_read', true))); //ignored in sql statement to include all topics despite permissions


// Select the last topics ignoring permissions
// Vybrat posledn tmata ke kterm mme oprvnen
$sql = 'SELECT p.post_id, p.topic_id, p.forum_id, p.post_subject, p.post_time, u.username
FROM ' . POSTS_TABLE . ' p , ' . USERS_TABLE . ' u
WHERE post_approved = 1
AND u.user_id = p.poster_id
ORDER BY post_time DESC
LIMIT 0,' . $topic_limit;
$result = $db->sql_query($sql);

// Proper header since output not buffered
// Poslat hlavicky pro sprvn kdovn, protoe vpis obsahu je postupn
// header('Content-Type: text/html; charset=utf-8');

// Now let's output the content
// A ted vypsat obsah
 echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Last Topics</title></head><body><div id="post_content"><strong>Last Topics:</strong><br/><ul>';
while ($row = $db->sql_fetchrow($result))
{
$url = generate_board_url() . "/viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}"; //added fedforum to url

//old line $url = generate_board_url() . "viewtopic.{$phpEx}?f={$row['forum_id']}&t={$row['topic_id']}&p={$row['post_id']}#p{$row['post_id']}";
echo '<tr><td colspan=\"2\"><a target="_blank" href="' . $url . '">' . $row['post_subject'] . '</a>, ' . ucwords($row['username']) . ', ' . date("d/m Hi",$row['post_time']).'hrs</td></tr>';
}
echo '</div></body></html>';
?>
Last edited by stickerboy on Mon Aug 18, 2008 7:34 pm, edited 1 time in total.
Reason: Use code tags in future plzkthxbai
Bluesplayer
Registered User
Posts: 8
Joined: Sun Nov 18, 2007 12:02 pm

Re: Latest Posts on site home page

Post by Bluesplayer »

Hi

What code needs to be added so that the forum name where the post was made shows as well?

Regards
Bluesplayer
Vinyard_X43q
Registered User
Posts: 19
Joined: Wed Jun 06, 2007 8:14 am

Re: Latest Posts on site home page

Post by Vinyard_X43q »

Bluesplayer wrote:What code needs to be added so that the forum name where the post was made shows as well?
Change this:

Code: Select all

    $sql="SELECT *
    FROM " . TOPICS_TABLE . "
    WHERE topic_approved = '1' AND " . $db->sql_in_set('forum_id', $forums) . "
    ORDER BY topic_last_post_time DESC";
to this:

Code: Select all

    $sql="SELECT f.forum_name, t.forum_id, t.topic_id, t.topic_title, t.topic_last_post_time, t.topic_replies, t.topic_last_post_id
    FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t
    WHERE topic_approved = 1 AND " . $db->sql_in_set('t.forum_id', $forums) . " AND f.forum_id = t.forum_id
    ORDER BY t.topic_last_post_time DESC";
After that change you can use the code below in the foreach loop to print out the forum name.

Code: Select all

<?=$t['forum_name'];?>
cold-machine
Registered User
Posts: 27
Joined: Tue Apr 15, 2008 7:36 pm

Re: Latest Posts on site home page

Post by cold-machine »

now i know i would need to create a new phpfile for this .. now using the template system for php for my site. I wanna incorperate this.. now i can incorperate the header and what not using <!-- INCLUDE overall_header.html -->

Now how would i incorperate this?

would i on say my page_body.html file goto the location i want and use something like <? include(lastposts.php); ?>
Bluesplayer
Registered User
Posts: 8
Joined: Sun Nov 18, 2007 12:02 pm

Re: Latest Posts on site home page

Post by Bluesplayer »

cold-machine wrote:now i know i would need to create a new phpfile for this .. now using the template system for php for my site. I wanna incorperate this.. now i can incorperate the header and what not using <!-- INCLUDE overall_header.html -->

Now how would i incorperate this?

would i on say my page_body.html file goto the location i want and use something like <? include(lastposts.php); ?>
I have made the code work on the front page of my forum. I did this by creating a file with an extension of html which I uploaded to the theme template directory. I also used ajaxinclude to incorporate the above code in the file. I don't think you can get it to work without a fair bit of editing otherwise. The ajax include code can be found here:

http://dynamicdrive.com/dynamicindex17/ajaxincludes.htm

You only have to include the ajaxinclude.js in the overall_header file and use the javascript where you want the file to appear. As long as you get the path right in the lastest posts code (I left mine empty and uploaded it to the mainforum root directory) it will work.

With the file I made I added a table and 3 columns. Column 1 and 2 list my forums and the 3rd has the above last posts code (using ajax). I have also wrapped the code in a div with an overflow auto command added so that any long titles will cause the scroll bar to appear, otherwise it will mess up my forum. You can see the result here:

http://bluesplayer.co.uk/mainforum

Sounds a bit long winded using ajax but I find it gets stuff to work quite easily whereas it was a heck of a struggle otherwise. I have also added a sliding side menu to my forum using ajax. Made the job of doing so very easy (click on help panel at the top of the forum to see it).

Regards
Bluesplayer
PS: Going to attempt to get the forum title showing in the latest posts as the author has updated the code for me.
Bluesplayer
Registered User
Posts: 8
Joined: Sun Nov 18, 2007 12:02 pm

Re: Latest Posts on site home page

Post by Bluesplayer »

$sql="SELECT f.forum_name, t.forum_id, t.topic_id, t.topic_title, t.topic_last_post_time, t.topic_replies, t.topic_last_post_id
FROM " . FORUMS_TABLE . " f, " . TOPICS_TABLE . " t
WHERE topic_approved = 1 AND " . $db->sql_in_set('t.forum_id', $forums) . " AND f.forum_id = t.forum_id
ORDER BY t.topic_last_post_time DESC";
That works a treat - cheers. Only trouble is I was using the code that showed the Name and Date. I will lose them now if I use the original code. So my next request is can Name and Date be added. The code will end up showing: Forum Title - Topic Title - Name of poster & Date.

Regards
Bluesplayer
Pond Life
Registered User
Posts: 388
Joined: Sat Jan 20, 2007 1:55 am

Re: Latest Posts on site home page

Post by Pond Life »

Many thanks to Vinyard and BlueSalamander. :D

I had the
[phpBB Debug] PHP Notice: in file /includes/session.php on line ...: Cannot modify header information -
error too until I moved

Code: Select all

<?php
define('IN_PHPBB', true);
$phpbb_root_path = 'Forums/'; // Path to phpbb folder
$phpEx = substr(strrchr(__FILE__, '.'), 1);
include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
// Start session management
$user->session_begin();
$auth->acl($user->data);
// Grab user preferences
$user->setup();
?>
to the top of the page.

It's working perfectly now. A very useful bit of code. Cheers. 8-)
Never argue with idiots, they will drag you down to their level and beat you with experience.
juxair
Registered User
Posts: 6
Joined: Thu Aug 07, 2008 7:16 pm

Re: Latest Posts on site home page

Post by juxair »

hello again

this is the blank code on my server WITH the session.php change 9 august version

http://www.hothairyone.com/newtopics.php

works like a charm

without the session.php change i keep getting the /includes/session.php error

any suggestions to get this code working without a session.php change ?

ty

regards
juxair

Return to “[3.0.x] MOD Requests”