MyBB Community Forums

Full Version: Latest threads/posts on your site
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7
I have written a PHP class which displays last X posts or threads. It will work independent from place which it is ececuted from (it must be placed at least in the same server as MyBB instalation is). Access to "inc/config.php" is neccessary (database login data are got from it). The most convinient is to save class as eg. "MyBBLatest.class.php" and include it in your site. Class will not display anything in case of database errors, so user will not see any unexpected errors. Feel free to use it. I thought GNU GPLv2 license would fit, but don't mind if you have not got bad intentions. Wink Bellow you can find manual and some usage examples.

Manual

__constructor(string $mybb, string $url)
It executes while class is initialized.

First argument contains relative path to main directory of MyBB instalation. For example if your site is placed in root directory and forum is installed in "board" directory just enter "board" here. If page in which you want to display posts/threads is placed in the same directory as board you do not have to enter anything. Just remember about trailing slash!
Default: NULL

Second argument contains absoute board URL (eg. http://www.siteaddress.com/board/). It is the only argument which is neccessary. Just remember about trailing slash!

threads(integer $many, boolean $lastpost, integer $fid)
Displays sorted by date threads list.

First argument says how many threads has to be displayed.
Default: 10

Second argument decides if thread link has to direct to last or first post.
Default: false

Third argument says from which forum ID threads have to be displayed. If NULL it gets threads from whole board.
Default: false

posts(integer $many, integer $fid)

First argument says how many posts has to be displayed.
Default: 10

Second argument says from which forum ID posts have to be displayed. If NULL it gets posts from whole board.
Default: false

Usage examples
I assume that you have saved the class in "MyBBLatest.class.php" in the same folder in which is placed the file in which you want to use it. So let's include the class:

require_once('MyBBLatest.class.php');

Now you have to initialize class:

$mybb = new MyBBLatest('board', 'http://www.com/board/');

Now you are ready to display latest threads and posts. The most simple way is to make list of 10 latest threads and posts from whole board:

echo $mybb->threads();
echo $mybb->posts();

You can add some HTML if you want:

<h3>Latest threads</h3>
echo $mybb->threads();
<h3>Latest posts</h3>
echo $mybb->posts();

You can display latest 5 threads from forum of ID 2 which direct to last post:

<h3>Latest threads from X</h3>
echo $mybb->threads(5, true, 2);

In the same way you can do with posts:

<h3>Latest posts from forum X</h3>
echo $mybb->posts(5, 2);

FAQ
Q: With which version of MyBB class works?
A: I've tested it with 1.4.
Q: Why class doesn't allow user to choose look of list easily?
A: Because I made this for really PHP beginners. It is very simple to use and that had to be. Smile It is first release and I'm sure I'd include formating in the future.
Q: Why don't you keep properly order of arguments in contructor?
A: Because I'm sure that almost everybody will use first argument. Smile
Q: Why don't you use MySQLi as database driver?
A: Because it is much slower than standard MySQL. I've tested it! It takes about 1.35x more time.

And that's it. Smile I am looking forward to your feedback and suggestions. If you need some help with this just ask.

Class content
<?php
/**
* This class can display latest threads or posts
* formated in unordered list (<ul/>)
* It doesn't depend on place where script is being run
* just initialize class and execute proper method
* 
* @author Mariusz "marines" Kujawski <[email protected]>
* @link http://marines.jogger.pl/
* @version 0.1
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/
class MyBBLatest {
   // mysql db handler
   private $db;
   // tables prefix
   private $prefix;
   // url to mybb
   private $url;
   /**
   * constructor
   *
   * @param string $mybb path to MyBB instalation
   * @param string $url URL of MyBB instalation
   * @return boolean
   */
   public function __construct($mybb = '', $url) {
      // include mybb config file
      @include('./' . $mybb . 'inc/config.php');
      // db connect
      $this->db = @mysql_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password']);
      // db choose
      @mysql_select_db($config['database']['database'], $this->db);
      // stop executing if db connection isn't availible
      if (!$this->db) return false;
      // set db prefix
      $this->prefix = $config['database']['table_prefix'];
      // set base url of mybb
      $this->url = $url;
      // return
      return true;
   }
   /**
   * display latest threads
   *
   * @param integer $many indicates how many threads have to be retrieved from database
   * @param boolean $lastpost indicates whether link has to direct to last post in thread
   * @param integer $fid ID of forum which threads have to be retrieved from
   * @return string list of threads
   */
   public function threads($many = 10, $lastpost = false, $fid = false) {
      // forum id select
      if ($fid) {
         $where = 'WHERE `fid` = ' . $fid;
      }
      if ($lastpost) {
         $last = '&action=lastpost';
      }
      // initialize temporary var
      $tmp = '<ul class="last-threads">';
      // select data
      $query = @mysql_query('SELECT `tid`, `subject` FROM `' . $this->prefix . 'threads` ' . $where . ' ORDER BY `dateline` DESC LIMIT ' . $many);
      // check if query has result
      if (!$query) return false;
      // collect data into list
      while ($row = mysql_fetch_array($query)) {
         $tmp .= '<li><a href="' . $this->url . 'showthread.php?tid=' . $row[0] . $last . '">' . $row[1] . '</a></li>';
      }
      $tmp .= '</ul>';
      // return list of threads
      return $tmp;
   }
   /**
   * display latest posts
   *
   * @param integer $many indicates how many posts have to be retrieved from database
   * @param integer $fid ID of forum which posts have to be retrieved from
   * @return string list of posts
   */
   public function posts($many = 10, $fid = false) {
      // forum id select
      if ($fid) {
         $where = 'WHERE `fid` = ' . $fid;
      }
      // initialize temporary array
      $tmp = '<ul class="last-threads">';
      // select db data
      $query = @mysql_query('SELECT `pid`, `tid`, `subject` FROM `' . $this->prefix . 'posts` ' . $where . ' ORDER BY `dateline` DESC LIMIT ' . $many);
      // check if query has result
      if (!$query) return false;
      // collect data into list
      while ($row = mysql_fetch_array($query)) {
         $tmp .= '<li><a href="' . $this->url . 'showthread.php?tid=' . $row[1] . '&pid=' . $row[0] . '#pid' . $row[0] . '">' . $row[2] . '</a></li>';
      }
      $tmp .= '</ul>';
      // return posts
      return $tmp;
   }
} // MyBBLatest end
?>
You should probably note that this was written for PHP 5+.
PHP4 hasn't got any support now. Wink
Woah, learnt something interesting =]

-favourated- this thread.
excellent sharing

keep it up
hmm... i wonder what functionality can i add to it Smile
I'm getting
Quote:Parse error: syntax error, unexpected '{' in /home/hu/public_html/MyBBLatest.class.php on line 13
show me your code. the most probably you've copied code wrongly.
How do I set the path to my config file properly?

I'm using the class on my WordPress site. MyBBLatest.class.php is located at /home/hu/public_html/MyBBLatest.class.php. The actual MyBB installation is at /home/homeundo/public_html/

Is that all I have to do? Set the path, and then I can call the class using:

<?php require_once('/home/hu/public_html/MyBBLatest.class.php'); 
$mybb = new MyBBLatest('board', 'http://homeundone.com/'); 

echo $mybb->threads();
echo $mybb->posts(); 
?>
try without 'board' Smile
$mybb = new MyBBLatest('', 'http://homeundone.com/');
Pages: 1 2 3 4 5 6 7