当前时间: 2010年07月29日, 21:23 你好, 欢迎光临! (登录注册)


发表回复 
 
主题评价:
  • 0 次(票) - 平均星级: 0
  • 1
  • 2
  • 3
  • 4
  • 5
在网站首页调用MyBB论坛的帖子 [教程] [待翻译]
2009年05月28日, 00:12
在网站首页调用MyBB论坛的帖子 [教程] [待翻译]
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. 眨眼 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:

PHP 代码:
require_once('MyBBLatest.class.php'); 

Now you have to initialize class:

PHP 代码:
$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:

PHP 代码:
echo $mybb->threads();
echo 
$mybb->posts(); 

You can add some HTML if you want:

PHP 代码:
<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:

PHP 代码:
<h3>Latest threads from X</h3>
echo 
$mybb->threads(5true2); 

In the same way you can do with posts:

PHP 代码:
<h3>Latest posts from forum X</h3>
echo 
$mybb->posts(52); 

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. 微笑 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. 微笑
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. 微笑 I am looking forward to your feedback and suggestions. If you need some help with this just ask.

Class content
PHP 代码:
<?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 <marinespl@gmail.com>
* @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
?>
查找这个用户的全部帖子
引用并回复
2010年05月13日, 09:18
RE: 在网站首页调用MyBB论坛的帖子 [教程] [待翻译]
谢谢admin淘到这么性感的class
在wordpress上运用成功
查找这个用户的全部帖子
引用并回复
2010年05月13日, 15:29
RE: 在网站首页调用MyBB论坛的帖子 [教程] [待翻译]
呵呵,看着这时间,我就觉得可悲~
刚淘到,还没来得及翻译,就与世隔绝了
查找这个用户的全部帖子
引用并回复
发表回复 


论坛跳转:


联系我们 | MyBB中文站 | 回到顶部 | 回到正文区 | 精简(归档)模式 | RSS 聚合