473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

How can I fix this code?

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
1 1956
gits
5,390 Expert Mod 4TB
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

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
9
by: bigoxygen | last post by:
Hi. I'm using a 3 tier FrontController Design for my web application right now. The problem is that I'm finding to have to duplicate a lot of code for similar functions; for example, listing...
699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
3
by: Wayne... | last post by:
I'm trying to make a sort of source code libary for some customers of commonly used code to save them a bit of time the problem I have is that although I can get the asp code into a field of an...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
7
by: soule | last post by:
Hello, Fellow Users, I need a brave soul (or 5) to peruse this de-sensitized button code for any obvious or unobvious errors. In the VB editor, there are no red “error” lines of text...but I haven’t...
5
NeoPa
by: NeoPa | last post by:
Log Code and Property References SECTION 1 - TABLE OF CONTENTS SECTION 1 - TABLE OF CONTENTS SECTION 2 - INTRODUCTION SECTION 3 - TABLE LAYOUTS SECTION 4 - CODE TO LOG CODE SECTION 5 - CODE...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.