Simple message board working on MySQL database. It supports multiple levels, threads, search by topic or free text. SQL part is made up using PHPLib. Zip file includes a working example and images for toolbars. http://members.xoom.it/fathom/TinyBoard-1.2.zip
By : fath
<?
/******************************************************************************************
* TinyBoard 1.2 *
* by Francesco Toscan, fathom@unixlover.com *
* Included classes: PHPLib *
* This code is released under GPL license *
******************************************************************************************/
$board_tbl_questions = "board_questions";
$board_tbl_answers = "board_answers";
function BoardToolsFull ($ID,$Depth,$FormerID) {
global $PHP_SELF, $type;
require("modules/board-toolsfull.html");
}
function BoardToolsPartial () {
global $PHP_SELF;
require("modules/board-toolspartial.html");
}
function BoardFooter () {
require("modules/board-footer.html");
}
/* Let's simply list messages in questions table and check whether there are replies */
function BoardIndex () {
global $db, $PHP_SELF, $board_tbl_questions, $board_tbl_answers;
$db->query("SELECT ID,Sender,Date,Topic,NumMsgs from $board_tbl_questions order by Date;");
$numrows = $db->num_rows();
if ($numrows != 0) {
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">";
echo "<tr><td>\n";
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#e6e6e6\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
echo "<tr>\n";
echo "<th bgcolor=\"#333399\" width=\"60%\"><font size=\"-1\" color=\"#FFFFFF\">Topic</font>\n";
echo "</th>\n";
echo "<th bgcolor=\"#333399\" width=\"20%\"><font color=\"#FFFFFF\" size=\"-1\">Sender</font></th>\n";
echo "<th bgcolor=\"#333399\"><font color=\"#FFFFFF\" size=\"-1\">Date</font></th>\n";
echo "</tr>\n";
while ($db->next_record()) {
$ID = $db->f(ID);
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
if ($db->f(NumMsgs) > 0) {
echo "<a href=\"$PHP_SELF?action=expand_thread&ID=$ID\"><img src=\"pics/plus.gif\" border=\"0\" valign=\"middle\"></a> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
}
else {
echo "<img src=\"pics/stateful.gif\" border=\"0\"> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
}
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</td></tr></table>\n";
}
else {
echo "No message found.\n";
}
BoardFooter();
}
/* We want to add messages */
function BoardAddNewMsg() {
global $db, $CodSede, $Sender, $Text, $Topic, $PHP_SELF, $board_tbl_questions, $board_tbl_answers;
/* Let's make an unique ID for each message. */
$ID = uniqid(rand(),true);
$Date = date("Y/m/d");
/* Check whether ID is really unique */
$db->query("SELECT ID from $board_tbl_questions where ID=\"$ID\";");
$numrows = $db->num_rows();
if ($numrows != 0) {
while ($numrows != 0) {
$ID = uniqid(rand(),true);
$numrows = $db->num_rows();
}
}
/* Insert query */
$query = "INSERT INTO $board_tbl_questions SET ID=\"$ID\",Sender=\"$CodSede\",Date=\"$Date\",Text=\"$Text\",Topic=\"$Topic\";";
if ($db->query($query)) {
echo "<center>Message sent.<br>\n";
echo "<a href=\"$PHP_SELF\">Back</a></center>\n";
}
else {
echo "Problem has occurred while sending message. Send failed.\n";
}
BoardFooter();
}
/* Expanding threads by question ID */
function BoardThreadExpand($ID,$maxDepth) {
global $db, $PHP_SELF, $board_tbl_questions, $board_tbl_answers;
/* First of all the former message */
if ($maxDepth == 1) {
$db->query("SELECT ID,Sender,Date,Topic from $board_tbl_questions where ID=\"$ID\";");
}
else {
$db->query("SELECT ID,Sender,Date,Topic from $board_tbl_answers where ID=\"$ID\";");
}
$db->next_record();
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">";
echo "<tr><td>\n";
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#e6e6e6\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
echo "<tr>\n";
echo "<th bgcolor=\"#333399\" width=\"60%\"><font size=\"-1\" color=\"#FFFFFF\">Topic</font>\n";
echo "</th>\n";
echo "<th bgcolor=\"#333399\" width=\"20%\"><font color=\"#FFFFFF\" size=\"-1\">Sender</font></th>\n";
echo "<th bgcolor=\"#333399\"><font color=\"#FFFFFF\" size=\"-1\">Date</font></th>\n";
echo "</tr>\n";
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
echo "<a href=\"javascript: history.back(-1)\">";
echo "<img src=\"pics/minus.gif\" border=\"0\" valign=\"middle\"></a> ";
if ($maxDepth == 1) {
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
}
else {
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=answer\">$Topic</a></font></td>\n";
}
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
/* List all messages matching thread ID */
$query = "SELECT ID,Sender,Date,Topic,Depth,NumMsgs from $board_tbl_answers where FormerID=\"$ID\" OR OrigID=\"$ID\";";
$db->query($query);
$numMsgs = $db->num_rows();
while ($db->next_record()) {
if ($db->f(Depth) == $maxDepth) {
$ID = $db->f(ID);
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
$Depth = $db->f(Depth);
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
if ($db->f(NumMsgs) > 0) {
$newDepth = $Depth +1;
echo "<a href=\"$PHP_SELF?action=expand_thread&ID=$ID&maxDepth=$newDepth\"><img src=\"pics/plus.gif\" border=\"0\" valign=\"middle\"></a> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=answer\">$Topic</a></font></td>\n";
}
else {
echo "<img src=\"pics/node.gif\" border=\"0\"> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=answer\">$Topic</a></font></td>\n";
}
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
}
}
echo "</table>\n";
echo "</td></tr></table>\n";
echo "<br>\n";
echo "<font size=\"-2\">Level <b>$maxDepth</b></font>\n";
BoardFooter();
}
function BoardShowMsg ($ID,$type) {
global $db, $PHP_SELF, $board_tbl_questions, $board_tbl_answers;
if ($type == "question") {
$query = "SELECT ID,Sender,Topic,Date,Text from $board_tbl_questions where ID=\"$ID\";";
}
else {
$query = "SELECT ID,Sender,Topic,Date,Text,FormerID,Depth from $board_tbl_answers where ID=\"$ID\";";
}
$db->query($query);
$numrows = $db->num_rows();
if ($numrows > 0) {
$db->next_record();
$Sender = $db->f(Sender);
$Topic = $db->f(Topic);
$Date = $db->f(Date);
$Text = $db->f(Text);
$FormerID = $db->f(FormerID);
$Depth = $db->f(Depth);
BoardToolsFull($ID,$Depth,$FormerID);
require("modules/board-show.html");
}
else {
echo "Message ID $ID not found.\n";
}
BoardFooter();
}
function BoardNewMsg () {
require("modules/board-newmsg.html");
}
function BoardAddReply ($ID) {
global $PHP_SELF, $db, $type, $Depth, $Sender, $Text, $Topic, $FormerID, $board_tbl_questions, $board_tbl_answers;
$newID = uniqid(rand(),true);
$Date = date("Y/m/d");
if ($type == "question") {
$query = "INSERT INTO $board_tbl_answers SET ID=\"$newID\",OrigID=\"$ID\",FormerID=\"$ID\",Depth=\"1\",Sender=\"$Sender\",Date=\"$Date\",Topic=\"$Topic\",Text=\"$Text\";";
$db->query($query);
$update = "UPDATE $board_tbl_questions SET NumMsgs=NumMsgs +1 WHERE ID=\"$ID\";";
$db->query($update);
echo "<center>Message sent.<br>\n";
echo "<a href=\"$PHP_SELF\">Back</a></center>\n";
}
else {
$Depth = $Depth +1;
$query = "INSERT INTO $board_tbl_answers SET ID=\"$newID\",OrigID=\"$ID\",FormerID=\"$FormerID\",Depth=\"$Depth\",Sender=\"$Sender\",Date=\"$Date\",Topic=\"$Topic\",Text=\"$Text\";";
$db->query($query);
$update = "UPDATE $board_tbl_answers SET NumMsgs=NumMsgs +1 WHERE ID=\"$ID\";";
$db->query($update);
echo "<center>Message sent.<br>\n";
echo "<a href=\"$PHP_SELF\">Back</a></center>\n";
}
BoardFooter();
}
function BoardReplyMsg($ID) {
global $PHP_SELF, $db, $FormerID, $Depth, $type, $CodSede, $board_tbl_questions, $board_tbl_answers;
if ($type == "question") {
$query = "SELECT Sender,Topic,Date,Text from $board_tbl_questions where ID=\"$ID\";";
}
else {
$query = "SELECT Sender,Topic,Date,Text from $board_tbl_answers where ID=\"$ID\";";
}
$db->query($query);
$numrows = $db->num_rows();
if ($numrows > 0) {
$db->next_record();
$Sender = $db->f(Sender);
$Topic = $db->f(Topic);
$Date = $db->f(Date);
$Text = $db->f(Text);
}
BoardToolsPartial();
require("modules/board-replymsg.html");
BoardFooter();
}
function BoardPerformSearch ($what) {
global $PHP_SELF, $db, $Topic, $Text, $board_tbl_questions, $board_tbl_answers;
$found = 0;
switch ($what) {
case topic:
$query = "SELECT ID,Sender,Topic,Date,NumMsgs from $board_tbl_questions WHERE Topic LIKE \"%$Topic%\";";
$db->query($query);
echo "<h4>Search results</h4>\n";
BoardToolsPartial();
if ($db->num_rows() > 0) {
$found++;
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">";
echo "<tr><td>\n";
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#e6e6e6\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
echo "<tr>\n";
echo "<th bgcolor=\"#333399\" width=\"60%\"><font size=\"-1\" color=\"#FFFFFF\">Topic</font>\n";
echo "</th>\n";
echo "<th bgcolor=\"#333399\" width=\"20%\"><font color=\"#FFFFFF\" size=\"-1\">Sender</font></th>\n";
echo "<th bgcolor=\"#333399\"><font color=\"#FFFFFF\" size=\"-1\">Date</font></th>\n";
echo "</tr>\n";
while ($db->next_record()) {
$ID = $db->f(ID);
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
if ($db->f(NumMsgs) > 0) {
echo "<a href=\"$PHP_SELF?action=expand_thread&ID=$ID\"><img src=\"pics/plus.gif\" border=\"0\" valign=\"middle\"></a> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
}
else {
echo "<img src=\"pics/stateful.gif\" border=\"0\"> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
}
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</td></tr></table>\n";
}
else {
echo "No message found.\n";
}
break;
case text:
$query = "SELECT ID,Topic,Sender,Date,NumMsgs from $board_tbl_questions WHERE Text LIKE \"%$Text%\";";
$db->query($query);
echo "<h4>Search results</h4>\n";
BoardToolsPartial();
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#000000\" cellspacing=\"0\" cellpadding=\"1\" align=\"center\">";
echo "<tr><td>\n";
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#e6e6e6\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
echo "<tr>\n";
echo "<th bgcolor=\"#333399\" width=\"60%\"><font size=\"-1\" color=\"#FFFFFF\">Topic</font>\n";
echo "</th>\n";
echo "<th bgcolor=\"#333399\" width=\"20%\"><font color=\"#FFFFFF\" size=\"-1\">Sender</font></th>\n";
echo "<th bgcolor=\"#333399\"><font color=\"#FFFFFF\" size=\"-1\">Date</font></th>\n";
echo "</tr>\n";
if ($db->num_rows() > 0) {
$found++;
while ($db->next_record()) {
$ID = $db->f(ID);
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
echo "<img src=\"pics/stateful.gif\" border=\"0\"> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=question\">$Topic</a></font></td>\n";
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
echo "</td></tr></table>\n";
}
$query = "SELECT ID,Topic,Sender,Date,Depth,NumMsgs from $board_tbl_answers WHERE Text LIKE \"%$Text%\";";
$db->query($query);
if ($db->num_rows() > 0) {
$found++;
while ($db->next_record()) {
$ID = $db->f(ID);
$Sender = $db->f(Sender);
$Date = $db->f(Date);
$Topic = $db->f(Topic);
$Depth = $db->f(Depth);
echo "<tr>\n";
echo "<td width=\"60%\"><font size=\"-2\">\n";
echo "<img src=\"pics/stateful.gif\" border=\"0\"> ";
echo "<a href=\"$PHP_SELF?action=show_msg&ID=$ID&type=answer\">$Topic</a></font></td>\n";
echo "<td width=\"20%\" align=\"center\"><font size=\"-2\">$Sender</font></td>\n";
echo "<td align=\"center\"><font size=\"-2\">$Date</font></td>\n";
echo "</tr>\n";
}
echo "</table>\n";
/* if ($found == "0") {
echo "<table width=\"100%\" border=\"0\" bgcolor=\"#e6e6e6\" cellspacing=\"0\" cellpadding=\"0\" align=\"center\">";
echo "<tr><td align=\"center\">No message found</td></tr></table>\n";
} */
echo "</td></tr></table>\n";
}
break;
default:
echo "Missing search key.\n";
break;
}
BoardFooter();
}
function BoardSearchMsg() {
global $PHP_SELF, $board_tbl_questions, $board_tbl_answers;
require("modules/board-searchmsg.html");
BoardFooter();
}
/* Use at your own risk :)) */
?>
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
More Discussion Board Code Articles
More By Codewalkers
developerWorks - FREE Tools! |
Hold your calendar on January 30, 2008 for this free webcast on the new i5/OS. Rational's Enterprise Modernization products will be discussed at this webcast as they help to drive the application development environment for this new System i OS. <br />And learn how i5/OS will take you to the next step of efficient, resilient business processing. You will hear about the new i5/OS capabilities as it will be the most significant i5/OS release in years. If you cannot join the webcast on 1/30/08 you can still use this link to listen to the replay.<br /> FREE! Go There Now!
|
|
|
|
You'll get answers to many questions and more from David Barnes, Lead Evangelist for IBM Emerging Internet Technologies. David will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve. FREE! Go There Now!
|
|
|
|
Build secure Web services with transport-level security using IBM Rational Application Developer V7 and IBM WebSphere Application Server V6.1. Follow this three-part series for step-by-step instructions about how to develop Web services and clients, configure HTTP basic authentication, and configure HTTP over SSL (HTTPS). This first part of the series walks you through building a Web service for a simple calculator application. You generate and test two different types of Web services clients: a Java Platform, Enterprise Edition (Java EE) client and a stand-alone Java client. You also handle user-defined exceptions in Web services. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of Lotus Quickr 8.0, which enables collaboration by transforming the way everyday business content such as documents, rich media, photos, and video can be shared. Lotus Quickr makes it faster and easier to share content of all types (not just documents) within virtual teams. It is designed to make it easier to collaborate across organizational boundaries, while continuing to work within the context of familiar desktop applications. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download the latest trial version of IBM Data Studio V1.1 at no cost. IBM Data Studio is a comprehensive data management solution that helps you effectively design, develop, deploy and manage your data, databases, and database applications throughout the data management life cycle utilizing a consistent and integrated user interface. Unlike other client-side data management solutions that focus on only one aspect of the application lifecycle or database administration, Data Studio complements the Rational Software Delivery platform, providing unparalleled flexibility for a heterogeneous data server environment across platforms. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial version of IBM Rational Business Developer V7.1. Rational Business Developer offers rapid and simplified development of business applications and services through Enterprise Generation Language (EGL) tools, generating Java or mainframe solutions while shielding developers from technical complexities. FREE! Go There Now!
|
|
|
|
Visit IBM developerWorks to download a free trial of the Rational Host Access Transformation Services (HATS) Toolkit. The HATS toolkit provides a set of plug-ins for the IBM Rational Software Delivery Platform to help you easily extend your legacy applications. HATS makes your 3270 and 5250 applications available as HTML through the most popular Web browsers, while converting your host screens to a Web look and feel and it also enables you to develop new Web, portal, and rich-client applications. FREE! Go There Now!
|
|
|
|
Get a free trial download of the latest version of IBM Rational Functional Tester V7.0.1. Rational Functional Tester is an automated functional and regression testing solution for QA teams concerned with the quality of their Java, Microsoft Visual Studio .NET, and Web-based applications. FREE! Go There Now!
|
|
|
|
The discipline of assembling and delivering software is maturing beyond standard developer-centric compile/test software builds. The end-to-end software development lifecycle is emerging as the new focus moves “Beyond the Build.” Join this on demand webcast to learn about methods for streamlining software delivery and key capabilities of the IBM Rational Build Forge framework for automating build and release management in environments of any size. FREE! Go There Now!
|
|
|
|
User communities play an important role in communication and collaboration around products, solutions and other areas of special interest to members. Successful communities are able to provide the right mix of content and services to deliver a value proposition that resonates with each audience. Join Tom Inman, VP of Marketing for Information and Platform Solutions as he introduces the new LeverageINFORMATION community. During this webcast, learn about the value provided by the community and how customers and partners derive value from the community in addressing their own technical and business challenges. FREE! Go There Now!
|
|
|
|
All FREE IBM® developerWorks Tools! |