Forleden manglede jeg en mulighed for at gemme diverse bruger notitser og senere hen udskrive dem. Jeg valgte at bruge sessions til formålet, således fejlene kan gemmes til de skal vises.
Mit projekt skulle kun have mulighed for, at gemme en enkelt notits før en ny blev genereret. Hvis dit projekt kræver mere end en notits, må du selv få udbygget scriptet. Jeg tænker, at det kunne klares vha. arrays.
Klassen er i sig selv ganske simpel. Jeg returnerer klassens objekt i nogle af funktionerne for at kæde metoderne sammen.
Kode: Vælg alt
<?php
class sessionNotice {
/*
* protected message
* The message to be shown
* @var string
*/
protected $message;
/*
* protected class
* The class that the element should have
* @var string
*/
protected $class;
/*
* protected output
* The full output including HTML markup.
* @var string
*/
protected $output;
/*
* __construct()
* The constructor of the class. It makes sure that the session is started.
*/
public function __construct() {
// Check if the session has already been started
if (session_status() == PHP_SESSION_NONE) {
// If it hasn't, start it.
session_start();
}
}
/*
* setMessage
* Set the class property "message".
* @param string $message The message
* @return object $this The class object.
*/
public function setMessage($message) {
$this->message = $message;
return $this;
}
/*
* getMessage
* Get the class property "message".
* @return string $this->message The class property, message.
*/
public function getMessage() {
return $this->message;
}
/*
* setMessage
* Set the class property "class".
* @param string $class The class
* @return object $this The class object.
*/
public function setClass($class) {
$this->class = $class;
return $this;
}
/*
* getClass
* Get the class property "class".
* @return string $this->class The class property, class.
*/
public function getClass() {
return $this->class;
}
/*
* save
* Save the class properties into sessions.
*/
public function save() {
$_SESSION['notice_message'] = $this->message;
$_SESSION['notice_class'] = $this->class;
}
/*
* prepare
* Create the full HTML markup and assign the value to the class property "output"
* @return object $this The class object.
*/
public function prepare() {
// Check if both the message and class is set
if (isset($_SESSION['notice_message']) && isset($_SESSION['notice_class'])) {
// If so, use these values
$this->output = '<div class="' . $_SESSION['notice_class'] . '">' . $_SESSION['notice_message'] . '</div>';
// Check if only the message is set.
} elseif (isset($_SESSION['notice_message']) && !isset($_SESSION['notice_class'])) {
// If so, use a default class.
$this->output = '<div class="alert notice-message">' . $_SESSION['notice_message'] . '</div>';
} else {
// If only the class is set OR both are empty, dont generate anything
$this->output = '';
}
return $this;
}
/*
* prepare
* Output the class property "output" and empty the sessions and empty the class properties
*/
public function generate() {
// echo the class property "output"
echo $this->output;
# Check if the message and class is set. If they are, remove them from the session.
if (isset($_SESSION['notice_message'])) {
unset($_SESSION['notice_message']);
}
if (isset($_SESSION['notice_class'])) {
unset($_SESSION['notice_class']);
}
# Empty the class variables.
$this->message = '';
$this->class = '';
$this->output = '';
}
}
Kode: Vælg alt
<?php
require ('component/sessionNotice.class.php');
$notice = new sessionNotice;
// Success
$notice->setMessage('This is an alert message')->setClass('alert success-message')->save();
echo $notice->prepare()->generate();
// Warning
$notice->setMessage('This is a warning message')->setClass('alert warning-message')->save();
echo $notice->prepare()->generate();
// Error
$notice->setMessage('This is an error message')->setClass('alert error-message')->save();
echo $notice->prepare()->generate();
// Notice
$notice->setMessage('This is a notice message')->setClass('alert notice-message')->save();
echo $notice->prepare()->generate();
Hvis du oplever fejl el. lign., skal du være velkommen at skrive i tråden her.