Advertisement
Guest User

Untitled

a guest
Oct 13th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.59 KB | None | 0 0
  1. <?php
  2.  
  3. class Trad
  4. {
  5. public $id;
  6. public $count;
  7. }
  8.  
  9. /**
  10. * Created by PhpStorm.
  11. * User: theophy
  12. * Date: 13/10/2017
  13. * Time: 5:38 AM
  14. */
  15. class Traditional1
  16. {
  17. private static $dbname = "playground";
  18. private static $username = "root";
  19. private static $password = "root";
  20. private $db;
  21.  
  22. function __construct($id, $request)
  23. {
  24. $this->db = $this->connect();
  25. if ($request == "add")
  26. $this->like($id);
  27. else
  28. echo $this->getPost($id);
  29. echo "Done";
  30. }
  31.  
  32. private function connect(){...}
  33.  
  34. /**
  35. * @return PDO
  36. */
  37. private function getDb(){ .... return $this->db; }
  38.  
  39. private function like($id)
  40. {
  41. $post = $this->getPost($id);
  42.  
  43. $this->savePost($id, $post->count + 1);
  44. }
  45.  
  46. /**
  47. * this is used to get a post
  48. * @param $id
  49. * @return Trad
  50. */
  51. private function getPost($id)
  52. {
  53. try {
  54. $stmt = $this->getDb()->prepare("select * from traditional1 where id='$id'");
  55. $stmt->setFetchMode(PDO::FETCH_CLASS, "Trad");
  56.  
  57. $stmt->execute();
  58. return $stmt->fetch();
  59.  
  60. } catch (PDOException $ex) {
  61. print $ex->getMessage();
  62. }
  63. }
  64.  
  65. /**
  66. * this is used to perform a simple save of our post
  67. * @param $id
  68. * @param $total
  69. */
  70. private function savePost($id, $total)
  71. {
  72. //form sql
  73. $sql = "update traditional1 set count='$total' where id='$id'";
  74.  
  75. $stmt = $this->getDb()->prepare($sql);
  76. $stmt->execute();
  77. }
  78. }
  79.  
  80. new Traditional1($_GET['id'], $_GET['request']);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement