Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Ocena skryptu i wskazanie błędów
tonapewno
post
Post #1





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 5.11.2009

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


chciałbym, aby ktoś chętny wystawił ocenę mojego skryptu tzn. chcę się dowiedzieć co w moim kodzie jest źle napisane lub ewentualnie jak można lepiej coś napisać, to jest mój pierwszy tak projekt który wykonałem dla własnego użytku, jak można się domyśleć jest to skrypt prostego bloga, oto on:
  1. <?php
  2. include('bbcode.php');
  3.  
  4. class user{
  5. function connectAndSelect(){
  6. mysql_connect("localhost", "user", "password") or die("Nie można połączyć z serwerem!");
  7. mysql_select_db("db_blog") or die("Nie wybrano bazy danych!");
  8. mysql_set_charset('utf8');
  9. }
  10.  
  11.  
  12. function showArticles(){
  13. $query = "SELECT id_articles, title, description FROM tbl_articles ORDER BY id_articles DESC";
  14. $results = mysql_query($query) or die(mysql_error());
  15.  
  16. function numberComm($id){
  17. $query = "SELECT count(id_comments) as number ,id_articles FROM tbl_comments WHERE id_articles = '".$id."' GROUP BY id_articles";
  18. $result = mysql_query($query);
  19. $num = mysql_fetch_assoc($result);
  20.  
  21. if($num['number'] == 0)
  22. {
  23. return "0";
  24. } else {
  25. return $num['number'];
  26. }
  27. }
  28.  
  29. while($art = mysql_fetch_assoc($results))
  30. {
  31. include('art_style.php');
  32. }
  33.  
  34. }
  35.  
  36. function showArticlesToId($id){
  37. $query = "SELECT id_articles, title, content FROM tbl_articles WHERE id_articles='".$id."'";
  38. $results = mysql_query($query) or die(mysql_error());
  39.  
  40. while($art = mysql_fetch_assoc($results))
  41. {
  42. include('art_id_style.php');
  43. }
  44. }
  45.  
  46. function showComments($id) {
  47. $query = "SELECT id_comments, who_add, content, date FROM tbl_comments WHERE id_articles='".$id."' ORDER BY id_articles DESC";
  48. $results = mysql_query($query) or die(mysql_error());
  49.  
  50. while($art = mysql_fetch_assoc($results))
  51. {
  52. include('dark_comments_id_style.php');
  53. }
  54. }
  55.  
  56. function topArticle(){
  57. $query = "SELECT art.id_articles, art.title, com.id_articles, count( com.id_articles ) as count_id
  58. FROM tbl_articles AS art, tbl_comments AS com
  59. WHERE art.id_articles = com.id_articles
  60. GROUP BY com.id_articles ORDER BY count_id DESC
  61. LIMIT 0 , 5 ";
  62. $result = mysql_query($query);
  63.  
  64. while($art = mysql_fetch_assoc($result))
  65. {
  66. include("topArticles.php");
  67. }
  68. }
  69.  
  70. function allArticles(){
  71. $query = "SELECT id_articles, title FROM tbl_articles WHERE id_articles >= 0 ORDER BY id_articles";
  72. $result = mysql_query($query);
  73.  
  74. while($art = mysql_fetch_assoc($result))
  75. {
  76. include("topArticles.php");
  77. }
  78. }
  79.  
  80. function programsList(){
  81. $query = "SELECT name, description, link FROM tbl_programs ";
  82. $result = mysql_query($query);
  83. $yes = mysql_num_rows($result);
  84. if($yes === 0){
  85. print "Nie ma żadnych programów!";
  86. } else {
  87. while($prog = mysql_fetch_assoc($result))
  88. {
  89. include("programs.php");
  90. }
  91. }
  92. }
  93.  
  94. function sendComm($id, $whoAdd, $email, $content){
  95.  
  96. if(empty($email)){
  97. $query = "INSERT INTO tbl_comments VALUES (null, '".$id."','".$whoAdd."', NULL, '".$content."', NOW())";
  98. mysql_query($query) or die(mysql_error());
  99. } else {
  100. $query = "INSERT INTO tbl_comments VALUES (null, '".$id."','".$whoAdd."','".$email."', '".$content."', NOW())";
  101. mysql_query($query) or die(mysql_error());
  102. }
  103. }
  104.  
  105.  
  106. }
  107.  
  108. class admin {
  109.  
  110. function connectAndSelect(){
  111. mysql_connect("localhost", "admin", "password") or die(mysql_error());
  112.  
  113. mysql_select_db("db_blog") or die(mysql_error());
  114. mysql_set_charset('utf8');
  115. }
  116.  
  117. function addArticle($title, $content, $description, $additional){
  118. if(empty($additional)){
  119. $query = "INSERT INTO tbl_articles VALUES (null,'".$title."','".$description."','".$content."', NOW(), null)";
  120. mysql_query($query) or die(mysql_error());
  121. } else {
  122. $query = "INSERT INTO tbl_articles VALUES (null,'".$title."','".$description."','".$content."', NOW(), '".$additional."')";
  123. mysql_query($query) or die(mysql_error());
  124. }
  125. }
  126.  
  127. function showAllArticles(){
  128. $query = "SELECT id_articles, title FROM tbl_articles";
  129. $result = mysql_query($query);
  130.  
  131. while($tab = mysql_fetch_assoc($result)){
  132. include('all_admin.php');
  133. }
  134. }
  135.  
  136. function updateArticle($id, $title, $description, $content, $additional) {
  137. if(empty($additional)){
  138. $query = "UPDATE tbl_articles SET title = '".$title."', description = '".$description."', content = '".$content."', additional = null WHERE id_articles = '".$id."'";
  139. mysql_query($query) or die(mysql_error());
  140. } else {
  141. $query = "UPDATE tbl_articles SET title = '".$title."', description = '".$description."', content = '".$content."', additional = '".$additional."' WHERE id_articles = '".$id."'";
  142. mysql_query($query) or die(mysql_error());
  143. }
  144. }
  145.  
  146. function editArticles($id){
  147. $query = "SELECT id_articles, title, content, description, additional FROM tbl_articles WHERE id_articles = '".$id."'";
  148. $result = mysql_query($query);
  149. global $edit;
  150. $edit = mysql_fetch_assoc($result);
  151. }
  152. }
  153. ?>


