Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. class DB {
  2. public $pdo;
  3. private $host = DB_HOST;
  4. private $user = DB_USER;
  5. private $pass = DB_PASS;
  6. private $dbname = DB_NAME;
  7.  
  8. public function __construct()
  9. {
  10. $this->connect();
  11. }
  12.  
  13. private function connect()
  14. {
  15. $options = array(
  16. PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
  17. );
  18. try {
  19. $this->pdo = new PDO("mysql:host=$this->host;dbname=$this->dbname;charset=utf8;", $this->user, $this->pass, $options);
  20. } catch(PDOException $e) {
  21. echo $e->getMessage();
  22. }
  23. }
  24.  
  25. public function __sleep()
  26. {
  27. return array('dsn', 'username', 'password');
  28. }
  29.  
  30. public function __wakeup()
  31. {
  32. $this->connect();
  33. }
  34.  
  35. public function __destruct()
  36. {
  37. $this->connection = null;
  38. $this->pdo = null;
  39. unset($this->pdo);
  40. }
  41.  
  42. // CRUD methods follow including
  43.  
  44. function retrieve($where, $groupBy='', $order_by='') {
  45.  
  46. $query = "SELECT * FROM `$this->table` $where $groupBy $order_by";
  47.  
  48. $q = $this->pdo->prepare($query);
  49. $q->execute();
  50.  
  51. $result = $q->fetchAll(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE,get_class($this));
  52. // was $result = $q->fetchAll(PDO::FETCH_CLASS,get_class($this));
  53. $this->query_log($query);
  54. $q = null;
  55.  
  56. if ($result == 'NULL') {
  57. return false;
  58. } else {
  59. return $result;
  60. }
  61. } // retrieve()
  62.  
  63. if (in_array($_GET['type'], $types)) {
  64. $type = $_GET['type'];
  65. $rsObj = new ReservedSlug;
  66. if ($type == 'artist') {
  67. $obj = new CalendarArtist;
  68. $slugfield = 'urlSlug';
  69. $namefield = 'name';
  70. } else if ($type == 'event') {
  71. $obj = new CalendarEvent;
  72. $slugfield = 'urlSlug';
  73. $namefield = 'name';
  74. } else if ($type == 'location') {
  75. $obj = new Location;
  76. $slugfield = 'UrlSlug';
  77. $namefield = 'LocationName1';
  78. }
  79. $needslug = $obj->retrieve("TRIM(`$namefield`) != '' AND (`$slugfield` = '' OR `$slugfield` IS NULL) LIMIT 0,400");
  80.  
  81. if ($needslug) {
  82. foreach ($needslug as $ns) {
  83. $testslug = slugify($ns->$namefield);
  84. list($reserved) = $rsObj->retrieve("`slug` = '$testslug' AND `type` = '$type'");
  85. if (!$reserved) {
  86. list($test) = $obj->retrieve("`$slugfield` = '$testslug'");
  87. if ($test) {
  88. for ($i = 2; $i < 26; $i++) {
  89. list($test) = $obj->retrieve("`$slugfield` = '$testslug-$i'");
  90. if (!$test) {
  91. $slug = $testslug . '-' . $i;
  92. }
  93. }
  94. } else { // not found in table
  95. $slug = $testslug;
  96. }
  97. } else { // was reserved
  98. $slug = false;
  99. }
  100. echo $ns->$namefield . " gets $slug<p>";
  101. } // foreach needslug
  102. } // if needslug
  103. } // type found in array
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement