Witam. W ramach ćwiczeń zabrałem się za napisanie bardzo prostego systemu newsów, który ma umożliwiać:
- wyświetlanie wszystkich newsów
- dodawanie newsa
Mam już pewien szkielet tego systemu, jednak nie za bardzo wiem W KTÓRYM MIEJSCU wpleść kod odpowiedzialny za dodanie newsa do db.
class.news.php
<?php
class News {
private $_news_id;
private $_author;
private $_title;
private $_content;
public function __construct($_news_id,$_author,$_title,$_content)
{
$this->_news_id = $_news_id;
$this->_author = $_author;
$this->_title = $_title;
$this->_content = $_content;
}
//Settery
public function setAuthor($author) {
$this->_author = $author;
}
public function setTitle($title) {
$this->_title = $title;
}
public function setContent($content) {
$this->_content = $content;
}
//gettery
public function getNewsId() {
return $this->_news_id;
}
public function getAuthor() {
return $this->_author;
}
public function getTitle() {
return $this->_title;
}
public function getContent() {
return $this->_content;
}
}
?>
index.php
<?php
include_once('connectDb.php');
require_once('class.news.php');
echo '<a href="index.php?mod=add">Dodaj</a>';
if(isset($_GET['mod'])) {
if($_GET['mod']=="add")
{
echo '
<form action="index.php" method="post">
<input type="text" name="author" /><br />
<input type="text" name="title" /><br />
<textarea name="content"></textarea><br />
<input type="submit" name="send" />
</form>
';
}
}
elseif(isset($_POST['send'])) {
//id,autor,tytul,tresc
$news = new News('x',$_POST['author'],$_POST['title'],$_POST['content']);
}
?>
Jak widać, po kliknięciu w przycisk następuje utworzenie obiektu. Czy konstruktor jest w ogóle potrzebny? Czy zrobić tak, by w setterach wkleić kod z INSERTami i uruchamiać settery po kolei? Jeszcze trudno mi się w tym połapać :P
karolinaavpiotr