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
<?php
//THIS CLASS VALIDATES AND INSERTS
//ARTICLES INTO DATABASE
class AddArticle {
public $title;
public $article;
public $img;
public $flag;
public $alert;
function __construct(){
$this->title = $_POST['title'];
$this->article = $_POST['article'];
$this->img = $_POST['imgurl'];
$this->flag = false;
$this->alert = '';
} // ./__construct
// IF SOMETHING GOES WRONG THIS METHOD IS STARTING
public function echoAlert(){
}
// VALIDATES LENGTH OF TEXTS AND IMG SIZES
public function checkInputs(){
//TEXTS VALIDATION
$explode = explode(" ", $this->article); if(strlen($this->title) < 10
){ $this->flag = false;
$this->alert ="Długość tytułu musi wynościć przynajmniej 10 znaków";
} elseif (count($explode)<20
) { $this->flag = false;
$this->alert = "Treśc artykułu musi wynosić przynajmniej 20 słów";
} else {
$this->flag = true;
}
//IMG VALIDATION
// Remove all illegal characters from a url
$url = filter_var($this->img, FILTER_SANITIZE_URL);
// Validate url
if (!filter_var($url, FILTER_VALIDATE_URL) === false) {
if($width<1900 || $height<800){
$this->flag=false;
$this->alert = "Minimalne wymiary to 1900x800";
} else{
$this->flag=true;
}
} else {
$this->flag=false;
$this->alert = "Podaj poprawny adres url ";
}
}// ./checkInputs()
// THIS METHOD INSERTS ARTICLE INTO DB
// USING OBJECT DbAction INSIDE db.php FILE
public function insertArticle(){
if($this->flag==true){
require_once 'db.php';
$db = new DbAction();
$db->connection();
$db->getResult("INSERT INTO `articles`(`title`,`imgurl`, `article`, date) VALUES ('$this->title','$this->img','$this->article', CURRENT_DATE)");
$this->alert = "Pomyślnie dodano post";
}
}// ./insertArticle
} // ./AddArticle
//THIS CLASS GETS ARTICLES FROM DB
//AND DISPLAYS THEM IN INDEX.PHP FILE
class DisplayArticles{
public $articleId;
public $articleTitle;
public $articleDate;
public $articleText;
public $noArticles;
function __construct(){
$this->articleId = '';
$this->articleTitle = '';
$this->articleDate='';
$this->articleText ='';
$this->noArticles = '';
} // ./__construct
//THIS METHOD GETS ARTICLES FROM DB
//AND CLASS ANOTHER METHOD THAT DISPLAYS THEM
public function getArticles(){
require_once 'db.php';
$db = new DbAction();
$db->connection();
$db->getResult("SELECT * FROM articles ORDER BY id DESC");
$numRow = $db->rowNumber();
if($numRow==0){$this->noArticles="Brak postów";}
//call echoArticles as much as number of articles in DB
while($row = $db->result->fetch_assoc()){
$this->echoAll($row['id'], $row['title'], $row['date'], $row['article']);
}
}// ./getArticles
//THIS METHOD DISPLAYS ALL OF ARTICLES INSIDE INDEX.PHP
public function echoAll($id, $title, $date, $article){
$this->articleId = $id;
$this->articleTitle = $title;
$this->articleDate = $date;
$this->articleText = $article;
$articleShort="";
echo '<div class="post-preview">'; echo '<a href="post.php?id='.$this->articleId .'"> '; echo '<h2 class="post-title">'.$this->articleTitle.'</h2>'; echo '</a><i>Posted on '.$this->articleDate.'</i>'; $articleExplode = explode(" ",$this->articleText); for ($i=0; $i<=19; $i++){
echo $articleExplode[$i]; if($i!=19){
}
}
}// ./echoAll
} // ./DisplayArticle
//THIS CLASS DISPLAY SINGLE FULL ARTICLE
//INSIDE post.php FILE
class PrintArticle {
public $articleId;
public $articleTitle;
public $articleDate;
public $articleText;
function __construct() {
$this->articleId='';
$this->articleTitle='';
$this->articleDate='';
$this->articleImage='';
$this->articleIText='';
}
//THIS METHOD DISPLAYS LONG VERSION OF ARTICLE IN FILE POST.PHP
public function getArticle(){
require_once 'db.php';
$this->articleId = $_GET['id'];
$db = new DbAction();
$db->connection();
$db->getResult("SELECT title, date, article, imgurl FROM articles where id='$this->articleId'");
$row = $db->result->fetch_assoc();
$this->articleTitle = $row['title'];
$this->articleDate = $row['date'];
$this->articleImage = $row['imgurl'];
$this->articleText = $row['article'];
}
}
//THIS CLASS DISPLAYS OVERWIEV OF ARTICLES IN ADMIN PANEL
class PanelEditing{
public $articleId;
public $articleTitle;
public $articleDate;
public $articleText;
public $noArticles;
public $alert;
function __construct() {
$this->articleId='';
$this->articleTitle='';
$this->articleDate='';
$this->articleIText='';
$this->noArticles='';
$this->alert='';
}
//THIS METHOD GET ARTICLES FROM DB
//AND CALLS ANOTHER METHOD THAT DISPLAYS THEm
public function getArticles(){
require_once '../db.php';
$db = new DbAction();
$db->connection();
$db->getResult("SELECT * FROM articles ORDER BY id DESC");
$numRow = $db->rowNumber();
if($numRow==0){$this->noArticles="Brak postów";}
//call echoArticles as much as number of articles in DB
while($row = $db->result->fetch_assoc()){
$this->printOverwiev($row['id'], $row['title'], $row['date'], $row['article']);
}
}// ./getAricles
public function printOverwiev($id, $title, $date, $article){
$this->articleId = $id;
$this->articleTitle = $title;
$this->articleDate = $date;
$this->articleText = $article;
echo'<form method="post" action="delete.php">'; echo'<input type="hidden" name="id" value="'.$this->articleId.'">'; echo'<div class="col-md-9">'; echo'<div class="panel panel-default">'; echo'<div class="panel-heading">'; echo'<strong>'.$this->articleTitle.'</strong></br>'; echo'<i>Posted on: '.$this->articleDate.'</i>'; echo'<div class="panel-body">'.$this->articleText.'</div>'; echo'<div class="panel-footer">'; echo'<a class="btn btn-default"; href="editArticle.php?id='.$this->articleId.'" style="margin-right: 4px;">Edytuj</a>'; echo'<input type="submit" value="Usuń" onClick="return confirm(\'Na pewno chcesz usunąć ten post?\')" class="btn btn-danger" />';
</div>
</div>
</form>
</div>';
}
public function deleteArticle($postId){
require_once '../db.php';
$db = new DbAction();
$db->connection();
$db->getResult("DELETE FROM `articles` WHERE id ='$postId' ");
}
}
} // ./PanelEditing
//THIS CLASS UPDATES ARTICLES
class UpdateArticles{
public $articleId;
public $articleTitle;
public $articleText;
function __construct(){
$this->articleId = $_GET['id'];
$this->articleTitle = $_POST['title'];
$this->articleText = $_POST['article'];
}
public function updateArticle(){
require_once 'db.php';
$db = new DbAction();
$db->connection();
$db->getResult("UPDATE `articles` SET `title`='$this->articleTitle',`article`='$this->articleText' WHERE `id`=$this->articleId");
}// ./updateArticle
}// ./UpdateArticle
Obsługa bazy danych db.php
<?php
class DbAction{
public $query;
public $result;
public $connection;
public $numRows;
public $row;
private $host;
private $db_user;
private $db_password;
private $db_name;
//CONSTRUCTOR
public function __construct(){
$this->connection;
$this->query = '';
$this->result = '';
$this->numRows = '';
$this->row = '';
$this->host = "localhost";
$this->db_user = "root";
$this->db_password = "";
$this->db_name = "cms";
} // ./__construct
//STARTS CONNECTION
public function connection(){
$this->connection = new mysqli($this->host, $this->db_user, $this->db_password, $this->db_name) ;
if($this->connection == true){
return true;
} else {
return false;
}
}// ./connection
//RETURNS RESULT
public function getResult($query){
if ($this->result = $this->connection->query($query)) {
return $this->result;
} else{
return false;
}
}// ./getResult
//RETURNS NUMBER OF ROW
public function rowNumber(){
if($this->numRows = $this->result->num_rows){
return $this->numRows;
} else{
return false;
}
}
//RETURNS NUMBER OF ROW
public function row(){
if($this->row = $this->result->fetch_assoc()){
return $this->row;
} else {
return false;
}
}
}// ./DbAction
?>