Not logged in. · Lost password · Register
Forum: Customising UNB Modifications and plug-ins RSS
UNB as a CMS
Avatar
NFG #1
Member since Sep 2006 · 125 posts
Group memberships: Members
Show profile · Link to this post
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:
  • 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

  1. // MySQL config
  2. $dbname = "database name";
  3. $dbuser = "database user";
  4. $dbpass = "database password";
  5. $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)

  1. /* First, truncate the string.  */
  2. /* Accepts 2 criteria: the string itself and the character limit  */
  3. function myTruncate($string, $limit, $break="\n")
  4.  {
  5.     // return with no change if string is shorter than $limit
  6.     if(strlen($string) <= $limit) return $string;
  7.  
  8.     // is $break present between $limit and the end of the string?
  9.     if(false !== ($breakpoint = strpos($string, $break, $limit))) {
  10.       if($breakpoint < strlen($string) - 1) {
  11.         $string = substr($string, 0, $breakpoint);
  12.       }
  13.     }
  14.  
  15. /* Next, replace BBCode with HTML */
  16.  $bbcode = array("<", ">",
  17.                 "[img]", "[/img]",
  18.                 "[b]", "[/b]",
  19.                 "[u]", "[/u]",
  20.                 "[i]", "[/i]",
  21.                 '[color="', "[/color]",
  22.                 '[url=', "[/url]",
  23.                 "[quote]", "[/quote]",
  24.                 ']');
  25.  $htmlcode = array("&lt;", "&gt;",
  26.                 "<img src=\"", "\">",
  27.                 "<b>", "</b>",
  28.                 "<u>", "</u>",
  29.                 "<i>", "</i>",
  30.                 "<span style=\"color:", "</span>",
  31.                 '<a href="', "</a>",
  32.                 "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
  33.                 '">');
  34.  $newtext = str_replace($bbcode, $htmlcode, $string);
  35.  $newtext = nl2br($newtext);//second pass
  36.  return $newtext;
  37. }

Post Display Loop

  1. /* This function accepts a forum number, number of posts to grab, and post contents or not */
  2. /* If you want only post subject, $content should be 0  */
  3. /* Note that this is hard-wired for user #1.  You can change this to whatever your user number is) */
  4. function getposts($forum, $postcount, $content) {
  5.     $sql = "select ID, Forum from unb_threads where (User=1) and (Forum=$forum) order by Date Desc limit $postcount";
  6.     $res = mysql_query($sql);
  7.     echo mysql_error();
  8. /* $firstpost = true means that it will not grab REPLIES, only the FIRST POST */
  9.     $firstpost = true;
  10.     //    -------------------------------------------------------------------------------------- BLOCK START
  11.     while($row = mysql_fetch_assoc($res)) {
  12.             $id = $row['ID'];
  13.     //    -------------------------------------------------------------------------------------- DISPLAY LOOP
  14.             $sql = "Select Subject, Msg from unb_posts where (Thread=$id) and (User=1) and (ReplyTo=0) order by Date Desc";
  15.             $res2 = mysql_query($sql);
  16.             $post = mysql_fetch_assoc($res2);
  17.     //    -------------------------------------------------------------------------------------- DISPLAY TITLE
  18.             echo "<div style=\"margin: 5px 0 0 0;\">";
  19.             echo "<div class=\"ctitle\"><b><a href=\"http://nfgworld.com/mb/thread/$id\">".$post['Subject']."</a></b></div>";
  20.             // echo "";
  21.             if($content) {
  22.     //    -------------------------------------------------------------------------------------- DISPLAY CONTENT
  23.                 echo "<div class=\"content\">";
  24.                 $body = myTruncate($post['Msg'], 75);
  25.                 echo $body;
  26.                 echo "</div>";
  27.             }
  28.     //    -------------------------------------------------------------------------------------- DISPLAY CONTENT END
  29.             echo "</div>";
  30.     //    -------------------------------------------------------------------------------------- DISPLAY TITLE END
  31.     }
  32.     // echo "";
  33. }
  34.     //    -------------------------------------------------------------------------------------- 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

  1. // First, connect to the DB.  This should be one of the first things in your page:
  2. <?
  3. $db = mysql_connect($dbhost, $dbuser, $dbpass);
  4. mysql_select_db($dbname);
  5. ?>

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!
Avatar
Yves (Administrator) #2
User title: UNB developer & webmaster
Member since Jan 2004 · 3864 posts · Location: Erlangen, Germany
Group memberships: Administrators, Members
Show profile · Link to this post
You could also use the UNB API to fetch stuff from the database (with the default configuration, no need to duplicate db access data) and render it to the screen with correct length limitation. The example plugin 'custompage' gives a small example of it.
♪ ...nanananah, all in all we’re just brilliant thieves, nanananah... ♪♬
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20120620-dev, © 2003-2011 by Yves Goergen
Page created in 178.3 ms (130 ms) · 49 database queries in 72.7 ms
Current time: 2014-08-01, 11:49:06 (UTC +02:00)