Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [SKRYPT] Ocena skryptu do dodawania/usuwania/edytowania artykułów na blogu
lewsky
post
Post #1





Grupa: Zarejestrowani
Postów: 1
Pomógł: 0
Dołączył: 15.06.2016

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


Witam, dopiero co uczę się programować obiektowo, jest te mój pierwszy skrypt napisany tym sposobem. Będę wdzięczny za konstruktywną krytykę, będę wrzucał poprawione fragmenty kodu właśnie tutaj.

Obsługa artykułów articleClass.php
  1. <?php
  2. //THIS CLASS VALIDATES AND INSERTS
  3. //ARTICLES INTO DATABASE
  4. class AddArticle {
  5.  
  6. public $title;
  7. public $article;
  8. public $img;
  9. public $flag;
  10. public $alert;
  11.  
  12. function __construct(){
  13. $this->title = $_POST['title'];
  14. $this->article = $_POST['article'];
  15. $this->img = $_POST['imgurl'];
  16. $this->flag = false;
  17. $this->alert = '';
  18. } // ./__construct
  19.  
  20. // IF SOMETHING GOES WRONG THIS METHOD IS STARTING
  21. public function echoAlert(){
  22. echo $this->alert;
  23. }
  24.  
  25. // VALIDATES LENGTH OF TEXTS AND IMG SIZES
  26. public function checkInputs(){
  27. //TEXTS VALIDATION
  28. $explode = explode(" ", $this->article);
  29. if(strlen($this->title) < 10){
  30. $this->flag = false;
  31. $this->alert ="Długość tytułu musi wynościć przynajmniej 10 znaków";
  32.  
  33. } elseif (count($explode)<20) {
  34. $this->flag = false;
  35. $this->alert = "Treśc artykułu musi wynosić przynajmniej 20 słów";
  36.  
  37. } else {
  38. $this->flag = true;
  39. }
  40.  
  41. //IMG VALIDATION
  42. // Remove all illegal characters from a url
  43. $url = filter_var($this->img, FILTER_SANITIZE_URL);
  44.  
  45. // Validate url
  46. if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
  47. list($width, $height) = getimagesize($url);
  48. if($width<1900 || $height<800){
  49. $this->flag=false;
  50. $this->alert = "Minimalne wymiary to 1900x800";
  51. } else{
  52. $this->flag=true;
  53. }
  54. } else {
  55. $this->flag=false;
  56. $this->alert = "Podaj poprawny adres url ";
  57. }
  58. }// ./checkInputs()
  59.  
  60.  
  61. // THIS METHOD INSERTS ARTICLE INTO DB
  62. // USING OBJECT DbAction INSIDE db.php FILE
  63. public function insertArticle(){
  64.  
  65. if($this->flag==true){
  66. require_once 'db.php';
  67. $db = new DbAction();
  68. $db->connection();
  69. $db->getResult("INSERT INTO `articles`(`title`,`imgurl`, `article`, date) VALUES ('$this->title','$this->img','$this->article', CURRENT_DATE)");
  70. $this->alert = "Pomyślnie dodano post";
  71. }
  72. }// ./insertArticle
  73. } // ./AddArticle
  74.  
  75.  
  76. //THIS CLASS GETS ARTICLES FROM DB
  77. //AND DISPLAYS THEM IN INDEX.PHP FILE
  78. class DisplayArticles{
  79.  
  80. public $articleId;
  81. public $articleTitle;
  82. public $articleDate;
  83. public $articleText;
  84. public $noArticles;
  85.  
  86. function __construct(){
  87.  
  88. $this->articleId = '';
  89. $this->articleTitle = '';
  90. $this->articleDate='';
  91. $this->articleText ='';
  92. $this->noArticles = '';
  93. } // ./__construct
  94.  
  95. //THIS METHOD GETS ARTICLES FROM DB
  96. //AND CLASS ANOTHER METHOD THAT DISPLAYS THEM
  97. public function getArticles(){
  98.  
  99. require_once 'db.php';
  100. $db = new DbAction();
  101. $db->connection();
  102. $db->getResult("SELECT * FROM articles ORDER BY id DESC");
  103. $numRow = $db->rowNumber();
  104.  
  105. if($numRow==0){$this->noArticles="Brak postów";}
  106.  
  107.  
  108. //call echoArticles as much as number of articles in DB
  109. while($row = $db->result->fetch_assoc()){
  110. $this->echoAll($row['id'], $row['title'], $row['date'], $row['article']);
  111. }
  112. }// ./getArticles
  113.  
  114.  
  115. //THIS METHOD DISPLAYS ALL OF ARTICLES INSIDE INDEX.PHP
  116. public function echoAll($id, $title, $date, $article){
  117.  
  118. $this->articleId = $id;
  119. $this->articleTitle = $title;
  120. $this->articleDate = $date;
  121. $this->articleText = $article;
  122. $articleShort="";
  123.  
  124. echo '<div class="post-preview">';
  125. echo '<a href="post.php?id='.$this->articleId .'"> ';
  126. echo '<h2 class="post-title">'.$this->articleTitle.'</h2>';
  127. echo '</a><i>Posted on '.$this->articleDate.'</i>';
  128. echo '</a></div>';
  129. echo '<p>';
  130. $articleExplode = explode(" ",$this->articleText);
  131. for ($i=0; $i<=19; $i++){
  132. echo $articleExplode[$i];
  133. if($i!=19){
  134. echo " ";
  135. }
  136. }
  137. echo "...";
  138. echo'</p>';
  139. echo '<hr>';
  140. }// ./echoAll
  141. } // ./DisplayArticle
  142.  
  143.  
  144. //THIS CLASS DISPLAY SINGLE FULL ARTICLE
  145. //INSIDE post.php FILE
  146. class PrintArticle {
  147.  
  148. public $articleId;
  149. public $articleTitle;
  150. public $articleDate;
  151. public $articleText;
  152.  
  153. function __construct() {
  154. $this->articleId='';
  155. $this->articleTitle='';
  156. $this->articleDate='';
  157. $this->articleImage='';
  158. $this->articleIText='';
  159. }
  160.  
  161. //THIS METHOD DISPLAYS LONG VERSION OF ARTICLE IN FILE POST.PHP
  162. public function getArticle(){
  163.  
  164. require_once 'db.php';
  165. $this->articleId = $_GET['id'];
  166. $db = new DbAction();
  167. $db->connection();
  168. $db->getResult("SELECT title, date, article, imgurl FROM articles where id='$this->articleId'");
  169. $row = $db->result->fetch_assoc();
  170.  
  171. $this->articleTitle = $row['title'];
  172. $this->articleDate = $row['date'];
  173. $this->articleImage = $row['imgurl'];
  174. $this->articleText = $row['article'];
  175. }
  176. }
  177.  
  178.  
  179. //THIS CLASS DISPLAYS OVERWIEV OF ARTICLES IN ADMIN PANEL
  180. class PanelEditing{
  181.  
  182. public $articleId;
  183. public $articleTitle;
  184. public $articleDate;
  185. public $articleText;
  186. public $noArticles;
  187. public $alert;
  188. function __construct() {
  189. $this->articleId='';
  190. $this->articleTitle='';
  191. $this->articleDate='';
  192. $this->articleIText='';
  193. $this->noArticles='';
  194. $this->alert='';
  195. }
  196.  
  197. //THIS METHOD GET ARTICLES FROM DB
  198. //AND CALLS ANOTHER METHOD THAT DISPLAYS THEm
  199. public function getArticles(){
  200.  
  201. require_once '../db.php';
  202. $db = new DbAction();
  203. $db->connection();
  204. $db->getResult("SELECT * FROM articles ORDER BY id DESC");
  205. $numRow = $db->rowNumber();
  206.  
  207. if($numRow==0){$this->noArticles="Brak postów";}
  208.  
  209. //call echoArticles as much as number of articles in DB
  210. while($row = $db->result->fetch_assoc()){
  211. $this->printOverwiev($row['id'], $row['title'], $row['date'], $row['article']);
  212. }
  213. }// ./getAricles
  214.  
  215. public function printOverwiev($id, $title, $date, $article){
  216.  
  217. $this->articleId = $id;
  218. $this->articleTitle = $title;
  219. $this->articleDate = $date;
  220. $this->articleText = $article;
  221.  
  222. echo'<form method="post" action="delete.php">';
  223. echo'<input type="hidden" name="id" value="'.$this->articleId.'">';
  224. echo'<div class="row">';
  225. echo'<div class="col-md-9">';
  226. echo'<div class="panel panel-default">';
  227. echo'<div class="panel-heading">';
  228. echo'<strong>'.$this->articleTitle.'</strong></br>';
  229. echo'<i>Posted on: '.$this->articleDate.'</i>';
  230. echo'</div>';
  231. echo'<div class="panel-body">'.$this->articleText.'</div>';
  232. echo'<div class="panel-footer">';
  233. echo'<a class="btn btn-default"; href="editArticle.php?id='.$this->articleId.'" style="margin-right: 4px;">Edytuj</a>';
  234. echo'<input type="submit" value="Usuń" onClick="return confirm(\'Na pewno chcesz usunąć ten post?\')"
  235. class="btn btn-danger" />';
  236.  
  237. echo'</div>
  238. </div>
  239. </div>
  240. </form>
  241. </div>';
  242.  
  243. }
  244.  
  245. public function deleteArticle($postId){
  246. if(isset($postId)){
  247. require_once '../db.php';
  248. $db = new DbAction();
  249. $db->connection();
  250. $db->getResult("DELETE FROM `articles` WHERE id ='$postId' ");
  251.  
  252. }
  253. }
  254. } // ./PanelEditing
  255.  
  256.  
  257. //THIS CLASS UPDATES ARTICLES
  258. class UpdateArticles{
  259.  
  260. public $articleId;
  261. public $articleTitle;
  262. public $articleText;
  263.  
  264. function __construct(){
  265. $this->articleId = $_GET['id'];
  266. $this->articleTitle = $_POST['title'];
  267. $this->articleText = $_POST['article'];
  268. }
  269.  
  270. public function updateArticle(){
  271. require_once 'db.php';
  272. $db = new DbAction();
  273. $db->connection();
  274. $db->getResult("UPDATE `articles` SET `title`='$this->articleTitle',`article`='$this->articleText' WHERE `id`=$this->articleId");
  275. }// ./updateArticle
  276.  
  277. }// ./UpdateArticle


Obsługa bazy danych db.php
  1. <?php
  2. class DbAction{
  3. public $query;
  4. public $result;
  5. public $connection;
  6. public $numRows;
  7. public $row;
  8. private $host;
  9. private $db_user;
  10. private $db_password;
  11. private $db_name;
  12.  
  13.  
  14. //CONSTRUCTOR
  15. public function __construct(){
  16. $this->connection;
  17. $this->query = '';
  18. $this->result = '';
  19. $this->numRows = '';
  20. $this->row = '';
  21. $this->host = "localhost";
  22. $this->db_user = "root";
  23. $this->db_password = "";
  24. $this->db_name = "cms";
  25. } // ./__construct
  26.  
  27.  
  28. //STARTS CONNECTION
  29.  
  30. public function connection(){
  31. $this->connection = new mysqli($this->host, $this->db_user, $this->db_password, $this->db_name) ;
  32. if($this->connection == true){
  33. return true;
  34. } else {
  35. return false;
  36. }
  37. }// ./connection
  38.  
  39.  
  40. //RETURNS RESULT
  41.  
  42. public function getResult($query){
  43. if ($this->result = $this->connection->query($query)) {
  44. return $this->result;
  45. } else{
  46. return false;
  47. }
  48. }// ./getResult
  49.  
  50.  
  51. //RETURNS NUMBER OF ROW
  52. public function rowNumber(){
  53. if($this->numRows = $this->result->num_rows){
  54. return $this->numRows;
  55. } else{
  56. return false;
  57. }
  58. }
  59.  
  60. //RETURNS NUMBER OF ROW
  61. public function row(){
  62.  
  63. if($this->row = $this->result->fetch_assoc()){
  64. return $this->row;
  65. } else {
  66. return false;
  67. }
  68. }
  69.  
  70. }// ./DbAction
  71.  
  72.  
  73. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
mrc
post
Post #2





Grupa: Zarejestrowani
Postów: 160
Pomógł: 27
Dołączył: 22.09.2008
Skąd: Tarnów

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


  1. //THIS CLASS VALIDATES AND INSERTS
  2. //ARTICLES INTO DATABASE


Po 1. Komentarz zbędny.
Po 2. Jeżeli jedna klasa ma dwie odpowiedzialności (robi dwie rzeczy), to rozbij ją na dwie klasy, które zajmują się jedną rzeczą.
Po 3. Nie uzupełniaj kodu komentarzami, które opisują coś, co już zostało opisane kodem.
Po 4.
  1. require_once 'db.php';


Nie lepiej użyć autoloadera?

Po 5. Brzydko formatujesz kod.
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: 14.10.2025 - 00:12