MyBB Community Forums

Full Version: Get list of thread names
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello.

Sorry for noobish question. I'm just new in MyBB. Smile

I want to get the latest 3 topics from chosen (static) forum and create a list <li> with their names and dates.

So if I have a forum of some ID=2 and 5 topics inside: T1 (created first), T2, T3, T4, T5, I want to get an input like:


<ul>
   <li>T5</li>
   <li>T4</li>
   <li>T3</li>
</ul>


I'm not writing a new plugin. I'm just trying to do something inside existing template...
(2017-08-01, 03:16 PM)Nickon Wrote: [ -> ]I'm not writing a new plugin. I'm just trying to do something inside existing template...

You need a plugin for this.
Oh, too bad. Maybe that's why I can't handle this in template Toungue
If your PHP knowledge is good enough you could try to use PHP In Templates:
http://mybbhacks.zingaburga.com/showthread.php?tid=260

But I'd rather use a plugin that allow PHP directly into my templates.
Ok. I tried to make my first plugin (don't look at bad code, it is only sandbox for proof of concept, I will rewrite it after getting everything to work).

Here is my code:

<?php

// Hooks
$plugins->add_hook("global_end", "lastforumposts");

// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}

function lastforumposts_info()
{
	global $lang;
	$lang->load("lastforumposts");
	return array(
		"name"          => $lang->plugname,
		"description"   => $lang->plugdesc,
		"website"       => "https://mybb.com/",
		"author"        => "Nickon",
		"authorsite"    => "https://mybb.com/",
		"version"       => "1.0.1",
		"guid"          => "lastforumposts",
		"compatibility" => "18*"
	);
}

function lastforumposts_install()
{
	global $db, $lang;
	$lang->load("lastforumposts");
	$new_setting_group = array(
		"name" => "lastforumposts",
		"title" => $lang->settings_name,
		"disporder" => 1,
		"isdefault" => 0,
		"description" => ""
	);

	$gid = $db->insert_query("settinggroups", $new_setting_group);

	$settings[] = array(
		"name" => "lastforumposts_threadcount",
		"title" => $lang->num_posts_to_show,
		"description" => "",
		"optionscode" => "text",
		"disporder" => 1,
		"value" => 5,
		"gid" => $gid
	);

	$settings[] = array(
		"name" => "lastforumposts_forum_include",
		"title" => $lang->forums_to_include,
		"description" => "",
		"optionscode" => "forumselectsingle",
		"disporder" => 2,
		"value" => NULL,
		"gid" => $gid
	);

	$settings[] = array(
		"name" => "lastforumposts_forum_wrapper_prefix",
		"title" => $lang->wrapper_prefix,
		"description" => $lang->wrapper_prefix_desc,
		"optionscode" => "text",
		"disporder" => 3,
		"value" => "",
		"gid" => $gid
	);

	$settings[] = array(
		"name" => "lastforumposts_forum_wrapper_suffix",
		"title" => $lang->wrapper_suffix,
		"description" => $lang->wrapper_suffix_desc,
		"optionscode" => "text",
		"disporder" => 4,
		"value" => "",
		"gid" => $gid
	);

	foreach($settings as $array => $content)
	{
		$db->insert_query("settings", $content);
	}
	rebuild_settings();
}

function lastforumposts_is_installed()
{
	global $db;
	$query = $db->simple_select("settinggroups", "*", "name='lastforumposts'");
	if($db->num_rows($query))
	{
		return TRUE;
	}
	return FALSE;
}

function lastforumposts_activate()
{
	global $db, $lang;
	$lang->load("lastforumposts");

	$insert_array = array(
		'title'		=> 'last_forum_posts',
		'template'	=> $db->escape_string('{$wrapperprefix}{$thread[\'subject\']}{$wrappersufix}'),
		'sid'		=> '-1',
		'version'	=> '',
		'dateline'	=> TIME_NOW
	);
	$db->insert_query("templates", $insert_array);
}

function lastforumposts_deactivate()
{
	global $db;
	$db->delete_query("templates", "title IN('last_forum_posts')");
}

function lastforumposts_uninstall()
{
	global $db;
	$query = $db->simple_select("settinggroups", "gid", "name='lastforumposts'");
	$gid = $db->fetch_field($query, "gid");
	if(!$gid) {
		return;
	}
	$db->delete_query("settinggroups", "name='lastforumposts'");
	$db->delete_query("settings", "gid=$gid");
	rebuild_settings();
}

function lastforumposts()
{
	global $mybb, $lang, $db, $templates, $lastforumposts;
	
	$threadcount = (int) $mybb->settings['lastforumposts_threadcount'];
	$includedforums = (int) $mybb->settings['lastforumposts_forum_include'];
	$wrapperprefix = $mybb->settings['lastforumposts_forum_wrapper_prefix'];
	$wrappersufix = $mybb->settings['lastforumposts_forum_wrapper_sufix'];

	if(!$threadcount || $threadcount <= 0) {
		$threadcount = 5;
	}
	if(!$wrapperprefix) {
		$wrapperprefix = "";
	}
	if(!$wrappersufix) {
		$wrappersufix = "";
	}
	if(!$includedforums) {
		$includedforums = 0;
	}

	$query = $db->query("
        SELECT t.*
        FROM ".TABLE_PREFIX."threads t
        WHERE fid = ".$includedforums."
		ORDER BY t.lastpost DESC
        LIMIT 0,".$threadcount
    );

	$lastforumposts = "";
    while($thread = $db->fetch_array($query))
    {
		$thread['subject'] = htmlspecialchars_uni($thread['subject']);		
		$thread['username'] = htmlspecialchars_uni($thread['username']);
        $thread['threadlink'] = get_thread_link($thread['tid']);

		echo("THREAD SUBJECT: ".$thread['subject'])."<br>";
		echo("USERNAME: ".$thread['username'])."<br>";
		echo("THREAD LINK: ".$thread['threadlink'])."<br>";
		echo("<br><br>");

		eval("\$lastforumposts .= \"".$templates->get("last_forum_posts")."\";");
    }
}

?>

As I said before - I want to read thread's subjects and show them somewhere on my MyBB.

Echoes return everything correctly for me and my template appears in Global templates (I have also MyTheme Templates just under the globals).

On my theme I have some jQuery component that needs a list of <a> elements to show up animated announcements.

$wrapperprefix and $wrappersufix variables are used to write HTML code (in case of my jQuery component - I want them to be <a> and </a>).

After installing plugin I modified other template (in MyTheme Templates) and added {$lastforumposts} inside.


<div class="newsticker">
<div class="newsTicker">
<span class="news-header"><i class="fa fa-microphone"></i> Ogłoszenia</span>
<div class="news-articleWrap">
	{$lastforumposts}
</div>
</div>
</div>

After refreshing the page I can't see anything div.news-articleWrap is empty inside Sad

I don't know if I used a wrong hook or my template is bad. Maybe templates don't work between different "sets" (I mean Global against MyTheme templates)...
You are using global_end. Before this hook, at least 20 templates are eval'd; if you are using one of these templates, including header, footer, headerinclude and many others, you won't see anything because your hook runs after the template definition.

A better hook would be global_intermediate.

You can also hook into global_start and cache your templates by adding them to the $templatelist variable, appending them and separating them by a coma.
Shade, my friend Smile

THANK YOU!!!!!!!!!!!
i have something like this. I wanted links and subject title names in view of the "merge thread template"....so i made a plugin to display them inside of that. 

I put it in the root MyBB 
<?php
define("IN_MYBB", 1);
require_once("./global.php"); // Change this if needed
$tlimit = 100; // How many titles you want

$query = $db->query("SELECT * FROM ".TABLE_PREFIX."threads ORDER BY `tid` DESC LIMIT $tlimit");
while($fetch = $db->fetch_array($query)){
echo '<a href="./showthread.php?tid='.$fetch['tid'].'">'.$fetch['subject'].'</a><br />' . "\n";
}
?>


template moderation_merge
<br><br><br>
	<center><h3>Latest threads</h3></center>

<iframe src="/latest100.php" width="100%" height="400px" >
	<input type="button" value="Back" onclick="history.back()">

  <p>Your browser does not support iframes.</p>
</iframe>