Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Konrad Pilch
Konrad Pilch
2,435 Points

Displaying user name when logged in.

HI,

So Im making a site in PHP OOP and so far so good but now that the user can log in, i want him to ahve his name shown.

I managed to pull the data from the database of pictures that every user can see but i cant pull the data with the same method, the same way of the user details.

And im thinking, do i need to set this in session something?

Heres my code:

top-nav.php

<?php 

$users = User::find_all();

 ?>

<div class="navbar-header">
    <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-ex1-collapse">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
    </button>
    <a class="navbar-brand" href="index.php">Visit Homepage</a>
</div>

<!-- Top Menu Items -->
<ul class="nav navbar-right top-nav">
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-envelope"></i> <b class="caret"></b></a>
        <ul class="dropdown-menu message-dropdown">
            <li class="message-preview">
                <a href="#">
                    <div class="media">
                        <span class="pull-left">
                            <img class="media-object" src="http://placehold.it/50x50" alt="">
                        </span>
                        <div class="media-body">
                            <h5 class="media-heading">
                                <strong>John Smith</strong>
                            </h5>
                            <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                            <p>Lorem ipsum dolor sit amet, consectetur...</p>
                        </div>
                    </div>
                </a>
            </li>
            <li class="message-preview">
                <a href="#">
                    <div class="media">
                        <span class="pull-left">
                            <img class="media-object" src="http://placehold.it/50x50" alt="">
                        </span>
                        <div class="media-body">
                            <h5 class="media-heading">
                                <strong>John Smith</strong>
                            </h5>
                            <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                            <p>Lorem ipsum dolor sit amet, consectetur...</p>
                        </div>
                    </div>
                </a>
            </li>
            <li class="message-preview">
                <a href="#">
                    <div class="media">
                        <span class="pull-left">
                            <img class="media-object" src="http://placehold.it/50x50" alt="">
                        </span>
                        <div class="media-body">
                            <h5 class="media-heading">
                                <strong>John Smith</strong>
                            </h5>
                            <p class="small text-muted"><i class="fa fa-clock-o"></i> Yesterday at 4:32 PM</p>
                            <p>Lorem ipsum dolor sit amet, consectetur...</p>
                        </div>
                    </div>
                </a>
            </li>
            <li class="message-footer">
                <a href="#">Read All New Messages</a>
            </li>
        </ul>
    </li>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-bell"></i> <b class="caret"></b></a>
        <ul class="dropdown-menu alert-dropdown">
            <li>
                <a href="#">Alert Name <span class="label label-default">Alert Badge</span></a>
            </li>
            <li>
                <a href="#">Alert Name <span class="label label-primary">Alert Badge</span></a>
            </li>
            <li>
                <a href="#">Alert Name <span class="label label-success">Alert Badge</span></a>
            </li>
            <li>
                <a href="#">Alert Name <span class="label label-info">Alert Badge</span></a>
            </li>
            <li>
                <a href="#">Alert Name <span class="label label-warning">Alert Badge</span></a>
            </li>
            <li>
                <a href="#">Alert Name <span class="label label-danger">Alert Badge</span></a>
            </li>
            <li class="divider"></li>
            <li>
                <a href="#">View All</a>
            </li>
        </ul>
    </li>
    <li class="dropdown">
        <a href="#" class="dropdown-toggle" data-toggle="dropdown"><i class="fa fa-user"></i><?php echo $users->first_name; ?><b class="caret"></b></a>
        <ul class="dropdown-menu">
            <li>
                <a href="#"><i class="fa fa-fw fa-user"></i> Profile</a>
            </li>
            <li>
                <a href="#"><i class="fa fa-fw fa-envelope"></i> Inbox</a>
            </li>
            <li>
                <a href="#"><i class="fa fa-fw fa-gear"></i> Settings</a>
            </li>
            <li class="divider"></li>
            <li>
                <a href="logout.php"><i class="fa fa-fw fa-power-off"></i> Log Out</a>
            </li>
        </ul>
    </li>
</ul>

session.php

<?php 



class Session {


    private $signed_in = false;
    public $user_id;
    public $count;
    public $message;

    public $first_name;




    function __construct() {
        session_start();
        $this->visitor_count();
        $this->check_the_login();
        $this->check_message();
        $this->first_name;
    }

        public function message($msg="") {

        if(!empty($msg)) {

            $_SESSION['message'] = $msg;



        } else {

            return $this->message;
        }


       }





        private function check_message(){

        if(isset($_SESSION['message'])) {

        $this->message = $_SESSION['message'];
        unset($_SESSION['message']);

        } else {

            $this->message = "";
        }


     }







    public function visitor_count() {

        if(isset($_SESSION['count'])) {

            return $this->count = $_SESSION['count']++;

        } else {

            return $_SESSION['count'] = 1;


        }



    }


    public function is_signed_in() {

        return $this->signed_in;
    }


    public function login($user) {

    if($user) {

        $this->user_id = $_SESSION['user_id'] = $user->id;
        $this->signed_in = true;
        $this->first_name = $_SESSION['first_name'] = $user->first_name;
    }


    }

    public function logout() {

    unset($_SESSION['user_id']);
    unset($this->user_id);
    $this->signed_in = false;


    }



    private function check_the_login() {

    if(isset($_SESSION['user_id'])) {

    $this->user_id = $_SESSION['user_id'];
    $this->signed_in = true;

    } else {

        unset($this->user_id);
        $this->signed_in = false;

    }


 }



}

$session = new Session();
$message = $session->message();





 ?>

db_object.php

<?php

class Db_object
{   

    //protected static $db_table = "users";



    public static function find_all() 
    {

        return static::find_by_query("SELECT * FROM " . static::$db_table . " ");

    }


    public static function find_by_id($user_id) 
    {
        global $database;

        $the_result_array = static::find_by_query("SELECT * FROM " . static::$db_table . " WHERE id = $user_id LIMIT 1");

        return !empty($the_result_array) ? array_shift($the_result_array) : false;

    }


    public static function find_by_query($sql) 
    {
        global $database;

        $result_set = $database->query($sql);
        $the_object_array = array();

        while($row = mysqli_fetch_array($result_set)) {
            $the_object_array[] = static::instantation($row);
        }

        return $the_object_array;


    }

    public static function instantation($the_record) 
    {

        $calling_class = get_called_class();

        $the_object = new static;

        foreach ($the_record as $the_attribute => $value) {

            if($the_object->has_the_attribute($the_attribute)) {

                $the_object->$the_attribute = $value;

            }

        }

        return $the_object;
    }


    private function has_the_attribute($the_attribute) 
    {

        $object_properties = get_object_vars($this);

        return array_key_exists($the_attribute, $object_properties);

    }

    public function save() 
    {

    return isset($this->id) ? $this->update() : $this->create();

    }


    protected function properties() 
    {

        $properties = array();

        foreach (static::$db_table_fields as $db_fieled) {

            if(property_exists($this, $db_fieled)) {

                $properties[$db_fieled] = $this->$db_fieled;

            }

        }

        return $properties;

    }


    protected function clean_properties()
    {

        global $database;

        $clean_properties = array();

        foreach ($this->properties() as $key => $value) {

            $clean_properties[$key] = $database->escape_string($value);

        }

        return $clean_properties;

    }


    public function create() 
    {
        global $database;

        $properties = $this->properties();

        $sql = "INSERT INTO " .static::$db_table . "(" . implode(",", array_keys($properties)) . ")" ;
        $sql .= "VALUES ('". implode("','", array_values($properties)) ."')";


        if($database->query($sql)) {

            $this->id = $database->the_insert_id();
            return true;

        } else {

            return flase;

        }

    } //create method


    public function update() 
    {
        global $database;


        $properties = $this->properties();

        $properties_pairs = array();

        foreach ($properties as $key => $value) {
            $properties_pairs[] = "{$key}='{$value}'";
        }

        $sql = "UPDATE " .static::$db_table . " SET ";
        $sql .= implode(", ", $properties_pairs);
        $sql .= " WHERE id = " . $database->escape_string($this->id);

        $database->query($sql);

        return (mysqli_affected_rows($database->connection) == 1) ? true : false;

    } //End of Update Method


    public function delete() 
    {
        global $database;

        $sql = "DELETE FROM " .static::$db_table . " ";
        $sql .= "WHERE id=" . $database->escape_string($this->id);
        $sql .= " LIMIT 1";

        $database->query($sql);

        return (mysqli_affected_rows($database->connection) == 1) ? true : false;

    } // End of Delete


}

1 Answer

The way would probably be; Once the user logs in, store there username in a session variable (See your other thread). It'll make it a little easier for you to then display it anywhere and make calls to the database using the session variable.

Konrad Pilch
Konrad Pilch
2,435 Points

It worked :) $user = User::find_all();

<?php echo $_SESSION["first_name"]; ?>

Cool, im getting to like this.

Thanks

You're welcome, happy coding :)

Konrad Pilch
Konrad Pilch
2,435 Points

I don't even need the static method or whtever it's called ;d

COuld you help me with this please? https://teamtreehouse.com/community/how-to-make-permissions