Subject: UNB as a CMS
While setting up a new site I thought it might be a good idea to have front page updates controlled by UNB. Now I can make forum posts, and all the content appears on the front page of the site, as if by magic! It means I now have one system for forum + CMS, instead of two.
If you'd like to pull posts from UNB and put them on your site, here's a guide to doing it. The following information does four things:
This function does both jobs:
(I did not write either function)
Now, it's probably not nearly as nice as Yves' code, but it works. Putting all the HTML code in the PHP echo blocks is cumbersome and ugly, but... Well, I suck. It's all I can do. By using CSS you can change the appearance of the posts depending on where they are in the page.
Finally, the page itself. This is a very short example. Your page design should look like this:
You can use the GETPOSTS function anywhere you want to display your posts
Finally, here's an example of the HTML & PHP used to display the post(s). It pulls 12 posts from forum 31, with no content:
Some extra thoughts:
If you'd like to pull posts from UNB and put them on your site, here's a guide to doing it. The following information does four things:
- Set up database access
- Replaces BBCode with HTML
- cuts the post after a set length so long posts don't overflow
- searches for posts in the UNB database and displays them on your website
Database Access
- // MySQL config
- $dbname = "database name";
- $dbuser = "database user";
- $dbpass = "database password";
- $dbhost = "server address (usually localhost)";
Post Data Fixing
This function does both jobs:
- cuts the post length
- replaces the UBCode
(I did not write either function)
- /* First, truncate the string. */
- /* Accepts 2 criteria: the string itself and the character limit */
- function myTruncate($string, $limit, $break="\n")
- {
- // return with no change if string is shorter than $limit
- // is $break present between $limit and the end of the string?
- }
- }
- /* Next, replace BBCode with HTML */
- "[img]", "[/img]",
- "[b]", "[/b]",
- "[u]", "[/u]",
- "[i]", "[/i]",
- '[color="', "[/color]",
- '[url=', "[/url]",
- "[quote]", "[/quote]",
- ']');
- "<img src=\"", "\">",
- "<b>", "</b>",
- "<u>", "</u>",
- "<i>", "</i>",
- "<span style=\"color:", "</span>",
- '<a href="', "</a>",
- "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
- '">');
- return $newtext;
- }
Post Display Loop
- /* This function accepts a forum number, number of posts to grab, and post contents or not */
- /* If you want only post subject, $content should be 0 */
- /* Note that this is hard-wired for user #1. You can change this to whatever your user number is) */
- function getposts($forum, $postcount, $content) {
- $sql = "select ID, Forum from unb_threads where (User=1) and (Forum=$forum) order by Date Desc limit $postcount";
- /* $firstpost = true means that it will not grab REPLIES, only the FIRST POST */
- $firstpost = true;
- // -------------------------------------------------------------------------------------- BLOCK START
- $id = $row['ID'];
- // -------------------------------------------------------------------------------------- DISPLAY LOOP
- $sql = "Select Subject, Msg from unb_posts where (Thread=$id) and (User=1) and (ReplyTo=0) order by Date Desc";
- // -------------------------------------------------------------------------------------- DISPLAY TITLE
- echo "<div style=\"margin: 5px 0 0 0;\">";
- echo "<div class=\"ctitle\"><b><a href=\"http://nfgworld.com/mb/thread/$id\">".$post['Subject']."</a></b></div>";
- // echo "";
- if($content) {
- // -------------------------------------------------------------------------------------- DISPLAY CONTENT
- echo "<div class=\"content\">";
- $body = myTruncate($post['Msg'], 75);
- echo $body;
- echo "</div>";
- }
- // -------------------------------------------------------------------------------------- DISPLAY CONTENT END
- echo "</div>";
- // -------------------------------------------------------------------------------------- DISPLAY TITLE END
- }
- // echo "";
- }
- // -------------------------------------------------------------------------------------- BLOCK END
Now, it's probably not nearly as nice as Yves' code, but it works. Putting all the HTML code in the PHP echo blocks is cumbersome and ugly, but... Well, I suck. It's all I can do. By using CSS you can change the appearance of the posts depending on where they are in the page.
The HTML Page 1
Finally, the page itself. This is a very short example. Your page design should look like this:
- HTML START
- PHP Functions (listed above)
- Connect to the DB (below)
- Some HTML/CSS/Whatever.
- HTML END
You can use the GETPOSTS function anywhere you want to display your posts
- // First, connect to the DB. This should be one of the first things in your page:
- <?
- ?>
The HTML Page 2
Finally, here's an example of the HTML & PHP used to display the post(s). It pulls 12 posts from forum 31, with no content:
<div class="news">
<?
echo getposts(31, 12, 0);
?>
</div>
Some extra thoughts:
- This code is as secure as any other PHP file on your server. Normally people won't be able to view your passwords and such if they're in a PHP file, but if your server's insecure this file is going to give away your passwords.
- This code does not use any UNB facilities, it's a raw assault on the database. Since it's read-only there's no chance of messing up your data, but it doesn't obey UNB's user access rules or anything, so it's easy to rip out private posts etc.
- I don't write good code. This is all probably dangerous and will cause your server to catch fire. Don't say I didn't warn you!


NFG
Show profile
Link to this post