Thursday, 6 September 2018

php - class property that does not return the value

I am wondering my class property $friend_username does not returning its value either it is public.
update
class Feed {
    public static $friend_username;

    // ONLINE FRIENDS LOGIC
    public function online_friends(){
        $friendsHTML = '';
        $countOnlineFriends = '';
        if(GetFriends($GLOBALS['log_username']) != false) {
            $all_friends = GetFriends($GLOBALS['log_username']);
            $orLogic = '';
            foreach($all_friends as $key => $user){
                if(IsBlocked($GLOBALS['log_username'],$user,true) == false){
                    $orLogic .= "username='$user' OR ";
                }
            }
            $orLogic = chop($orLogic, "OR ");
            $sql = "SELECT username, avatar, logged_in FROM users WHERE ($orLogic) AND logged_in = 1";
            $query = mysqli_query($GLOBALS['db_conx'], $sql);
            $friend_loggedIn = array();
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
                $this->friend_username = $row["username"];
                $friend_avatar = $row["avatar"];
                $friend_loggedIn[] = $row["logged_in"];
                $friend_pic =  userImage($this->friend_username,$friend_avatar,'42','42',$link = false,$up = true);
                $friendsHTML .= '<li><a href="#" onClick="chatbox(\''.$this->friend_username.'\',\''.getName($this->friend_username,true).'\');return false;">'.$friend_pic.' '.getName($this->friend_username,true).'</li>';
                $countFriends = count($friend_loggedIn);
                $countOnlineFriends = ($countFriends > 0) ? '<span class="online_friends animated">'.$countFriends.'</span>' : '';
            }
        }else{
            $friendsHTML = 'No friends';
        }
        return "$countOnlineFriends|$friendsHTML";
    }

    public function update_chat() {
        $id = '';
        $messages = '';
        $randUser = '';
        $user = sanitize($this->friend_username);
        $sql = "SELECT * FROM pm_chat WHERE (sender='$GLOBALS[log_username]' AND receiver='$user') OR (sender='$user' AND receiver='$GLOBALS[log_username]') ORDER BY datetime DESC";
        $result = mysqli_query($GLOBALS['db_conx'],$sql) or die(mysqli_error($GLOBALS['db_conx']));
        while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            $user1 = $row['sender'];
            $user2 = $row['receiver'];
            $message = parseData($row['message']);
            $did_read = $row['did_read'];
            $datetime = $row['datetime'];
            if ($user1 != $GLOBALS['log_username']) {
                $randUser = $user1;
            }elseif ($user2 != $GLOBALS['log_username']) {
                $randUser = $user2;
            }
            if ($user1 == $GLOBALS['log_username'] && $user2 != $GLOBALS['log_username']) {
                $messages .= '<li class="row" id="pm_row_'.$id.'"><div class="me">'.$message.'</div></li>';
            }else{
                $messages .= '<li class="row" id="pm_row_'.$id.'">'.userImage($randUser,getAvatar($randUser),28,28,$link = true,$up = true).'<div class="userfrnd">'.$message.'</div></li>';
            }
        }
        return $this->friend_username."$id|$messages|$randUser";
        // this is for ^^^^^^^ testing  purpose
    }
}

here is the other file where I am calling the other class method. And its content-type is text/event-stream
class update_chat extends SSEEvent {
    public function update(){
        //Here's the place to send data
        $feed = new Feed();
        return $feed->update_chat();
    }
    public function check(){
        //Here's the place to check when the data needs update
        return true;
    }
}

Any idea or suggestion why this problem persist ?
thanks in advance.

If you are calling bar() in another file and then creating a new Foo in otherClass, you are not referencing the same instance of Foo. Either make $friend_username static and call it statically
public static $friend_username;

public function update(){
    //Here's the place to send data
    return Foo::$friend_username;
}

or at least make the function static
public static function bar() {}

public function update(){
    //Here's the place to send data
    return Foo::bar();
}

or pass in the instance of Foo to the function
public function update(Foo $Foo){
    //Here's the place to send data
    return $Foo->bar();
}

If you want to call a static method from within the same class, you have to use the self identifier (self::$var)
    class Feed {
    public static $friend_username = array();

    // ONLINE FRIENDS LOGIC
    public function online_friends(){
        $friendsHTML = '';
        $countOnlineFriends = '';
        if(GetFriends($GLOBALS['log_username']) != false) {
            $all_friends = GetFriends($GLOBALS['log_username']);
            $orLogic = '';
            foreach($all_friends as $key => $user){
                if(IsBlocked($GLOBALS['log_username'],$user,true) == false){
                    $orLogic .= "username='$user' OR ";
                }
            }
            $orLogic = chop($orLogic, "OR ");
            $sql = "SELECT username, avatar, logged_in FROM users WHERE ($orLogic) AND logged_in = 1";
            $query = mysqli_query($GLOBALS['db_conx'], $sql);
            $friend_loggedIn = array();
            while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)) {
                array_push(self::$friend_username, $row["username"]);
                $friend_avatar = $row["avatar"];
                $friend_loggedIn[] = $row["logged_in"];
                $friend_pic =  userImage(self::$friend_username,$friend_avatar,'42','42',$link = false,$up = true);
                $friendsHTML .= '<li><a href="#" onClick="chatbox(\''.self::$friend_username.'\',\''.getName(self::$friend_username,true).'\');return false;">'.$friend_pic.' '.getName(self::$friend_username,true).'</li>';
                $countFriends = count($friend_loggedIn);
                $countOnlineFriends = ($countFriends > 0) ? '<span class="online_friends animated">'.$countFriends.'</span>' : '';
            }
        }else{
            $friendsHTML = 'No friends';
        }
        return "$countOnlineFriends|$friendsHTML";
    }

    public function update_chat() {
        $id = '';
        $messages = '';
        $randUser = '';

        $user = Feed::$friend_username;
        foreach ($user as $key => $value) {
            $user[$key] = sanitize($value);
        }

        //I leave it up to you to figure out how you want to deal with the array of users in this next line
        $sql = "SELECT * FROM pm_chat WHERE (sender='$GLOBALS[log_username]' AND receiver='$user') OR (sender='$user' AND receiver='$GLOBALS[log_username]') ORDER BY datetime DESC";
        $result = mysqli_query($GLOBALS['db_conx'],$sql) or die(mysqli_error($GLOBALS['db_conx']));
        while ($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            $user1 = $row['sender'];
            $user2 = $row['receiver'];
            $message = parseData($row['message']);
            $did_read = $row['did_read'];
            $datetime = $row['datetime'];
            if ($user1 != $GLOBALS['log_username']) {
                $randUser = $user1;
            }elseif ($user2 != $GLOBALS['log_username']) {
                $randUser = $user2;
            }
            if ($user1 == $GLOBALS['log_username'] && $user2 != $GLOBALS['log_username']) {
                $messages .= '<li class="row" id="pm_row_'.$id.'"><div class="me">'.$message.'</div></li>';
            }else{
                $messages .= '<li class="row" id="pm_row_'.$id.'">'.userImage($randUser,getAvatar($randUser),28,28,$link = true,$up = true).'<div class="userfrnd">'.$message.'</div></li>';
            }
        }
        return Feed::$friend_username."$id|$messages|$randUser";
        // this is for ^^^^^^^ testing  purpose
    }
}

0 comments:

Post a Comment