Skip to content

Updated User.class.php

GopiKrishnan requested to merge (removed):master into master

I added some functions to retrieve data from the db and set the data in the db.

  • setData and getData These functions play an important role, while others call it and retrieve the required data.

Below are the codes I added, check it out.

    public function __construct($username)
    {
        //TODO: Write the code to fetch user data from Database for the given username. If username is not present, throw Exception.

        $this->conn = Database::getConnection();
        $this->username = $username;
        $sql = "SELECT `id` FROM `auth` WHERE `username`= '$username' LIMIT 1";
        $result = $this->conn->query($sql);
        if ($result->num_rows) {
            $row = $result->fetch_assoc();
            $this->id = $row['id']; //Updating this from database
        } else throw new Exception("Username does't exist");
    }

    //this function helps to retrieve data from the database
    private function getData($var)
    {
        if (!$this->conn) {
            $this->conn = Database::getConnection();
        }
        $sql = "SELECT `$var` FROM `users` WHERE `id` = '$this->id'";
        $result = $this->conn->query($sql);
        if ($result->num_rows) {
            return $result->fetch_assoc()["$var"];
        } else  return null;
    }

    //This function helps to  set the data in the database
    private function setData($var, $data)
    {
        if (!$this->conn) {
            $this->conn = Database::getConnection();
        }
        $sql = "UPDATE `users` SET `$var`='$data' WHERE `id`='$this->id';";
        if ($this->conn->query($sql)) {
            return true;
        } else return false;
    }

    public function authenticate()
    {
    }

    public function setBio($bio)
    {
        //TODO: Write UPDATE command to change new bio
        return $this->setData('bio', $bio);
    }

    public function getBio()
    {
        //TODO: Write SELECT command to get the bio.
        return $this->getData('bio');
    }

    public function setAvatar($link)
    {
        return $this->setData('avatar', $link);
    }

    public function getAvatar()
    {
        return $this->getData('avatar');
    }

    public function setFirstname($name)
    {
        return $this->setData("firstname", $name);
    }

    public function getFirstname()
    {
        return $this->getData('firstname');
    }

    public function setLastname($name)
    {
        return $this->setData("lastname", $name);
    }

    public function getLastname()
    {
        return $this->getData('lastname');
    }

    public function setDob($year, $month, $day)
    {
        if (checkdate($month, $day, $year)) { //checking data is valid
            return $this->setData('dob', "$year.$month.$day");
        } else return false;
    }

    public function getDob()
    {
        return $this->getData('dob');
    }

    public function setInstagramlink($link)
    {
        return $this->setData('instagram', $link);
    }

    public function getInstagramlink()
    {
        return $this->getData('instagram');
    }

    public function setTwitterlink($link)
    {
        return $this->setData('twitter', $link);
    }

    public function getTwitterlink()
    {
        return $this->getData('twitter');
    }
    public function setFacebooklink($link)
    {
        return $this->setData('facebook', $link);
    }

    public function getFacebooklink()
    {
        return $this->getData('facebook');
    }
 
Edited by GopiKrishnan

Merge request reports