Volkova

PHP Forum V0.2

Jul 2nd, 2011
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.79 KB | None | 0 0
  1. <?php
  2. /*
  3. *    +--------------------------------------------------------------+
  4. *    | Changelog                                                    |
  5. *    +--------------------------------------------------------------+
  6. *    |                                                              |
  7. *    | Version: 0.2                                                 |
  8. *    |  0.2 > Some other minor things                               |
  9. *    |  0.2 > Threads with new reply go to the top                  |
  10. *    |  0.2 > No blank posts                                        |
  11. *    |  0.1 > Simple Threads, and posts                             |
  12. *    |                                                              |
  13. *    +--------------------------------------------------------------+
  14. */
  15.  
  16. $host = 'localhost';
  17. $username = 'forum_user';
  18. $password = 'Awesome Password';
  19. $database = 'forum_db';
  20. connect($host, $username, $password, $database);
  21.  
  22. function connect($host, $username, $password, $database) {
  23.     mysql_connect($host,$username,$password) or die("Could not connect. " . mysql_error());
  24.     mysql_select_db($database) or die("Could not select database. " . mysql_error());
  25.  
  26.     return buildDB();
  27. }
  28.  
  29. function buildDB() {
  30.     $sql = <<<MySQL_QUERY
  31. CREATE TABLE IF NOT EXISTS forum (
  32. post_num INT NOT NULL AUTO_INCREMENT,
  33. title VARCHAR(150),
  34. user VARCHAR(50),
  35. bodytext TEXT,
  36. OP INT,
  37. created VARCHAR(100),
  38. last_post VARCHAR(100),
  39. PRIMARY KEY(post_num)
  40. );
  41. MySQL_QUERY;
  42.  
  43.     return mysql_query($sql);
  44. }
  45.  
  46. function post($title, $user, $body, $OP, $time) {
  47.     $title = mysql_real_escape_string(htmlentities($title));
  48.     $user = mysql_real_escape_string(htmlentities($user));
  49.     $body = mysql_real_escape_string(htmlentities($body));
  50.     $body = substr($body, 0, 2500);
  51.     $OP ? $OP = mysql_real_escape_string(htmlentities($OP)) : $OP = 0;
  52.     $SQL = "INSERT INTO forum VALUES (NULL, '$title', '$user', '$body', $OP,'$time', '$time');";
  53.     mysql_query($SQL);
  54.     $SQL = "UPDATE forum SET last_post='$time' WHERE post_num=$OP";
  55.     mysql_query($SQL);
  56.     $page_name = $_SERVER['REQUEST_URI'];
  57.     header("Location: $page_name");
  58. }
  59.  
  60. function get_thread() {
  61.     $amount_page = 6;
  62.  
  63.     if ( !$_GET ) post_box();
  64.     if ( $_GET ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."\">Return </a> </h4>";
  65.    
  66.     if (isset($_GET["page"])) { $page = $_GET["page"]; } else { $page = 1; };
  67.     $start_from = ($page-1) * $amount_page;
  68.  
  69.     if ( $_GET['id'] ) {
  70.         $OP = mysql_real_escape_string(htmlentities($_GET['id']));
  71.         if (!is_numeric($OP) || is_float($OP) || $OP <= 0) { exit; }
  72.         $SQL = "SELECT * FROM forum WHERE OP=$OP OR post_num=$OP ORDER BY created ASC LIMIT $start_from, $amount_page";
  73.     } else {
  74.         $OP = 0;
  75.         $SQL = "SELECT * FROM forum WHERE OP=$OP ORDER BY last_post DESC LIMIT $start_from, $amount_page";
  76.     }
  77.    
  78.     $result=mysql_query($SQL);
  79.  
  80.     //pages
  81.     $pages = "<div id=\"pages\">";
  82.  
  83.     $pages_SQL = "SELECT COUNT($OP) from forum WHERE OP=$OP";
  84.     $pages_result = mysql_query($pages_SQL);
  85.     $pages_row = mysql_fetch_row($pages_result);
  86.     $pages_total_records = $pages_row[0];
  87.     $total_pages = ceil($pages_total_records / $amount_page);
  88.    
  89.     if (isset($_GET["page"])) { $currentpage = $_GET["page"]; } else { $currentpage = 1; };
  90.     $currentpage = mysql_real_escape_string(htmlentities($currentpage));
  91.  
  92.     $pages .= "<span class=\"pages\">";
  93.    
  94.     if ($total_pages > 0 && $currentpage > 1) {
  95.             $pages .= "<a href=\"?page=" . (intval($currentpage) - 1) . "\">Prev &laquo; </a>";
  96.         }
  97.        
  98.         if ($total_pages > 1) {
  99.             for ( $i= 1; $i <= $total_pages; $i++) {
  100.                 $pages .= "<a href=\"?page=$i\"> $i </a>  ";
  101.                 if ($i != $total_pages) {
  102.                     $pages .=  " - ";
  103.                 }
  104.             }
  105.         }
  106.        
  107.         if ($total_pages > 0 && $currentpage != $total_pages && $total_pages > 1) {
  108.             $pages .= "<a href=\"?page=" . (intval($currentpage) + 1) . "\"> &raquo; Next </a>";
  109.         }
  110.     $pages .= "</span>\n</div>";
  111.     //end of pages
  112.  
  113.     print $pages;
  114.  
  115.     while ($db_field = mysql_fetch_assoc($result)) {
  116.  
  117.         $reply_am = $db_field["post_num"];
  118.  
  119.         $replyquery = "SELECT COUNT(*) FROM forum WHERE OP=$reply_am";
  120.         $replyresult = mysql_query($replyquery) or die(mysql_error());
  121.         while($replyrow = mysql_fetch_array($replyresult)){
  122.             $replies = $replyrow['COUNT(*)'];
  123.         }
  124.  
  125.         print "<div class=\"thread\">\n";
  126.         print "<div class=\"title\">\n";
  127.         print "<h2>" . stripslashes($db_field["title"]) . "</h2>\n";
  128.         if ( $_GET['id'] ) {
  129.             print "<h4>" . stripslashes($db_field["user"]) . " - " . date('H:i', $db_field["created"]) . "</h4>\n";
  130.         } else {
  131.             print "<h4>" . stripslashes($db_field["user"]) . " - " . date('H:i', $db_field["created"]) . " - " . $replies . " Replies" . "</h4>\n";
  132.         }
  133.         print "</div>\n";
  134.         print "<div class=\"post_body\">\n";
  135.         print "<p>" . stripslashes($db_field["bodytext"]) . "</p>\n";
  136.         print "</div>\n";
  137.         if ( !$_GET['id'] ) print "<h4> <a href=\"".$_SERVER['PHP_SELF']."?id=".$db_field["post_num"]."\">Read more... </a> </h4>";
  138.         print "</div>";
  139.     }
  140.     if ( $_GET ) post_box();
  141. }
  142.  
  143. function post_box() {
  144.     print <<<POST_FORM
  145. <a id="link1" href="javascript:display('show')"> Make Post </a>
  146.  
  147. <div id="postform" style="display:none">
  148.  
  149. <form method="post" >
  150.  
  151.     <input type="hidden" value="{$_GET["id"]}" name="OP" />
  152.  
  153.     <div class="clear"></div>
  154.     <label for="title">Title: </label><br />
  155.     <input name="title" id="title" type="text" maxlength="150" /><br>
  156.  
  157.     <div class="clear"></div>
  158.     <label for="name">Name: </label><br />
  159.     <input name="name" id="name" type="text" maxlength="150" /><br>
  160.  
  161.     <div class="clear"></div>
  162.     <label for="message">Message: </label>Maximum of 2500 Characters
  163.     <textarea name="bodytext" id="bodytext" maxlength=2500></textarea><br>
  164.  
  165.     <input type="submit" value="Submit" />
  166. </form>
  167. </div>
  168. POST_FORM;
  169.  
  170. }
  171.  
  172. if ( $_POST && $_POST['bodytext']) {
  173.     post($_POST['title'],$_POST['name'],$_POST['bodytext'],$_POST['OP'],time());
  174. } else {
  175.     get_thread();
  176. }
  177.  
  178. ?>
Advertisement
Add Comment
Please, Sign In to add comment