Jump to content

Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:\xampp\htdocs\social\Classes\Post.php on line 39


Ehsan_Nawaz

Recommended Posts

<?php
class Post{
	private $user_obj;
	private $con;

	public function __construct($con, $user){
  	$this->con = $con; 
   	$this->user_obj =  new User($con,$user);
	}
	public function submitPost($body,$user_to){
	$body = strip_tags($body);////Removing HTML TAGS
	$body = mysqli_real_escape_string($this->con,$body);
	$check_empty = preg_replace('/\s+/', '', $body);//delte all spaces

	if($check_empty != ""){


	//Current Date and time
	$date_added = date("Y-m-d H:i:s");

	//get username
	$added_by = $this->user_obj->getUsername();

	//if user have not a profile send to the none
	if($user_to == $added_by){
	$user_to = "none";
	}
	////insert query
	$query = mysqli_query($this->con,"INSERT INTO posts VALUES('','$body','$added_by','$user_to','$date_added','no','no','0')");
	$retured_id = mysqli_insert_id($this->con);

	//insert notification

	//Update post count for user
	$num_post = $this->user_obj->getNumPosts();
	$num_post++;
	$update_query = mysqli_query($this->con,"UPDATE users SET num_post = '$num_post' WHERE username = '$added_by'" );
}
	public function loadPostsFriends(){
	$str = ""; //string to return
	$data = mysqli_query($this->con,"SELECT * FROM posts where deleted = 'no' ORDER by id DESC");
	while($row = mysqli_fetch_array($data))
	{
		$id = $row['id'];
		$body = $row['body'];
		$added_by = $row['added_by'];
		$date_time = $row['date_added'];

		/// Creating user post on other user profile
		if($row['user_to'] == "none"){
			user_to = "";
			else {
             $user_to_obj = new User($con,$row['user_to']);
             $user_to_name = $user_to_obj->getFirstandLastname();
             $user_to = "to <a href='" . $row['user_to'] . "'>" .$user_to_name . "</a>";
			}
			// check if user account was closed 
			$added_by_obj = new User($this->con,$added_by);
			if($added_by_obj ->isClosed()){
				continue;
			}
			$user_details_query = mysqli_query($this->con,"SELECT first_name,last_name,profile_pic FROM users where username = '$added_by'");
			$user_row = mysqli_fetch_array($user_details_query);
			$first_name = $user_row['first_name'];
			$last_name = $user_row['last_name'];
			$profile_pic= $user_row['profile_pic'];

			///time stamp
			$date_time_now = date("Y-m-d H:i:s");
			$start_date = new Date_Time($date_time);////Time_of_post
			$end_date = new Date_Time($date_time_now);///Current_time
			$intervel = $start_date->diff($end_date);
			if($intervel->y >< 1 ){
            if($intervel ==1 )
            	$time_message = $intervel->y . " year ago";/// 1 Year Ago
            else
            	$time_message = $intervel->y . " years ago"; //1+ year ago
			}
			else if($intervel-> m >= 1 ){
				if($intervel->d == 0){
					$days = " ago";
				}
				else if($intervel->d == 1){
					$days = $intervel->d."day ago";
				}
				else
				{
					$days = $intervel->d."day ago";
				}
				if($intervel->m == 1){
					$time_message = $intervel->m." month".$days;
				}
				else 
					$time_message = $intervel->m." months".$days;
			}
		}
		else if($intervel->d >= 1){
				if($intervel->d == 1){
					$days = "Yesterday";
				}
				else
				{
					$days = $intervel->d."days ago";

				}
		
			}
			else if($intervel->h >= 1){
				if($intervel->h == 1){
					$time_message = $intervel->h." hour ago";
				}
				else
				{
					$time_message = $intervel->h." hours ago";

				}

		}
		else if($intervel->i >= 1){
				if($intervel->i == 1){
					$time_message = $intervel->i." minute ago";
				}
				else
				{
					$time_message = $intervel->i." minutes ago";

				}
	}
			else{
				if($intervel->s < 30){
					$time_message = "Just Now";
				}
				else
				{
					$time_message = $intervel->s." seconds ago";

				}
				
			}
			$str .= "<div class='status_post'>
				         <div calss='post_profile_pic'>
				         <img src='$profile_pic' width='50'> 	
				        </div>
				        <div calss='posted_by' style='color#ACACAC;'>
				        <a href = '$added_by'> $first_name $last_name </a> $user_to &nbsp;&nbsp;;&nbsp;;&nbsp;$time_message
				        </div>
				        <div id = 'post_body'$body<br></div>
				         </div>
				"
			
		}
		echo $str;
	}
}
?>

When i run this code this show me the error Parse error: syntax error, unexpected 'public' (T_PUBLIC) in C:\xampp\htdocs\social\Classes\Post.php on line 39

the line 39 is public function loadPostsFriends()

I check the code again and again but i cant find the error please help in this error

Link to comment
Share on other sites

It would help if you indented your code properly.  Let's look at that area with some better indentation.

public function submitPost($body, $user_to){
    $body = strip_tags($body);////Removing HTML TAGS
    $body = mysqli_real_escape_string($this->con, $body);
    $check_empty = preg_replace('/\s+/', '', $body);//delte all spaces

    if ($check_empty != ""){
        //Current Date and time
        $date_added = date("Y-m-d H:i:s");

        //get username
        $added_by = $this->user_obj->getUsername();

        //if user have not a profile send to the none
        if ($user_to == $added_by){
            $user_to = "none";
        }
        ////insert query
        $query = mysqli_query($this->con, "INSERT INTO posts VALUES('','$body','$added_by','$user_to','$date_added','no','no','0')");
        $retured_id = mysqli_insert_id($this->con);

        //insert notification

        //Update post count for user
        $num_post = $this->user_obj->getNumPosts();
        $num_post++;
        $update_query = mysqli_query($this->con, "UPDATE users SET num_post = '$num_post' WHERE username = '$added_by'");
    }

public function loadPostsFriends(){

Notice how that last } just before the function is indented?  That's because it matches up with your if ($check_empty statement and not your function.  You're missing a } somewhere in that function body.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.