By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,538 Members | 911 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,538 IT Pros & Developers. It's quick & easy.

How can I fix this code?

P: 1
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. class Post {
  3. private $user_obj;
  4. private $con;
  5.  
  6. public function __construct($con, $user){
  7. $this->con = $con;
  8. $this->user_obj = new User($con, $user);
  9. }
  10.  
  11. public function submitPost($body, $user_to) {
  12. $body = strip_tags($body); 
  13. $body = mysqli_real_escape_string($this->con, $body);
  14. $check_empty = preg_replace('/\s+/', '', $body);
  15.  
  16. if($check_empty != "") {
  17.  
  18. $date_added = date("Y-m-d H:i:s");
  19. $added_by = $this->user_obj->getUsername();
  20.  
  21. if($user_to == $added_by) {
  22.     $user_to = "none";
  23. }
  24.  
  25.     $query = mysqli_query($this->con, "INSERT INTO posts VALUES('', '$body', '$added_by', '$user_to', '$date_added', 'no', 'no', '0')");
  26.     $returned_id = mysqli_insert_id($this->con);
  27.     $num_posts = $this->user_obj->getNumPosts();
  28.     $num_posts++;
  29.     $update_query = mysqli_query($this->con, "UPDATE users SET num_posts='$num_posts' WHERE username='$added_by'");
  30.  
  31.         }
  32.     }
  33.  
  34.         public function loadPostsFriends() {
  35.         $str = ""; //String to return 
  36.         $data = mysqli_query($this->con, "SELECT * FROM posts WHERE deleted='no' ORDER BY id DESC");
  37.  
  38.         while($row = mysqli_fetch_array($data)) {
  39.                 $id = $row['id'];
  40.                 $body = $row['body'];
  41.                 $added_by = $row['added_by'];
  42.                 $date_time = $row['date_added'];
  43.  
  44.                 //Prepare user_to string so it can be included even if not posted to a user
  45.                 if($row['user_to'] == "none") {
  46.                 $user_to = "";                
  47.             }
  48.             else {
  49.  
  50.             $user_to_obj = new User($con, $row['user_to']);            
  51.             $user_to_name = $user_to_obj->getFirstAndLastName();
  52.             $user_to = "to <a href='" . $row['user_to'] ."'>" . $user_to_name . "</a>";
  53.             }
  54.  
  55.                 //Check if user who posted, has their account closed
  56.                 $added_by_obj = new User($this->con, $added_by);    
  57.                 if($added_by_obj->isClosed()) {
  58.                     continue;
  59.             }
  60.  
  61.             $user_details_query = mysqli_query($this->con, "SELECT first_name, last_name, profile_pic FROM users WHERE username='$added_by'");            
  62.             $user_row = mysqli_fetch_array($user_details_query);
  63.             $first_name = $user_row['first_name'];
  64.             $profile_pic = $user_row['profile_pic'];
  65.  
  66.             //Timeframe        
  67.             $date_time_now = date("Y-m-d H:i:s");
  68.             $start_date = new DateTime($date_time); //Time of post
  69.             $end_date = new DateTime($date_time_now); //Current time    
  70.             $interval = $start_date->diff($end_date); //Difference between dates     
  71.             if($interval->y >= 1) {    
  72.                 if($interval == 1)            
  73.                     $time_message = $interval->y . " year ago"; //1 year ago
  74.                 else
  75.                     $time_message = $interval->y . " years ago"; //1+ year ago
  76.             }
  77.             else if ($interval-> m >= 1) {            
  78.                 if($interval->d == 0) {
  79.                     $days = " ago";                
  80.  
  81.                 }
  82.                 else if($interval->d == 1) {
  83.                     $days = $interval->d . " day ago";
  84.                 }
  85.                 else {
  86.                     $days = $interval->d . " days ago";
  87.                 }
  88.  
  89.                 if($interval->m == 1) {
  90.                     $time_message = $interval->m . " month". $days;
  91.                 }
  92.                 else {
  93.                     $time_message = $interval->m . " months". $days;
  94.                 }
  95.  
  96.             }
  97.             else if($interval->d >= 1) {
  98.                 if($interval->d == 1) {
  99.                     $time_message = "Yesterday";
  100.                 }
  101.                 else {
  102.                     $time_message = $interval->d . " days ago";
  103.                 }
  104.             }
  105.             else if($interval->h >= 1) {
  106.                 if($interval->h == 1) {
  107.                     $time_message = $interval->h . " hour ago";
  108.                 }
  109.                 else {
  110.                     $time_message = $interval->h . " hours ago";
  111.                 }    
  112.             }
  113.             else if($interval->i >= 1) {
  114.                 if($interval->i == 1) {
  115.                     $time_message = $interval->i . " minute ago";
  116.                 }
  117.                 else {
  118.                     $time_message = $interval->i . " minutes ago";
  119.                 }    
  120.             }
  121.             else {
  122.                 if($interval->s < 30) {
  123.                     $time_message = "Just now";
  124.                 }
  125.                 else {
  126.                     $time_message = $interval->s . " seconds ago";
  127.                 }    
  128.             }
  129.  
  130.             $str .= "<div class='status_post'>
  131.                     <div class='post_profile_pic'>
  132.                         <img src='$profile_pic' width='50'>
  133.                     </div>
  134.  
  135.                     <div class='posted_by' style='color:#ACACAC;'>
  136.                         <a href='$added_by'> $first_name $last_name </a> $user_to &nbsp;&nbsp;&nbsp;&nbsp;$time_message                
  137.                     </div>
  138.                     <div id='post_body'>
  139.                     $body
  140.                     <br>
  141.                     </div>                        
  142.  
  143.                 </div>";
  144.  
  145.         }
  146.  
  147.         echo $str;
  148.  
  149.     }
  150. }
  151.  
  152. ?>
Feb 18 '20 #1
Share this Question
Share on Google+
1 Reply


gits
Expert Mod 5K+
P: 5,387
what exactly needs to be fixed? please provide more context for the reader and ask a proper question - this will increase the probability of getting an answer to your problem.
Feb 18 '20 #2

Post your reply

Sign in to post your reply or Sign up for a free account.