Guest User

news.php

a guest
Aug 1st, 2016
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. <?php
  2.  
  3. class News
  4. {
  5. /**
  6. * Returns single news item with specified id
  7. * @param $id
  8. *
  9. */
  10. public static function getNewsById($id)
  11. {
  12. $id = intval($id);
  13.  
  14. if ($id) {
  15. // $host = 'localhost';
  16. // $dbname = 'mvc_site';
  17. // $user = 'root';
  18. // $password = '';
  19. // $db = new PDO("mysql:host=$host;dbname=$dbname", $user, $password);
  20. $db = DB::getConnection();
  21. $result = $db->query('SELECT * FROM news WHERE ID=' . $id);
  22. $result->setFetchMode(PDO::FETCH_ASSOC);
  23.  
  24. $newsItem = $result->fetch();
  25.  
  26. return $newsItem;
  27. }
  28. }
  29.  
  30. /**
  31. * Returns an array of news items
  32. */
  33. public static function getNewsList()
  34. {
  35. //Запрос в БД
  36. $db = DB::getConnection();
  37.  
  38. $newsList = array();
  39.  
  40. $result = $db->query('SELECT id, title, date, short_content '
  41. . 'FROM news '
  42. . 'ORDER BY date DESC '
  43. . 'LIMIT 10');
  44.  
  45. $i = 0;
  46. while ($row = $result->fetch()) {
  47. $newsList[$i]['id'] = $row['id'];
  48. $newsList[$i]['title'] = $row['title'];
  49. $newsList[$i]['date'] = $row['date'];
  50. $newsList[$i]['short_content'] = $row['short_content'];
  51. $i++;
  52. }
  53.  
  54. return $newsList;
  55.  
  56. }
  57. }
Add Comment
Please, Sign In to add comment