Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Tweet
- {
- static private $conn;
- private $id;
- private $userId;
- private $userName;
- private $tweet;
- // This function sets connection for this class to use
- // This function needs to be run on startup
- public static function SetConnection($newConnection)
- {
- self::$conn = $newConnection;
- }
- //this function returns:
- // new Tweet object if new entry was added to table
- // null if there was some problem
- public static function CreateTweet($creatorId, $creatorName, $tweet)
- {
- $sqlStatement = "INSERT INTO Tweets(user_id, tweet) values ($creatorId, '$tweet')";
- if (Tweet::$conn->query($sqlStatement) === TRUE) {
- return new Tweet(Tweet::$conn->insert_id, $creatorId, $creatorName, $tweet);
- }
- //error
- return null;
- }
- //this function return:
- // true if tweet was deleted
- // false if not
- public static function DeleteTweet($toDeleteId)
- {
- $sql = "DELETE FROM Tweets WHERE id = {$toDeleteId}";
- return Tweet::$conn->query($sql) === TRUE && Tweet::$conn->affected_rows > 0;
- }
- public static function GetAllUserTweets($creatorId, $creatorName, $limit = 0)
- {
- $ret = array();
- $sqlStatement = "Select id, tweet, user_id from Tweets where user_id = $creatorId";
- if ($limit > 0) {
- $sqlStatement .= " LIMIT $limit";
- }
- $result = Tweet::$conn->query($sqlStatement);
- if ($result->num_rows > 0) {
- while ($row = $result->fetch_assoc()) {
- $ret[] = new Tweet($row['id'], $row['user_id'], $creatorName, $row['tweet']);
- }
- }
- return $ret;
- }
- public static function GetTweet($id)
- {
- $sqlStatement = "Select user_id, name, tweet from Tweets join Users on Users.id = Tweets.id where id=$id";
- $result = Tweet::$conn->query($sqlStatement);
- if ($result->num_rows > 0) {
- $row = $result->fetch_assoc();
- return new Tweet($id, $row['user_id'], $row['name'], $row['tweet']);
- }
- return null;
- }
- private function __construct($newId, $newUserId, $userName, $newTweet)
- {
- $this->id = $newId + 1;
- $this->userId = $newUserId;
- $this->userName = $userName;
- $this->tweet = $newTweet;
- }
- public function getId()
- {
- return $this->id;
- }
- public function getUserId()
- {
- return $this->userId;
- }
- public function getUserName()
- {
- return $this->userName;
- }
- public function getTweetText()
- {
- return $this->tweet;
- }
- public function setTweetText($newTweet)
- {
- $this->tweet = $newTweet;
- }
- //this function is responsible for saving any changes done to User to database
- public function saveToDB()
- {
- $sql = "UPDATE Tweets SET tweet='{$this->tweet}' WHERE id={$this->id}";
- return Tweet::$conn->query($sql);
- }
- }
Add Comment
Please, Sign In to add comment