Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.10 KB | None | 0 0
  1. <?php
  2. function query_db($query)
  3. {
  4.   $db_username = 'user';
  5.   $db_password = 'pass';
  6.   $db_name = 'database';
  7.  
  8.   $link = mysql_connect('localhost', $db_username, $db_password);
  9.  
  10.   if (!$link) {
  11.     die ('Not Connected : ' . mysql_error());
  12.   }
  13.  
  14.   $database = mysql_select_db($db_name, $link);
  15.   if (!$database) {
  16.     die ('Error: Can\'t conect to database');
  17.   }
  18.  
  19.   $result = mysql_query($query);
  20.  
  21.   mysql_close($link);
  22.  
  23.   if (!$result) {
  24.     $message  = 'Query Failed: ' . mysql_error() . "\n";
  25.     $message .= 'Whole query: ' . $query;
  26.     die($message);
  27.   }
  28.   else {
  29.     return $result;
  30.   }  
  31. }
  32.  
  33. function submit_post($subject, $body)
  34. {
  35.   $subject = mysql_real_escape_string($subject);
  36.   $body = mysql_real_escape_string($body);
  37.  
  38.   $insert_query = "INSERT INTO comments VALUES (NULL, \"$subject\", \"$body\", CURDATE(), CURTIME())";
  39.  
  40.   query_db($insert_query);  
  41. }
  42.  
  43.  
  44. function submit_comment($name, $comment, $postID)
  45. {
  46.   $name = mysql_real_escape_string($name);
  47.   $comment = mysql_real_escape_string($comment);
  48.   $postID = mysql_real_escape_string($postID);
  49.  
  50.   $insert_query = "INSERT INTO comments VALUES (NULL, $postID, \"$name\", \"$comment\", CURDATE(), CURTIME())";
  51.  
  52.   query_db($insert_query);
  53. }
  54.  
  55. // Not yet implimented
  56. function format_date($date)
  57. {
  58.   return $date;
  59. }
  60.  
  61. function pull_comments($postID, $numToPull)
  62. {
  63.   $pull_query = "SELECT name,comment FROM comments WHERE post = $postID
  64.          ORDER BY id LIMIT $numToPull";
  65.  
  66.   $result = query_db($pull_query);
  67.  
  68.   echo '<div class="comments">' . "\n";
  69.   if (mysql_num_rows($result)) {
  70.    
  71.     echo "<table>\n";
  72.  
  73.     while ($row = mysql_fetch_assoc($result))  {
  74.       echo '<div class="comment">' . "\n";
  75.       echo "<tr>\n";
  76.       echo '<td><h6>' . $row['name'] . ":</h6></td>\n";
  77.       echo '<td><p>' . $row['comment'] . "</p></td>\n";
  78.       echo "</tr></div>\n";
  79.     }
  80.    
  81.     echo "</table>\n";
  82.   }
  83.  
  84.   echo "
  85.  <form class=\"commentForm\" action=\"submitComment.php\" method=\"post\">
  86.  <label>name:   </label><input type=\"text\" name=\"name\" /><br />
  87.  <label>comment:</label><textarea name=\"comment\"></textarea>
  88.  <input type=\"submit\" value=\"submit\"></input>
  89.  <input type=\"button\" value=\"cancel\"></input>
  90.  <input type=\"hidden\" value=\"$postID\" onclick=\"cancel_comment(this);\" name=\"postID\"></input>
  91.  </form>
  92. ";
  93.   echo "<a class=\"make_comment\" href=\"javascript: void(0);\" onclick=\"show_comment_form(this)\">leave comment</a>\n";
  94.   echo "</div>\n";
  95. }
  96.  
  97. function pull_posts($numToPull)
  98. {
  99.   // Number of posts to display on the home page
  100.   $postCount = 20;
  101.   $commentCount = 15;
  102.  
  103.   $pull_query = "SELECT * FROM posts ORDER BY date DESC,time DESC LIMIT $postCount";
  104.  
  105.   $result = query_db($pull_query);
  106.  
  107.   while ($row = mysql_fetch_assoc($result)) {
  108.       echo '<div class="post">';
  109.       echo '<div class="postHeader">';
  110.       echo '<h1>' . $row['subject'] . "</h1>\n";
  111.       echo '<h3>' . format_date($row['date']) . "</h3>\n";
  112.       echo "</div>\n";
  113.       echo '<p>' . $row['body'] . "</p>\n";
  114.       pull_comments($row['id'], $commentCount);
  115.       echo "</div>\n";
  116.   }
  117. }
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement