Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klasy do sprawdzenia
tukan
post
Post #1





Grupa: Zarejestrowani
Postów: 73
Pomógł: 0
Dołączył: 19.06.2011

Ostrzeżenie: (0%)
-----


Witam,
Chciałbym Wam zaprezentować 3 "szablony" klas, klasę kategorii, artykułu i pseudo-obsługi bazy danych. Te klasy nie są całkowicie gotowe ( ich funkcjonalność), ale daję je pod Wasze profesjonalne oko, żebyście już na początku mogli mnie pokierować na bardziej poprawne tory (IMG:style_emoticons/default/smile.gif) . Zdaję sobie sprawę, że pewnie te klasy są paskudne, mało funkcjonalne itd. Ale o to właśnie chodzi, żeby się poprawiać (IMG:style_emoticons/default/wink.gif) .
Z góry dzięki za każdą wypowiedź.
Pozdrawiam! (IMG:style_emoticons/default/smile.gif)
  1. <?php
  2. class Database {
  3. public $host = "localhost";
  4. public $dbname = "articles";
  5. public $user = "root";
  6. public $pass = "vertrigo";
  7. public $dbh;
  8. function connect(){
  9.  
  10. try {
  11.  
  12. $this->dbh = new PDO("mysql:host=$this->host; dbname=$this->dbname", $this->user, $this->pass);
  13.  
  14.  
  15. echo "Połączenie nawiązane";
  16. } catch ( PDOException $error){ die("Failed to connect:".$error->getMessage());}
  17.  
  18. }
  19.  
  20. function set_coding($what)
  21. {
  22. $this->dbh->exec("SET NAMES ".$what);
  23. }
  24.  
  25.  
  26. }
  27.  
  28. class Taskplain{
  29.  
  30. public $content;
  31. public $solution;
  32. public $nr_original;
  33. public $id_autor;
  34. public $id_kategoria;
  35. public $points;
  36. public $id_date;
  37. public $link;
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. function __construct($content, $id_autor, $id_kategoria,$id_date,$link ){
  46. $this->content = $content;
  47. $this->id_autor = $id_autor;
  48. $this->id_kategoria = $id_kategoria;
  49. $this->id_date = $id_date;
  50. $this->link = $link;
  51.  
  52. }
  53.  
  54. function add($dbh)
  55. {
  56.  
  57. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  58. $sql = "INSERT INTO task_e( content, id_autor, id_kategoria, link) VALUES (:content, :id_autor, :id_kategoria, :link)";
  59. try {
  60. $q = $dbh->prepare($sql);
  61. }
  62. catch(PDOException $e){ print_r( $e->getTrace() );
  63.  
  64. }
  65. try {
  66. $q->execute(array(':content'=>$this->content,
  67. :id_autor'=>$this->id_autor,
  68. ':id_kategoria'=>$this->id_kategoria,
  69. :link'=>$this->link));
  70. }
  71.  
  72. catch(PDOException $e)
  73. {
  74. print_r( $e->getTrace() );
  75. }
  76. }
  77.  
  78.  
  79. }
  80.  
  81. class Category{
  82. public $id;
  83. public $name;
  84. public $ids = array();
  85. public $names = array();
  86.  
  87.  
  88. function addCategory($name, $dbh){
  89. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  90. $sql = "INSERT INTO category( name) VALUES (:name)";
  91. $q = $dbh->prepare($sql);
  92.  
  93.  
  94. try {
  95. $q->execute(array(':name'=>$this->name));
  96.  
  97. }
  98.  
  99. catch(PDOException $e)
  100. {
  101. print_r( "AddCategory: ".$e->getTrace() );
  102. }
  103. }
  104.  
  105. function __construct($name)
  106. {
  107. $this->name = $name;
  108.  
  109.  
  110. }
  111.  
  112.  
  113. function getCateogory($dbh){ // ta klasa miałaby mi tworzyć tablice ze wszystkimi kategoriami
  114.  
  115. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  116. $q = $dbh->prepare("SELECT * FROM autor");
  117. try{$q->execute();}
  118. catch(PDOException $e) { print_r( "getCategory: ".$e->getTrace() );}
  119.  
  120.  
  121.  
  122. while ($row = $q->fetch(PDO::FETCH_ASSOC))
  123. {
  124. array_push($this->names, $row['name']); // tu chciałbym zastosować lepszy pomysł, zamiast tego while użyć fetch.... z PDO
  125. array_push($this->ids, $row['id']);
  126. }
  127.  
  128.  
  129. }
  130.  
  131.  
  132.  
  133.  
  134. }
  135.  
  136. ?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 23:17