Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Klasy -> Komantarze
Sh4dow
post
Post #1





Grupa: Zarejestrowani
Postów: 569
Pomógł: 0
Dołączył: 17.08.2003
Skąd: Dąbrowa Górnicza

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


Mam dwa nurtujace mnie pytania.
Czy opłaca sie robić klasy dla komantarzy czy zrobic zwykłą funckje z możliwości includowania do roznych miejsc. Taka funkcja działała by prawie jak klasa ( :?: chyba :?: ).

A jesli na klasach zrobic taki system komantarzy to jak go zrobić :?: nie chodzi mi o podanie gotowego skryptu, tylko o rozwiazanie teoretyczne, czy nowym obiektem ma by komantarz :?: czy moze wszystkie komantarze :?:
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Seth
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 335
Pomógł: 6
Dołączył: 7.03.2002

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


Jako, ze moja propozycja przeszla niezauwazona oto jej implementacja:

  1. CREATE TABLE `test_comments` (
  2. `id` mediumint(8) NOT NULL AUTO_INCREMENT,
  3. `docId` mediumint(8) NOT NULL,
  4. `date` int(11) NOT NULL,
  5. `topic` varchar(30) NOT NULL,
  6. `author` varchar(20) NOT NULL,
  7. `authorId` mediumint(8) NULL,
  8. `content` text NOT NULL,
  9. PRIMARY KEY (`id`),
  10. INDEX ( `docId` )
  11. )


  1. <?php
  2. // Przyklad dzialania /////////////////////////////
  3. define( 'TABLE_PREFIX', 'test_' );
  4.  
  5. mysql_connect( 'host', 'login', 'pass' ) or die( mysql_error() );
  6. mysql_selectdb( 'db' ) or die( mysql_error() );
  7.  
  8. //Dodawanie komentarza
  9. $comment = new Comment();
  10. $comment->docId = 1;
  11. $comment->date = time();
  12. $comment->topic = 'Temat1';
  13. $comment->author = 'Seth';
  14. $comment->authorId = 42;
  15. $comment->content = 'Tresc komentarza 1';
  16. $comment->Insert();
  17. unset( $comment );
  18.  
  19. // Tworzenie instancji listy komentarzy z docId = 1
  20. $comments = new Comments( 1 );
  21.  
  22. print &#092;"---- Lista komentarzy:rn\";
  23. // Wyswietlanie tekstowej reprezentacji listy komentarzy
  24. print $comments->ToString();
  25. print &#092;"---- koniec listyrn\";
  26.  
  27. //Dodawanie komentarza
  28. $comment = new Comment();
  29. $comment->docId = 1;
  30. $comment->date = time();
  31. $comment->topic = 'Temat2';
  32. $comment->author = 'Seth';
  33. $comment->authorId = 42;
  34. $comment->content = 'Tresc komentarza';
  35. $comment->Insert();
  36. unset( $comment );
  37.  
  38. // Odswiezenie listy komentarzy
  39. $comments->Refresh();
  40.  
  41. print &#092;"---- Lista komentarzy:rn\";
  42. // Wyswietlanie tekstowej reprezentacji listy komentarzy
  43. print $comments->ToString();
  44. print &#092;"---- koniec listyrn\";
  45.  
  46. $comment = null;
  47. if ( $comment =& $comments->GetById( 1 ) )
  48. {
  49. $comment->content = 'zmieniona tresc';
  50. $comment->Update();
  51. }
  52.  
  53. print &#092;"---- Lista komentarzy:rn\";
  54. // Wyswietlanie tekstowej reprezentacji listy komentarzy
  55. print $comments->ToString();
  56. print &#092;"---- koniec listyrn\";
  57.  
  58. // Usuwanie wszyskich komentarzy z danego docId
  59. // $comments->RemoveAll();
  60.  
  61. // Koniec przykladu ///////////////////////////////
  62.  
  63.  
  64. class Comment
  65. {
  66. var
  67. $id  = null,
  68. $docId = null,
  69. $date  = null,
  70. $topic = '',
  71. $author  = '',
  72. $authorId = 0,
  73. $content = '';
  74.  
  75. function Comment()
  76. {
  77. $this->date = time();
  78. }
  79.  
  80. function Remove()
  81. {
  82. $sql = &#092;"DELETE FROM \".TABLE_PREFIX.\"comments WHERE id = \".$this->id;
  83.  
  84. if ( mysql_query( $sql ) )
  85. {
  86. return true;
  87. }
  88. else
  89. {
  90. return false;
  91. }
  92. }
  93.  
  94. function Update()
  95. {
  96. $sql = &#092;"UPDATE \".TABLE_PREFIX.\"comments SET
  97. docId = &#092;".$this->docId.\",
  98. date = &#092;".$this->date.\",
  99. topic = '\".$this->topic.\"',
  100. author = '\".$this->author.\"',
  101. authorId = &#092;".$this->authorId.\",
  102. content = '\".$this->content.\"'
  103. WHERE id = &#092;".$this->id;
  104.  
  105. if ( mysql_query( $sql ) )
  106. {
  107. return true;
  108. }
  109. else
  110. {
  111. return false;
  112. }
  113. }
  114.  
  115. function Insert()
  116. {
  117. $sql = &#092;"INSERT INTO \".TABLE_PREFIX.\"comments
  118. ( docId, date, topic, author, authorId, content )
  119. VALUES (
  120. &#092;".$this->docId.\",
  121. &#092;".$this->date.\",
  122. '\".$this->topic.\"',
  123. '\".$this->author.\"',
  124. &#092;".$this->authorId.\",
  125. '\".$this->content.\"' )&#092;";
  126.  
  127. if ( mysql_query( $sql ) )
  128. {
  129. return true;
  130. }
  131. else
  132. {
  133. return false;
  134. }
  135. }
  136.  
  137. function ToString()
  138. {
  139. $varsList = array();
  140. $toString = '';
  141.  
  142. $varsList = get_object_vars( $this );
  143.  
  144. foreach( $varsList as $key => $val )
  145. {
  146. $toString .= $val.&#092;"rn\";
  147. }
  148.  
  149. return $toString;
  150. }
  151. }
  152.  
  153.  
  154.  
  155. class Comments
  156. {
  157. var
  158. $docId = 0,
  159. $list = array();
  160.  
  161.  
  162. function Comments( $docId )
  163. {
  164. $this->docId = $docId;
  165. $this->list = array();
  166.  
  167. $sql = &#092;"SELECT * FROM \".TABLE_PREFIX.\"comments WHERE docId = \".$this->docId.\" ORDER BY date DESC\";
  168.  
  169. if ( $result = mysql_query( $sql ) )
  170. {
  171. while ( $row = mysql_fetch_array( $result ) )
  172. {
  173. $this->list[$row['id']] = new Comment();
  174. $this->list[$row['id']]->id  = $row['id'];
  175. $this->list[$row['id']]->docId = $row['docId'];
  176. $this->list[$row['id']]->date  = $row['date'];
  177. $this->list[$row['id']]->topic = $row['topic'];
  178. $this->list[$row['id']]->author  = $row['author'];
  179. $this->list[$row['id']]->authorId = $row['authorId'];
  180. $this->list[$row['id']]->content = $row['content'];
  181. }
  182.  
  183. return true;
  184. }
  185. else
  186. {
  187. return false;
  188. }
  189. }
  190.  
  191. function Refresh()
  192. {
  193. unset( $this->list ); // update
  194. return $this->Comments( $this->docId );
  195. }
  196.  
  197. function RemoveAll()
  198. {
  199. foreach ( $this->list as $key => $obj )
  200. {
  201. $obj->Remove();
  202. unset( $this->list[$key] );
  203. }
  204. }
  205.  
  206. function &GetById( $id )
  207. {
  208. if ( isset( $this->list[$id] ) )
  209. {
  210. return $this->list[$id];
  211. }
  212. else
  213. {
  214. return false;
  215. }
  216. }
  217.  
  218. function ToString()
  219. {
  220. $toString = '';
  221.  
  222. foreach ( $this->list as $key => $obj )
  223. {
  224. $toString .= $obj->ToString().&#092;"rn\";
  225. }
  226.  
  227. return $toString;
  228. }
  229. }
  230. ?>



Wynik powyzszego przykladu:
Cytat
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
Tresc komentarza 1

---- koniec listy
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
Tresc komentarza 1

2
1
1073938210
Temat2
Seth
42
Tresc komentarza

---- koniec listy
---- Lista komentarzy:
1
1
1073938210
Temat1
Seth
42
zmieniona tresc

2
1
1073938210
Temat2
Seth
42
Tresc komentarza

---- koniec listy


Edit: mozna by jeszcze pokusic sie o min. dodawanie komenatarza przez przekazanie obiektu komentarza do listy i tam dopiero wykonanie Insert'a oraz uaktualnienie lsty bez potrzeby Refresha.
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: 23.12.2025 - 02:45