ओपी के अनुरोध पर मैं अपना गेस्टबुक कोड साझा करूंगा।
संदेश वर्ग:
<?php
Class message
{
private $db;
private $messageID;
private $message;
private $name;
private $mail;
public function setmessageID($messageID)
{
$this->messageID = $messageID;
}
public function getmessageID()
{
return $this->messageID;
}
public function setmessage($message)
{
$this->message = $message;
}
public function getmessage()
{
return $this->message;
}
public function setname($name)
{
$this->name = $name;
}
public function getname()
{
return $this->name;
}
public function setMail($mail)
{
$this->mail = $mail;
}
public function getMail()
{
return $this->mail;
}
}
संदेश डेटा एक्सेस ऑब्जेक्ट क्लास:
<?php
class messageDAO
{
private $db;
private $aantalMessages;
private $messages;
private $message;
//bij laden roept hij automatisch Db class aan (en de daarbij gezeten functies)
public function __construct(Db $db)
{
$this->db = $db;
}
public function getMessages()
{
return $this->messages;
}
public function getAantalMessages()
{
return $this->aantalMessages;
}
//Function to retrieve messages
public function findMessages($args)
{
$dbh = $this->db->DBH();
//$offset for pagination
$offset = ($args['currentPage'] - 1) * $args['itemsPerPage'];
$sth = $dbh->prepare("SELECT SQL_CALC_FOUND_ROWS
messageen.messageID,
messageen.message,
messageen.name,
messageen.mail
FROM `messageen`
ORDER BY messageen.datumToegevoegd DESC
LIMIT ?, ?");
$sth->bindParam(1, $offset, PDO::PARAM_INT);
$sth->bindParam(2, $args['itemsPerPage'], PDO::PARAM_INT);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
$messages = array();
while($row = $sth->fetch())
{
$message = new message();
$message->setMessageID(htmlentities(strip_tags($row['messageID'])));
$message->setSessage(htmlentities(strip_tags($row['message'])));
$message->setName(htmlentities(strip_tags($row['name'])));
$message->setMail(htmlentities(strip_tags($row['mail'])));
$messages[] = $message;
}
$sth = $dbh->prepare("SELECT FOUND_ROWS() as numberOfMessages");
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
$this->numberOfMessages = $sth->fetch();
return $messages;
}
public function setMessageToEdit($args)
{
$sth = $this->db->DBH()->prepare("SELECT messages.message
FROM `messages`
WHERE messages.messageID = ?");
$sth->bindParam(1, $args['messageID']);
$sth->execute();
$sth->setFetchMode(PDO::FETCH_ASSOC);
//return the retrieved message
while($row = $sth->fetch())
{
$message = new message();
$message->setMessage(htmlentities(strip_tags($row['message'])));
$message->setMessageID(intval($args['messageID']));
}
return $message;
}
//functie om messageen aan te passen
public function save(message $message)
{
//insert part
//if(isset($message->getname()) && isset($message->getmessage()) && isset($message->getMail()))
//{
$sth = $this->db->DBH()->prepare("INSERT INTO `messages`
SET messages.name = ?,
messages.mail = ?,
messages.message = ?,
messages.dateAdded = NOW()");
$sth->bindParam(1, $message->getName());
$sth->bindParam(2, $message->getMail());
$sth->bindParam(3, $message->getMessage());
$sth->execute();
//}
//update part
/*if(isset($message->getmessageID()) && isset($message->getmessage()))
{
$sth = $this->db->DBH()->prepare("UPDATE `messageen`
SET messageen.message = ?
WHERE messageen.messageID = ?
LIMIT 1");
$sth->bindParam(1, $message->getmessage());
$sth->bindParam(2, $message->getmessageID());
$sth->execute();
}*/
}
}
index.php
<?php
//include file loader.php
include("includes/loader.php");
$guestbook = new guestbook($db);
$user = new user($db);
$messageDAO = new messageDAO($db);
//Make a array named error
$error = array();
//Get action (login/setmessage/editmessage/deletemessage)
if(isset($_GET['action']))
{
switch ($_GET['action'])
{
//if login submit is pressed
case 'login':
//Check if filled
if(isset($_POST['username']) && isset($_POST['username']))
{
$error['usernameEmpty'] = (bool) !strlen(trim($_POST['username']));
$error['passwordEmpty'] = (bool) !strlen(trim($_POST['password']));
}
if(in_array(1, $error))
{
//Assign $error to smarty
$smarty->assign('error', $error);
}
else
{
if(isset($_POST['username']) && isset($_POST['username']))
{
$user->setLoggedIn(array('username'=>$_POST['username'],
'password'=>$_POST['password']));
if($user->getLoggedIn() != true)
{
$smarty->assign('loggedInError', $user->getLoggedIn());
}
}
}
break;
//Als if "place message" is pressed
case 'placemessage':
//if user is not logged in
if($user->getLoggedIn() != true)
{
//Controleren of message-velden wel zijn ingevuld
$error['nameEmpty'] = (bool) !strlen(trim(htmlentities(strip_tags($_POST['messagename']))));
$error['mailEmpty'] = (bool) !strlen(trim(htmlentities(strip_tags($_POST['messageMail']))));
$error['messageEmpty'] = (bool) !strlen(trim(htmlentities(strip_tags(str_replace('place message...','', $_POST['messageInput'])))));
if($error['mailEmpty'] != 1)
{
$error['mailInvalid'] = !filter_input((INPUT_POST), 'messageMail', FILTER_VALIDATE_EMAIL);
}
if(in_array(1, $error))
{
$smarty->assign('error', $error);
}
else
{
$message = new message();
$message->setname($_POST['messagename']);
$message->setMail($_POST['messageMail']);
$message->setmessage($_POST['messageInput']);
dump($message);
//place message
$messageDAO->save($message);
}
}
//if user is logged in
else
{
//is message filled?
$error['messageEmpty'] = (bool) !strlen(trim(htmlentities(strip_tags(str_replace('place hier uw message...','', $_POST['messageInput'])))));
if($error['messageEmpty'] != 1)
{
$user->setUser();
$guestbook->placemessage(array('name'=>$user->getLoggedInUsername(),
'mail'=>$user->getLoggedInUserMail(),
'messageInput'=>$_POST['messageInput']));
}
else
{
$smarty->assign('error', $error);
}
}
break;
case 'deletemessage':
$user->setUser();
if($user->getLoggedInUserAdmin() == 1)
{
if(isset($_GET['messageID']) && is_numeric($_GET['messageID']) && isset($_GET['key']))
{
$guestbook->setURLKey($_GET['messageID']);
if($guestbook->getURLKey() == $_GET['key'])
{
$guestbook->verwijdermessage(array('messageID'=>$_GET['messageID']));
}
}
}
die(header("location: /index.php"));
break;
}
}
if(isset($_GET['pagina']) && is_numeric($_GET['pagina']))
{
$currentpage = $_GET['pagina'];
}
else
{
//$currentpage is 1
$currentpage = 1;
}
$user->setUser();
//assign var to smarty
$smarty->assign('messages', $messageDAO->findmessages(array('currentpage'=>$currentpage, 'itemsPerPagina'=>10)));
$smarty->assign('user', $user);
//Pagination
$numbermessages = $messageDAO->getnumbermessages();
$totalpages = ceil($numbermessages['numbermessages'] / 10);
if($currentpage < 1)
{
//$currentpage is 1
$currentpage = 1;
}
if($currentpage > $totalpages)
{
$currentpage = $totalpages;
}
$smarty->assign('numbermessages', $messageDAO->getnumbermessages());
$smarty->assign('guestbook', $guestbook);
$smarty->assign('currentpage', $currentpage);
$smarty->assign('totalpages', $totalpages);
//display index.tpl
$smarty->display('index.tpl');
मैंने कुछ चर और फ़ंक्शन का नाम बदलकर आपको समझ में आने के लिए (डच से अंग्रेजी में अनुवाद: पी) किया है, इसलिए आपको कभी-कभी कुछ अजीब भावनाएं मिल सकती हैं, क्योंकि मैंने बस एक त्वरित प्रतिस्थापन किया था आदि इसके साथ मज़े करते हैं। इसके अलावा यह पूरा कोड नहीं है, क्योंकि इससे मुझे कोड के लायक 20 फाइलों की तरह पोस्टिंग मिलेगी: पी