sanjiisan

Untitled

Sep 7th, 2017
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. <?php
  2.  
  3. class Tweet
  4. {
  5. static private $conn;
  6.  
  7. private $id;
  8. private $userId;
  9. private $userName;
  10. private $tweet;
  11.  
  12. // This function sets connection for this class to use
  13. // This function needs to be run on startup
  14. public static function SetConnection($newConnection)
  15. {
  16. self::$conn = $newConnection;
  17. }
  18.  
  19. //this function returns:
  20. // new Tweet object if new entry was added to table
  21. // null if there was some problem
  22. public static function CreateTweet($creatorId, $creatorName, $tweet)
  23. {
  24. $sqlStatement = "INSERT INTO Tweets(user_id, tweet) values ($creatorId, '$tweet')";
  25. if (Tweet::$conn->query($sqlStatement) === TRUE) {
  26. return new Tweet(Tweet::$conn->insert_id, $creatorId, $creatorName, $tweet);
  27. }
  28. //error
  29. return null;
  30. }
  31.  
  32. //this function return:
  33. // true if tweet was deleted
  34. // false if not
  35. public static function DeleteTweet($toDeleteId)
  36. {
  37. $sql = "DELETE FROM Tweets WHERE id = {$toDeleteId}";
  38.  
  39. return Tweet::$conn->query($sql) === TRUE && Tweet::$conn->affected_rows > 0;
  40. }
  41.  
  42.  
  43. public static function GetAllUserTweets($creatorId, $creatorName, $limit = 0)
  44. {
  45. $ret = array();
  46. $sqlStatement = "Select id, tweet, user_id from Tweets where user_id = $creatorId";
  47. if ($limit > 0) {
  48. $sqlStatement .= " LIMIT $limit";
  49. }
  50. $result = Tweet::$conn->query($sqlStatement);
  51. if ($result->num_rows > 0) {
  52. while ($row = $result->fetch_assoc()) {
  53. $ret[] = new Tweet($row['id'], $row['user_id'], $creatorName, $row['tweet']);
  54. }
  55. }
  56. return $ret;
  57. }
  58.  
  59. public static function GetTweet($id)
  60. {
  61. $sqlStatement = "Select user_id, name, tweet from Tweets join Users on Users.id = Tweets.id where id=$id";
  62. $result = Tweet::$conn->query($sqlStatement);
  63. if ($result->num_rows > 0) {
  64. $row = $result->fetch_assoc();
  65. return new Tweet($id, $row['user_id'], $row['name'], $row['tweet']);
  66. }
  67. return null;
  68. }
  69.  
  70. private function __construct($newId, $newUserId, $userName, $newTweet)
  71. {
  72. $this->id = $newId + 1;
  73. $this->userId = $newUserId;
  74. $this->userName = $userName;
  75. $this->tweet = $newTweet;
  76. }
  77.  
  78. public function getId()
  79. {
  80. return $this->id;
  81. }
  82.  
  83. public function getUserId()
  84. {
  85. return $this->userId;
  86. }
  87.  
  88. public function getUserName()
  89. {
  90. return $this->userName;
  91. }
  92.  
  93. public function getTweetText()
  94. {
  95. return $this->tweet;
  96. }
  97.  
  98. public function setTweetText($newTweet)
  99. {
  100. $this->tweet = $newTweet;
  101. }
  102.  
  103. //this function is responsible for saving any changes done to User to database
  104. public function saveToDB()
  105. {
  106. $sql = "UPDATE Tweets SET tweet='{$this->tweet}' WHERE id={$this->id}";
  107. return Tweet::$conn->query($sql);
  108. }
  109. }
Add Comment
Please, Sign In to add comment