MyBB Community Forums

Full Version: MYBB News Integration For Website?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Definitely know questions on this topic have been asked haha! However,

I'm trying to use this php code which can show latest posts on your website. Only I'm trying to use it as a news system. 


<?php
chdir("forums"); // path to MyBB
define("IN_MYBB", 1);
require("./global.php");
global $mybb, $db, $cache, $lang, $plugins;  // Trim this list of global vars, if you want to.
?>

<?php
      $query = $db->query("
            SELECT t.*, u.username
            FROM mybb_threads t
            LEFT JOIN mybb_users u ON (u.uid=t.uid)
            WHERE 1=1 AND t.visible='1' AND t.closed NOT LIKE 'moved|%'
            ORDER BY t.lastpost DESC
            LIMIT 0, 6" // Change the last digit to how many recent post you want to be shown
        );

    $list = '';
    while($fetch = $db->fetch_array($query))
    {
        $list .= "<strong><a href=\"forums/showthread.php?tid={$fetch['tid']}\">".htmlspecialchars_uni($fetch['subject'])."</a></strong><br />";
        $poster = "<a href=\"forums/member.php?action=profile&uid=".$fetch['uid']."\">{$fetch['username']}</a>";
        $list .= "Created by: {$poster}<br />";
        $list .= "<i>" .$fetch['replies']. " Replies</i>";
        $list .= "<i> , " .$fetch['views']. " Views</i><br />";
        $list .= " (<i>Last post by: " .$fetch['lastposter']. "</i>)<br /><hr width=\"50\"><br />";
    
    }
    //output
    echo $list;
?>

So like I said, I'd like to use this as a news system. First am I suppose to edit the ['tid'] to make the link actually work? Because I'm getting a URL does not exist.Secondly, I'd like to show the content of the actual thread. Third I just would like to show the content from one category so do I need to add another code in the php to do this. Also is there a better code to use for what I want to get done?
replies here can be helpful
Thanks for the help, figured out what I needed to know. Thanks to others who posted most of this all I had to do was play around with it.

Incase anyone is interested:

Obviously I put this php code before any output on my file.

$forumpath = 'http://forums.yourdomain.com/' (no www. I think most people have a sub folder for their forums I personally use a sub-domain so my url is forums.yourdomain.com/ yours may be yourdomain.com/forums/)
chdir('/your absolute path/forums')
require('/your absolute path/forums/global.php')
next line down after MYBB_ROOT
chdir('/your absolute path/your domain/index.php (whatever file your news will be posted on I always use an index.php)
<?php

    $fid = 2;
    $limit = 5;
    $forumpath = 'http://forums.yourdomain.com/';

    chdir('/your absolute path/forums');
    define("IN_MYBB", 1);
    require('/your absolute path/forums/global.php');
    require_once MYBB_ROOT."inc/class_parser.php";
    require_once MYBB_ROOT."inc/functions_post.php";
    require_once MYBB_ROOT."inc/functions_user.php";
    $parser = new postParser;
    chdir('/your absolute path/your domain/index.php');
?>
So like I said above I have a sub-domain so when i type in my absolute path, I have to remember not to type in my domain name because both forums (subdomain), and my domain are in the same parent directory in my file manager. So I hope there's not much confusion. However, for the second chdir you include the absolute path which includes your domain then the file your news is posted on.


Then you just stick this bad boy within the body of your html wherever you want your news to appear in your website 
<?php  
    $query = $db->simple_select('threads', '*', "fid='{$fid}' ORDER BY tid DESC LIMIT {$limit}");
    if($db->num_rows($query) > 0)
    {
        while($row = $db->fetch_array($query))
        {
            $query2 = $db->simple_select('posts', '*', "pid='{$row['firstpost']}'");
            $row2 = $db->fetch_array($query2);
            
            $date = my_date($mybb->settings['dateformat'], $row2['dateline'], "", 1);
            $time = my_date($mybb->settings['timeformat'], $row2['dateline'], "", 1);

            $options = array(
                        "allow_html" => 1,
            "allow_mycode" => 1,
            "allow_smilies" => 1,
            "allow_imgcode" => 1,
            "allow_videocode" => 1,
            "filter_badwords" => 1
                            );
            $message = $parser->parse_message($row2['message'], $options);
            
            echo("<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['subject']}</a> - 
                Posted: {$date} {$time} by <a href=\"{$forumpath}member.php?action=profile&uid={$row2['uid']}\">{$row2['username']}</a><br />");
            echo("{$message}<br /><br />");
            echo("Replies (<a href=\"{$forumpath}showthread.php?tid={$row['tid']}\">{$row['replies']}</a>)<br />");
        }
    }
    else
    {
        echo 'Nothing to display.';
    }

?>

I do wonder if by putting absolute paths, does it expose my website's privacy a little? 

The only thing I wanna play with next is the inclusion of paginations. This code still does the trick after all these years man kudos!
Thank you for this code. I am still struggling with the integration. I am trying to add the latest posts and/or threads to my magento main page.

If I put the two code snippets into one file and call it via magento, I get an error:

Fatal error: require(): Failed opening required '/forum/global.php'


If I try to access global.php, I get: Direct initialization of this file is not allowed.
Please make sure IN_MYBB is defined.


Could it be it's because the code sample is in one file? Magento is a bit special as the index file cannot be used to add the first code snippet. Any thought or ideas are much appreciated.
Why don't you get te latest posts and/or threads directly from mysql?!? It's easier.

I'm grab the last 5 post from specfic thread with thread prefix:

SELECT c.tid as ctid, e.pid as epid, b.fid as filid, e.displaystyle, c.dateline, b.dateline as bdate, c.subject, c.prefix, c.tid, b.pid, b.tid as btid, b.username, b.edittime FROM mybb_posts as b LEFT JOIN mybb_threads as c ON c.tid=b.tid LEFT JOIN mybb_threadprefixes as e ON e.pid=c.prefix LEFT JOIN mybb_forums as d ON b.tid=d.fid WHERE b.fid='2' ORDER BY b.edittime DESC LIMIT 0, 5

Where b.fid='2' is the Forum id

[Image: Capture.jpg]

And at my external site i get (with litle css and php code):

[Image: zzz.png]
I'm looking to potentially do something similar collect and show various data via mysql for a website frontpage so if anyone has a example set of files they can share so I can go through and customize to my needs that would be great.

Found something in this forum I am modifying now as a test.
$last_post_q=mysqli_query($mybbconnect,"SELECT c.tid as ctid, e.pid as epid, b.fid as filid, e.displaystyle, c.dateline, b.dateline as bdate, c.subject, c.prefix, c.tid, b.pid, b.tid as btid, b.username, b.edittime FROM mybb_posts as b LEFT JOIN mybb_threads as c ON c.tid=b.tid LEFT JOIN mybb_threadprefixes as e ON e.pid=c.prefix LEFT JOIN mybb_forums as d ON b.tid=d.fid WHERE b.fid='$movieprogresid' ORDER BY b.edittime DESC LIMIT 0, 5 ");
  while ($mpr = $last_post_q->fetch_assoc()) {
  $mtitle=$mpr['subject'];
  $progres=$mpr['displaystyle'];
  $curper=substr($mpr['displaystyle'],0,-1);
  $prevodach=$mpr['username'];
  $pid=$mpr['pid'];
  $ctid=$mpr['ctid'];

 echo '<div class="progress">
 <div class="progress-bar progress-bar-striped progress-bar-custom" role="progressbar" style="width: '.$progres.'" aria-valuenow="'.$curper.'" aria-valuemin="0" aria-valuemax="100"></div>
    <span class="text-left"><a href="'.$forumdir.'/showthread.php?tid='.$ctid.'">'.$progres.' - '.$mtitle.'</span><span class="text-right">'.$prevodach.'</a></span>
  </div>';

  }

Here you a example
(2018-04-13, 06:19 PM)jjd Wrote: [ -> ]
$last_post_q=mysqli_query($mybbconnect,"SELECT c.tid as ctid, e.pid as epid, b.fid as filid, e.displaystyle, c.dateline, b.dateline as bdate, c.subject, c.prefix, c.tid, b.pid, b.tid as btid, b.username, b.edittime FROM mybb_posts as b LEFT JOIN mybb_threads as c ON c.tid=b.tid LEFT JOIN mybb_threadprefixes as e ON e.pid=c.prefix LEFT JOIN mybb_forums as d ON b.tid=d.fid WHERE b.fid='$movieprogresid' ORDER BY b.edittime DESC LIMIT 0, 5 ");
  while ($mpr = $last_post_q->fetch_assoc()) {
  $mtitle=$mpr['subject'];
  $progres=$mpr['displaystyle'];
  $curper=substr($mpr['displaystyle'],0,-1);
  $prevodach=$mpr['username'];
  $pid=$mpr['pid'];
  $ctid=$mpr['ctid'];

 echo '<div class="progress">
 <div class="progress-bar progress-bar-striped progress-bar-custom" role="progressbar" style="width: '.$progres.'" aria-valuenow="'.$curper.'" aria-valuemin="0" aria-valuemax="100"></div>
    <span class="text-left"><a href="'.$forumdir.'/showthread.php?tid='.$ctid.'">'.$progres.' - '.$mtitle.'</span><span class="text-right">'.$prevodach.'</a></span>
  </div>';

  }

Here you a example
Sorry for the reply how do I define this and the variables as I am getting error 500?

I'm not a php coder but I can lightly modify stuff.
$mybbconnect  = your mysql parameters.
Like:
file mysqlconnection.php
with parameters in it for your forum DB:
<?php
$mybbconnect=mysqli_connect('YOUR HOST HERE','MYSQL USER HERE','YOUR PASSWORD HERE','YOUR DB NAME HERE')or die(mysqli_error());
$mybbconnect->set_charset("utf8");
?>

In your (let say index.php) at the beginning put:
include_once('mysqlconnection.php');

And that is.

You must change and the:

b.fid='2'

to meet your forums and post number!!!

If you want 10 news, change 5 with 10 here:

LIMIT 0, 5
(2018-04-22, 12:46 PM)jjd Wrote: [ -> ]$mybbconnect  = your mysql parameters.
Like:
file mysqlconnection.php
with parameters in it for your forum DB:
<?php
$mybbconnect=mysqli_connect('YOUR HOST HERE','MYSQL USER HERE','YOUR PASSWORD HERE','YOUR DB NAME HERE')or die(mysqli_error());
$mybbconnect->set_charset("utf8");
?>

In your (let say index.php) at the beginning put:
include_once('mysqlconnection.php');

And that is.

You must change and the:

b.fid='2'

to meet your forums and post number!!!

If you want 10 news, change 5 with 10 here:

LIMIT 0, 5

Ok I got it working somewhat Link to test
I'll link a updated code below as I changed a couple light things for formatting.

When you refresh the page the post numbers rotate  as its showing for me order 3,2,4,6,1 / 3,1,5,7,2 when I refresh so your code isn't working right and needs modifying to be descending from latest as per the forum listing with recent on top to oldest on bottom.
Here is a link to the development forum I am grabbing the threads from its ordered 7-1 descending order with Test 7 being the latest.

This is the slightly modified code complete PHP file:


<?php
$mybbconnect=mysqli_connect('YOUR HOST HERE','MYSQL USER HERE','YOUR PASSWORD HERE','YOUR DB NAME HERE')or die(mysqli_error());
$mybbconnect->set_charset("utf8");
$movieprogresid="3";
$forumdir='./forum'
?>

<?php
$last_post_q=mysqli_query($mybbconnect,"SELECT c.tid as ctid, e.pid as epid, b.fid as filid, e.displaystyle, c.dateline, b.dateline as bdate, c.subject, c.prefix, c.tid, b.pid, b.tid as btid, b.username, b.edittime FROM mybb_posts as b LEFT JOIN mybb_threads as c ON c.tid=b.tid LEFT JOIN mybb_threadprefixes as e ON e.pid=c.prefix LEFT JOIN mybb_forums as d ON b.tid=d.fid WHERE b.fid='$movieprogresid' ORDER BY b.edittime DESC LIMIT 0, 5 ");
  while ($mpr = $last_post_q->fetch_assoc()) {
  $mtitle=$mpr['subject'];
  $progres=$mpr['displaystyle'];
  $curper=substr($mpr['displaystyle'],0,-1);
  $prevodach=$mpr['username'];
  $pid=$mpr['pid'];
  $ctid=$mpr['ctid'];

 echo '<div class="progress">
 <div class="progress-bar progress-bar-striped progress-bar-custom" role="progressbar" style="width: '.$progres.'" aria-valuenow="'.$curper.'" aria-valuemin="0" aria-valuemax="100"></div>
    <span class="text-left"><a href="'.$forumdir.'/showthread.php?tid='.$ctid.'">'.$progres.' - '.$mtitle.'</span> - <span class="text-right">'.$prevodach.'</a></span>
  </div>';

  } 
?>

I can design this better to allow multiple boxes on a page with the recent 10 or so topic listings for various forums etc but the ordering of topics need fixing first.
Pages: 1 2