Skip to content

Instantly share code, notes, and snippets.

@jcchikikomori
Last active February 11, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcchikikomori/41245da8fb8fe54d0c3a to your computer and use it in GitHub Desktop.
Save jcchikikomori/41245da8fb8fe54d0c3a to your computer and use it in GitHub Desktop.
PHP Feedback using Session
<?php
/**
* Get Feedback
* Set this on views
*/
class Feedback {
/**
* renders the feedback messages into the view
*/
public function renderFeedbackMessages()
{
// echo out the feedback messages (errors and success messages etc.),
// they are in $_SESSION["feedback_positive"] and $_SESSION["feedback_negative"]
// require VIEWS_PATH . '_templates/feedback.php';
// get the feedback (they are arrays, to make multiple positive/negative messages possible)
$feedback_positive = Session::get('feedback_positive');
$feedback_negative = Session::get('feedback_negative');
$feedback_note = Session::get('feedback_note');
// echo out positive messages
if (isset($feedback_positive)) {
echo '<div class="alert bg-success alert-dismissible" role="alert"><button class="close" aria-label="close" data-dismiss="alert" type="button"><span aria-hidden="true">x</span></button>';
echo '<ul class="list-unstyled"><span class="glyphicon glyphicon-ok">&nbsp;</span><strong>SUCCESS</strong><br /><br />';
foreach ($feedback_positive as $feedback) {echo '<li>' . $feedback . '</li>';}
echo '</ul>';
echo '</div>';
}
// echo out negative messages
else if (isset($feedback_negative)) {
echo '<div class="alert bg-danger alert-dismissible" role="alert"><button class="close" aria-label="close" data-dismiss="alert" type="button"><span aria-hidden="true">x</span></button>';
echo '<ul class="list-unstyled"><span class="glyphicon glyphicon-remove">&nbsp;</span><strong>OOPS</strong><br /><br />';
foreach ($feedback_negative as $feedback) {echo '<li>' . $feedback . '</li>';}
echo '</ul>';
echo '</div>';
}
else if (isset($feedback_note)) {
echo '<div class="alert bg-success alert-dismissible" role="alert"><button class="close" aria-label="close" data-dismiss="alert" type="button"><span aria-hidden="true">x</span></button>';
echo '<ul class="list-unstyled"><span class="glyphicon glyphicon-exclamation-sign">&nbsp;</span><strong>ATTENTION</strong><br /><br />';
foreach ($feedback_note as $feedback) {echo '<li>' . $feedback . '</li>';}
echo '</ul>';
echo '</div>';
}
// delete these messages (as they are not needed anymore and we want to avoid to show them twice
Session::set('feedback_positive', null);
Session::set('feedback_negative', null);
Session::set('feedback_note', null);
}
}
<?php
/**
* Session class
*
* handles the session stuff. creates session when no one exists, sets and
* gets values, and closes the session properly (=logout). Those methods
* are STATIC, which means you can call them with Session::get(XXX);
*/
class Session
{
/**
* starts the session
*/
public static function init()
{
// if no session exist, start the session
if (session_id() == '') {
session_start();
}
}
/**
* sets a specific value to a specific key of the session
* @param mixed $key
* @param mixed $value
*/
public static function set($key, $value)
{
$_SESSION[$key] = $value;
}
/**
* gets/returns the value of a specific key of the session
* @param mixed $key Usually a string, right ?
* @return mixed
*/
public static function get($key)
{
if (isset($_SESSION[$key])) {
return $_SESSION[$key];
}
}
/**
* deletes the session (= logs the user out)
*/
public static function destroy() {
//session_unset();
session_destroy();
//session_write_close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment