Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * +--------------------------------------------------------------+
- * | Changelog |
- * +--------------------------------------------------------------+
- * | |
- * | Version: 0.2 |
- * | 0.2 > Some other minor things |
- * | 0.2 > Threads with new reply go to the top |
- * | 0.2 > No blank posts |
- * | 0.1 > Simple Threads, and posts |
- * | |
- * +--------------------------------------------------------------+
- */
- $host = 'localhost';
- $username = 'forum_user';
- $password = 'Awesome Password';
- $database = 'forum_db';
- connect($host, $username, $password, $database);
- function connect($host, $username, $password, $database) {
- mysql_connect($host,$username,$password) or die("Could not connect. " . mysql_error());
- mysql_select_db($database) or die("Could not select database. " . mysql_error());
- return buildDB();
- }
- function buildDB() {
- $sql = <<<MySQL_QUERY
- CREATE TABLE IF NOT EXISTS forum (
- post_num INT NOT NULL AUTO_INCREMENT,
- title VARCHAR(150),
- user VARCHAR(50),
- bodytext TEXT,
- OP INT,
- created VARCHAR(100),
- last_post VARCHAR(100),
- PRIMARY KEY(post_num)
- );
- MySQL_QUERY;
- return mysql_query($sql);
- }
- function post($title, $user, $body, $OP, $time) {
- $title = mysql_real_escape_string(htmlentities($title));
- $user = mysql_real_escape_string(htmlentities($user));
- $body = mysql_real_escape_string(htmlentities($body));
- $body = substr($body, 0, 2500);
- $OP ? $OP = mysql_real_escape_string(htmlentities($OP)) : $OP = 0;
- $SQL = "INSERT INTO forum VALUES (NULL, '$title', '$user', '$body', $OP,'$time', '$time');";
- mysql_query($SQL);
- $SQL = "UPDATE forum SET last_post='$time' WHERE post_num=$OP";
- mysql_query($SQL);
- $page_name = $_SERVER['REQUEST_URI'];
- header("Location: $page_name");
- }
- function get_thread() {
- $amount_page = 6;
- if ( !$_GET ) post_box();
- if ( $_GET ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."\">Return </a> </h4>";
- if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; };
- $start_from = ($page-1) * $amount_page;
- if ( $_GET['id'] ) {
- $OP = mysql_real_escape_string(htmlentities($_GET['id']));
- if (!is_numeric($OP) || is_float($OP) || $OP <= 0) { exit; }
- $SQL = "SELECT * FROM forum WHERE OP=$OP OR post_num=$OP ORDER BY created ASC LIMIT $start_from, $amount_page";
- } else {
- $OP = 0;
- $SQL = "SELECT * FROM forum WHERE OP=$OP ORDER BY last_post DESC LIMIT $start_from, $amount_page";
- }
- $result=mysql_query($SQL);
- //pages
- $pages = "<div id=\"pages\">";
- $pages_SQL = "SELECT COUNT($OP) from forum WHERE OP=$OP";
- $pages_result = mysql_query($pages_SQL);
- $pages_row = mysql_fetch_row($pages_result);
- $pages_total_records = $pages_row[0];
- $total_pages = ceil($pages_total_records / $amount_page);
- if (isset($_GET["page"])) { $currentpage = $_GET["page"]; } else { $currentpage = 1; };
- $currentpage = mysql_real_escape_string(htmlentities($currentpage));
- $pages .= "<span class=\"pages\">";
- if ($total_pages > 0 && $currentpage > 1) {
- $pages .= "<a href=\"?page=" . (intval($currentpage) - 1) . "\">Prev « </a>";
- }
- if ($total_pages > 1) {
- for ( $i= 1; $i <= $total_pages; $i++) {
- $pages .= "<a href=\"?page=$i\"> $i </a> ";
- if ($i != $total_pages) {
- $pages .= " - ";
- }
- }
- }
- if ($total_pages > 0 && $currentpage != $total_pages && $total_pages > 1) {
- $pages .= "<a href=\"?page=" . (intval($currentpage) + 1) . "\"> » Next </a>";
- }
- $pages .= "</span>\n</div>";
- //end of pages
- print $pages;
- while ($db_field = mysql_fetch_assoc($result)) {
- $reply_am = $db_field["post_num"];
- $replyquery = "SELECT COUNT(*) FROM forum WHERE OP=$reply_am";
- $replyresult = mysql_query($replyquery) or die(mysql_error());
- while($replyrow = mysql_fetch_array($replyresult)){
- $replies = $replyrow['COUNT(*)'];
- }
- print "<div class=\"thread\">\n";
- print "<div class=\"title\">\n";
- print "<h2>" . stripslashes($db_field["title"]) . "</h2>\n";
- if ( $_GET['id'] ) {
- print "<h4>" . stripslashes($db_field["user"]) . " - " . date('H:i', $db_field["created"]) . "</h4>\n";
- } else {
- print "<h4>" . stripslashes($db_field["user"]) . " - " . date('H:i', $db_field["created"]) . " - " . $replies . " Replies" . "</h4>\n";
- }
- print "</div>\n";
- print "<div class=\"post_body\">\n";
- print "<p>" . stripslashes($db_field["bodytext"]) . "</p>\n";
- print "</div>\n";
- if ( !$_GET['id'] ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."?id=".$db_field["post_num"]."\">Read more... </a> </h4>";
- print "</div>";
- }
- if ( $_GET ) post_box();
- }
- function post_box() {
- print <<<POST_FORM
- <a id="link1" href="javascript:display('show')"> Make Post </a>
- <div id="postform" style="display:none">
- <form method="post" >
- <input type="hidden" value="{$_GET["id"]}" name="OP" />
- <div class="clear"></div>
- <label for="title">Title: </label><br />
- <input name="title" id="title" type="text" maxlength="150" /><br>
- <div class="clear"></div>
- <label for="name">Name: </label><br />
- <input name="name" id="name" type="text" maxlength="150" /><br>
- <div class="clear"></div>
- <label for="message">Message: </label>Maximum of 2500 Characters
- <textarea name="bodytext" id="bodytext" maxlength=2500></textarea><br>
- <input type="submit" value="Submit" />
- </form>
- </div>
- POST_FORM;
- }
- if ( $_POST && $_POST['bodytext']) {
- post($_POST['title'],$_POST['name'],$_POST['bodytext'],$_POST['OP'],time());
- } else {
- get_thread();
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment