Advertisement
Z645

posts.inc.php

Apr 19th, 2013
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.14 KB | None | 0 0
  1. <?php
  2.  
  3. //checks if the given post id is in the table
  4. function valid_pid($pid){
  5.     $pid = (int)$pid;
  6.    
  7.     $total = mysql_query("SELECT COUNT(`post_id`) FROM `posts` WHERE `post_id` = '{$pid}'");
  8.     $total = mysql_result($total, 0);
  9.    
  10.     echo mysql_error();
  11.    
  12.     if ($total != 1){
  13.         return false;
  14.     }else{
  15.         return true;
  16.     }
  17. }
  18.  
  19. //fetches a summary of all the posts
  20. function get_posts(){
  21.     $sql = "SELECT
  22.                 `posts`.`post_id` AS `id`,
  23.                 `posts`.`post_body` AS `preview`,
  24.                 `posts`.`post_user` AS `user`,
  25.                 DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i') AS `date`,
  26.                 `comments`.`total_comments`,
  27.                 DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
  28.             FROM `posts`
  29.             LEFT JOIN(
  30.                 SELECT
  31.                     `post_id`,
  32.                     COUNT(`comment_id`) AS `total_comments`,
  33.                     MAX(`comment_date`) AS `last_comment`
  34.                 FROM `comments`
  35.                 GROUP BY `post_id`
  36.             ) AS `comments`
  37.             ON `posts`.`post_id` = `comments`.`post_id`
  38.             ORDER BY `posts`.`post_date` DESC";
  39.    
  40.     $posts = mysql_query($sql);
  41.    
  42.     $rows = array();
  43.     while (($row = mysql_fetch_assoc($posts)) !== false){
  44.         $rows[] = array(
  45.             'id'                => $row['id'],
  46.             'preview'           => $row['preview'],
  47.             'user'              => $row['user'],
  48.             'date'              => $row['date'],
  49.             'total_comments'    => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
  50.             'last_comment'      => ($row['last_comment'] === null) ? 'No recent comments. Be the first to comment!' : $row['last_comment']
  51.         );
  52.     }
  53.    
  54.     return $rows;
  55. }
  56.  
  57. //fetches a single post from the table
  58. function get_post($pid){
  59.     $pid = (int)$pid;
  60.    
  61.     $sql = "SELECT
  62.                 `post_body` AS `body`,
  63.                 `post_user` AS `user`,
  64.                 `post_date` AS `date`
  65.             FROM `posts`
  66.             WHERE `post_id` = {$pid}";
  67.            
  68.     $post = mysql_query($sql);
  69.     $post = mysql_fetch_assoc($post);
  70.    
  71.     $post['comments'] = get_comments($pid);
  72.    
  73.     return $post;
  74. }
  75.  
  76. // adds a new post entry
  77. function add_post($name, $body){
  78.     $name = mysql_real_escape_string(htmlentities($name));
  79.     $body = mysql_real_escape_string(nl2br(htmlentities($body)));
  80.    
  81.     mysql_query("INSERT INTO `posts` (`post_user`, `post_body`, `post_date`) VALUES ('{$name}', '{$body}', NOW())");
  82. }
  83.  
  84. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement