Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function query_db($query)
- {
- $db_username = 'user';
- $db_password = 'pass';
- $db_name = 'database';
- $link = mysql_connect('localhost', $db_username, $db_password);
- if (!$link) {
- die ('Not Connected : ' . mysql_error());
- }
- $database = mysql_select_db($db_name, $link);
- if (!$database) {
- die ('Error: Can\'t conect to database');
- }
- $result = mysql_query($query);
- mysql_close($link);
- if (!$result) {
- $message = 'Query Failed: ' . mysql_error() . "\n";
- $message .= 'Whole query: ' . $query;
- die($message);
- }
- else {
- return $result;
- }
- }
- function submit_post($subject, $body)
- {
- $subject = mysql_real_escape_string($subject);
- $body = mysql_real_escape_string($body);
- $insert_query = "INSERT INTO comments VALUES (NULL, \"$subject\", \"$body\", CURDATE(), CURTIME())";
- query_db($insert_query);
- }
- function submit_comment($name, $comment, $postID)
- {
- $name = mysql_real_escape_string($name);
- $comment = mysql_real_escape_string($comment);
- $postID = mysql_real_escape_string($postID);
- $insert_query = "INSERT INTO comments VALUES (NULL, $postID, \"$name\", \"$comment\", CURDATE(), CURTIME())";
- query_db($insert_query);
- }
- // Not yet implimented
- function format_date($date)
- {
- return $date;
- }
- function pull_comments($postID, $numToPull)
- {
- $pull_query = "SELECT name,comment FROM comments WHERE post = $postID
- ORDER BY id LIMIT $numToPull";
- $result = query_db($pull_query);
- echo '<div class="comments">' . "\n";
- if (mysql_num_rows($result)) {
- echo "<table>\n";
- while ($row = mysql_fetch_assoc($result)) {
- echo '<div class="comment">' . "\n";
- echo "<tr>\n";
- echo '<td><h6>' . $row['name'] . ":</h6></td>\n";
- echo '<td><p>' . $row['comment'] . "</p></td>\n";
- echo "</tr></div>\n";
- }
- echo "</table>\n";
- }
- echo "
- <form class=\"commentForm\" action=\"submitComment.php\" method=\"post\">
- <label>name: </label><input type=\"text\" name=\"name\" /><br />
- <label>comment:</label><textarea name=\"comment\"></textarea>
- <input type=\"submit\" value=\"submit\"></input>
- <input type=\"button\" value=\"cancel\"></input>
- <input type=\"hidden\" value=\"$postID\" onclick=\"cancel_comment(this);\" name=\"postID\"></input>
- </form>
- ";
- echo "<a class=\"make_comment\" href=\"javascript: void(0);\" onclick=\"show_comment_form(this)\">leave comment</a>\n";
- echo "</div>\n";
- }
- function pull_posts($numToPull)
- {
- // Number of posts to display on the home page
- $postCount = 20;
- $commentCount = 15;
- $pull_query = "SELECT * FROM posts ORDER BY date DESC,time DESC LIMIT $postCount";
- $result = query_db($pull_query);
- while ($row = mysql_fetch_assoc($result)) {
- echo '<div class="post">';
- echo '<div class="postHeader">';
- echo '<h1>' . $row['subject'] . "</h1>\n";
- echo '<h3>' . format_date($row['date']) . "</h3>\n";
- echo "</div>\n";
- echo '<p>' . $row['body'] . "</p>\n";
- pull_comments($row['id'], $commentCount);
- echo "</div>\n";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement