Guest User

Untitled

a guest
Apr 20th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. <?php
  2. /*
  3. * database.class.php
  4. */
  5. class database {
  6. static private $obj = NULL; // storage for a database class instance
  7. private $hostname = 'localhost';
  8. private $username = 'root';
  9. private $password = 'root';
  10. private $database = 'test';
  11. private $db_connection; // storage for a mysql resource identifier
  12.  
  13. function __construct() {
  14. // setup the database connection on construct
  15. $this->db_connection = mysql_connect($this->hostname, $this->username, $this->password, true);
  16. mysql_select_db($this->database, $this->db_connection);
  17. }
  18.  
  19. static function getInstance() {
  20. // return our instance of the database class, create it if it does not already exist
  21. if (self::$obj == NULL) {
  22. self::$obj = new database;
  23. }
  24. return self::$obj;
  25. }
  26.  
  27. public function query($query) {
  28. return mysql_query($query, $this->db_connection);
  29. }
  30. }
  31. ?>
  32.  
  33. <?php
  34. /*
  35. * news.class.php
  36. */
  37. class news {
  38. function post($title, $content) {
  39. return '<a href="#"><h2>'.$title.'</h2></a>'.
  40. '<p>'.$content.'</p>';
  41. }
  42.  
  43. function show($amount) {
  44. $query = database::getInstance()->query('SELECT id, date, title, content FROM archive ORDER BY date DESC LIMIT '.$amount);
  45. while ($fetch = mysql_fetch_array($query, MYSQL_ASSOC)) {
  46. return '<a id="'.$fetch['id'].'"><h2>'.$fetch['title'].'</h2></a>'.
  47. '<p>'.$fetch['content'].'</p>';
  48. }
  49. return '<p>show '.$amount.'</p>';
  50. }
  51. }
  52. ?>
Add Comment
Please, Sign In to add comment