3.2: Show recent topics on site home page (not part of forum)?? Script help!

Need some custom code changes to the phpBB core simple enough that you feel doesn't require an extension? Then post your request here so that community members can provide some assistance.

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

NOTE: NO OFFICIAL SUPPORT IS PROVIDED IN THIS SUB-FORUM
Post Reply
Swanny
Registered User
Posts: 486
Joined: Sun Apr 14, 2002 2:11 am
Location: Canada

3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Swanny »

OK so I did the phpBB 3.1.10 --> 3.2.1 upgrade today. It went pretty slick and I had no problems.

The thing now is that my separate script I used on the website home page no longer pulls the recent topics. Here is the code that was working, and is now broken.

I cannot use an extension because this is outside phpBB just accessing the database. Thanks.

Code: Select all

<?php
        $urlPath = "../forums";
        include '/home/site/public_html/forums/config.php';

        $table_topics = $table_prefix. "topics";
        $table_forums = $table_prefix. "forums";
        $table_posts = $table_prefix. "posts";
        $table_users = $table_prefix. "users";
        $link = mysqli_connect("$dbhost", "$dbuser", "$dbpasswd", "$dbname") or die("Could not connect");
        
        $query = "SELECT t.topic_id, t.topic_title, t.topic_first_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, p.post_text, u.user_id, u.username
        FROM $table_topics t, $table_forums f, $table_posts p, $table_users u
        WHERE t.topic_id = p.topic_id AND
        f.forum_id = t.forum_id AND
		f.forum_id <> '8' AND 
        p.post_id = t.topic_first_post_id AND
        p.poster_id = u.user_id
        ORDER BY p.post_id DESC LIMIT 20";
        $result = mysqli_query($link, $query) or die("Query failed");                           
		$num=mysqli_num_rows($result);
		echo "<h2>Recent <a href='/forums/'>Forum</a> Discussions</h2>";
		if ($num == "0") { echo "<li>There are currently no related topics in our forums. <a href='/forums/'>Post a new forum topic now</a>.</li>"; }
		else {
			$num=0;
        	print "<p>";
        	while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) {
        		$posttext = substr(strip_tags($row["post_text"]), 0, 325);
					echo  "<b><a href=\"https://www.example.com/forums/viewtopic.php?t=$row[topic_id]\">" .
        			$row["topic_title"] . "</a></b><br />" . $posttext . "..." . "<br /><br />";
					$num++;
        	}
		}
        print "</p>";
        mysqli_free_result($result);
        mysqli_close($link);
?>
Last edited by Swanny on Tue Jul 18, 2017 5:49 pm, edited 2 times in total.
User avatar
Ger
Registered User
Posts: 2107
Joined: Wed Jan 02, 2008 7:35 pm
Location: 192.168.1.100
Contact:

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Ger »

Your query is correct. However, you are using the mysqli_* functions wrong. It seems you just added the i to the old mysql_* functions, but to make them work you should add the $link parameter.

More info here: http://php.net/manual/en/mysqli.query.php
My extensions:
Simple CMS, Feed post bot, Avatar Resize, Modbreak, Magic OGP, Live topic update, Modern Quote, Quoted Where (GDPR) and Autoresponder.
Newest: FAQ manager for 3.2

Like my work? Buy me a coffee to keep it coming. :ugeek:

-Don't PM me for support-
Swanny
Registered User
Posts: 486
Joined: Sun Apr 14, 2002 2:11 am
Location: Canada

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Swanny »

Ger wrote: Tue Jul 18, 2017 7:20 am...you are using the mysqli_* functions wrong. It seems you just added the i to the old mysql_* functions, but to make them work you should add the $link parameter.
You're right. I'll look into that, thanks.
Swanny
Registered User
Posts: 486
Joined: Sun Apr 14, 2002 2:11 am
Location: Canada

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Swanny »

Ger wrote: Tue Jul 18, 2017 7:20 am Your query is correct. However, you are using the mysqli_* functions wrong. It seems you just added the i to the old mysql_* functions, but to make them work you should add the $link parameter.

More info here: http://php.net/manual/en/mysqli.query.php
Sorry, I had pasted the wrong code earlier. I updated the code for what is not working at the moment, but will still look at the functions thing. If you see something obvious, let me know. Thanks.

Here's the weird thing though. I had switched to PHP7 and got that script working under 3.1 so that's what's puzzling.
Swanny
Registered User
Posts: 486
Joined: Sun Apr 14, 2002 2:11 am
Location: Canada

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Swanny »

Doh.... I figured it out. Here's the correct working code in case it can help someone else... (I had to change MYSQL_ASSOC to MYSQLI_ASSOC) 8-)

Code: Select all

<?php
        $urlPath = "../forums";
        include '/home/site/public_html/forums/config.php';

        $table_topics = $table_prefix. "topics";
        $table_forums = $table_prefix. "forums";
        $table_posts = $table_prefix. "posts";
        $table_users = $table_prefix. "users";
        $link = mysqli_connect("$dbhost", "$dbuser", "$dbpasswd", "$dbname") or die("Could not connect");
        
        $query = "SELECT t.topic_id, t.topic_title, t.topic_first_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, p.post_text, u.user_id, u.username
        FROM $table_topics t, $table_forums f, $table_posts p, $table_users u
        WHERE t.topic_id = p.topic_id AND
        f.forum_id = t.forum_id AND
		f.forum_id <> '8' AND 
        p.post_id = t.topic_first_post_id AND
        p.poster_id = u.user_id
        ORDER BY p.post_id DESC LIMIT 20";
        $result = mysqli_query($link, $query) or die("Query failed");                           
		$num=mysqli_num_rows($result);
		echo "<h2>Recent <a href='/forums/'>Forum</a> Discussions</h2>";
		if ($num == "0") { echo "<li>There are currently no related topics in our forums. <a href='/forums/'>Post a new forum topic now</a>.</li>"; }
		else {
			$num=0;
        	print "<p>";
        	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        		$posttext = substr(strip_tags($row["post_text"]), 0, 325);
					echo  "<b><a href=\"/forums/viewtopic.php?t=$row[topic_id]\">" .
        			$row["topic_title"] . "</a></b><br />" . $posttext . "..." . "<br /><br />";
					$num++;
        	}
		}
        print "</p>";
        mysqli_free_result($result);
        mysqli_close($link);
?>
Tivland
Registered User
Posts: 9
Joined: Thu May 24, 2018 9:21 am

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Tivland »

Hello
I may be late here but please i need help with a similar issue.
Swanny wrote: Tue Jul 18, 2017 5:48 pm Doh.... I figured it out. Here's the correct working code in case it can help someone else... (I had to change MYSQL_ASSOC to MYSQLI_ASSOC) 8-)

Code: Select all

<?php
        $urlPath = "../forums";
        include '/home/site/public_html/forums/config.php';

        $table_topics = $table_prefix. "topics";
        $table_forums = $table_prefix. "forums";
        $table_posts = $table_prefix. "posts";
        $table_users = $table_prefix. "users";
        $link = mysqli_connect("$dbhost", "$dbuser", "$dbpasswd", "$dbname") or die("Could not connect");
        
        $query = "SELECT t.topic_id, t.topic_title, t.topic_first_post_id, t.forum_id, p.post_id, p.poster_id, p.post_time, p.post_text, u.user_id, u.username
        FROM $table_topics t, $table_forums f, $table_posts p, $table_users u
        WHERE t.topic_id = p.topic_id AND
        f.forum_id = t.forum_id AND
		f.forum_id <> '8' AND 
        p.post_id = t.topic_first_post_id AND
        p.poster_id = u.user_id
        ORDER BY p.post_id DESC LIMIT 20";
        $result = mysqli_query($link, $query) or die("Query failed");                           
		$num=mysqli_num_rows($result);
		echo "<h2>Recent <a href='/forums/'>Forum</a> Discussions</h2>";
		if ($num == "0") { echo "<li>There are currently no related topics in our forums. <a href='/forums/'>Post a new forum topic now</a>.</li>"; }
		else {
			$num=0;
        	print "<p>";
        	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
        		$posttext = substr(strip_tags($row["post_text"]), 0, 325);
					echo  "<b><a href=\"/forums/viewtopic.php?t=$row[topic_id]\">" .
        			$row["topic_title"] . "</a></b><br />" . $posttext . "..." . "<br /><br />";
					$num++;
        	}
		}
        print "</p>";
        mysqli_free_result($result);
        mysqli_close($link);
?>
I need to use this script too as I cannot get the Recent Topics show on my Board 3 portal the way I would want them.

I need recent topics to show boldly on the front page, even if its the last thing that will show.
How do I go about this?

Say I was to use your script above, where will i fix it? Custom block on Board 3 portal? or inside a file somewhere in my cpanel.

Thanks in anticipation
User avatar
GanstaZ
Registered User
Posts: 1187
Joined: Wed Oct 11, 2017 10:29 pm
Location: GZOverse

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by GanstaZ »

Tivland wrote: Thu May 24, 2018 10:30 am I cannot get the Recent Topics show on my Board 3 portal the way I would want them
Why not ask that question in Board3 discussion topic?
Usus est magister optimus! phpBB pre-Triton & latest php environment.
When answer lies in the question, question becomes redundant!
Tivland
Registered User
Posts: 9
Joined: Thu May 24, 2018 9:21 am

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Tivland »

GanstaZ wrote: Thu May 24, 2018 1:12 pm
Tivland wrote: Thu May 24, 2018 10:30 am I cannot get the Recent Topics show on my Board 3 portal the way I would want them
Why not ask that question in Board3 discussion topic?
Ok, thanks
User avatar
Madea
Registered User
Posts: 3
Joined: Sun Jul 08, 2018 7:41 pm

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by Madea »

here's a small fix for you.
just modify it as you need..

Code: Select all

<?php
		$dbhost    = "localhost";
		$dbuser    = "username";
		$dbpasswd  = "password";
		$dbname    = "database";

        $link = mysqli_connect($dbhost, $dbuser, $dbpasswd, $dbname) or die("Could not connect");

		$q = "SELECT post_id,forum_id,post_subject FROM `phpbb_posts` ORDER BY post_id DESC LIMIT 0,25;";
        $result = mysqli_query($link, $q) or die("Query failed");                           
		$num=mysqli_num_rows($result);
		if ($num == "0") { 
		  //no topics
		}
		else {
			$num=0;
        	while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
				echo $row['forum_id'];
				echo $row['post_id'];
				echo $row['post_subject'];
				$num++;
        	}
		}
        mysqli_free_result($result);
        mysqli_close($link);
?>
Give a man a fire, and he will be warm for a day!
Set a man on fire and he will be warm for the rest of his life!
User avatar
richey
Registered User
Posts: 636
Joined: Mon Feb 18, 2002 4:26 pm
Location: now@Cyberspace
Contact:

Re: 3.2: Show recent topics on site home page (not part of forum)?? Script help!

Post by richey »

This is awesome, thanks for sharing!

I've noticed that special characters like German umlauts are displayed incorrectly (large black dots), my website is encoded in UTF-8.
How would I convert the strings to display properly in UTF8 format?

EDIT: found it by myself: utf8_encode() does the trick. :-)

thanks, r.
.
Post Reply

Return to “phpBB Custom Coding”