z góry wielkie dzięki za zainteresowanie i ewentualną pomoc.

Ten post edytował tonapewno 9.01.2011, 20:29:27
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
ayeo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 1 202
Pomógł: 117
Dołączył: 13.04.2007
Skąd: 127.0.0.1

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


...że co to mówisz jest?


--------------------
Go to the top of the page
+Quote Post
webdice
post
Post #3


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Metoda connectAndSelect w klasie user/admin i do tego zdublowana? Funkcje w metodach? Include w while? Za global pójdziesz do piekła (o ile istnieje winksmiley.jpg).
Go to the top of the page
+Quote Post
Spawnm
post
Post #4





Grupa: Moderatorzy
Postów: 4 069
Pomógł: 497
Dołączył: 11.05.2007
Skąd: Warszawa




Ocena -1/10.
Błąd: skrypt
Go to the top of the page
+Quote Post
r4xz
post
Post #5





Grupa: Zarejestrowani
Postów: 673
Pomógł: 106
Dołączył: 31.12.2008

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


dostal.gif
odsyłam do lektury:
http://phpro.org/tutorials/Object-Oriented...g-with-PHP.html
(lub jakiejkolwiek innej, byle nie to co widać powyżej)


--------------------
Go to the top of the page
+Quote Post
tonapewno
post
Post #6





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 5.11.2009

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


wasze uwagi mi nie pomogły(dlaczego to jest błąd nikt nie napisał), ale czekam na dalszą krytykę, informacje może znajdę sam.
Go to the top of the page
+Quote Post
webdice
post
Post #7


Developer


Grupa: Moderatorzy
Postów: 3 045
Pomógł: 290
Dołączył: 20.01.2007




Przede wszystkim poczytaj o idei programowania obiektowego (z naciskiem na regułę DRY). Łączenie z bazą powinno być zaimplementowane w klasie do tego przeznaczonej, a nie pierwszej lepszej. Zmienne globalne to archaizm nie używany głównie ze względów bezpieczeństwa. Dołączanie plików (include/require) nie jest najszybsze więc wykonywanie tego w pętli nie jest najmądrzejszym rozwiązaniem.
Go to the top of the page
+Quote Post
tonapewno
post
Post #8





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 5.11.2009

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


mam rozumieć, że błędy przedstawione przez webdice wystarczają do tego, aby mój kod był poprawny? wiem, że to dużo błędów, ale zastanawiam się czy jest więcej.
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 21.08.2025 - 01:47