[eluser]Freeze Dried Pop[/eluser]
I extended sessions so that I could set feedback for the user, flashdata would have been fine but sometimes multiple redirects happened and I also wanted to be able to set multiple different "feedbacks".
So rather than hacking at the core functionality I added my very own basic session library like so:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* MY_Session extends the normal session by adding a "feedback" option.
*
* The feedback option works similar to flash data but doesn't automatically delete on every page load.
*
* The idea is to easily save any user feedback such as success or error messages across pageloads automatically
* without worrying about saving it when doing redirects.
*
* Feedback data is reset everytime the get_feedback() function is called, assuming that it's only ever called
* when feedback is set to be displayed.
*/
class MY_Session extends CI_Session
{
private $feedback_key = 'feedback';
public function __construct($params = array())
{
parent::__construct($params);
//Set feedback if it hasn't already been set (saves doing a check everytime we set or get feedback)
if(!isset($this->userdata[$this->feedback_key])) $this->userdata[$this->feedback_key] = array();
}
/**
* Add feedback messages for the next request
*
* @access public
* @param string
* @param string
* @param string
* @return void
*/
function set_feedback($type, $text, $title = '')
{
array_push($this->userdata[$this->feedback_key], array(
'type' => $type,
'text' => $text,
'title' => $title
));
}
/**
* Gets the feedback ready for output
*
* We empty it as we assume if its being gotten, then its being displayed.
*
* @access public
* @return array
*/
function get_feedback()
{
$feedback = $this->userdata[$this->feedback_key];
$this->empty_feedback();
return $feedback;
}
/**
* Checks if there's any feedback set
*
* @access public
* @return bool
*/
function feedback_exists()
{
return empty($this->userdata[$this->feedback_key]);
}
/**
* Deletes any saved feedback
*
* @access public
* @return void
*/
function empty_feedback()
{
$this->userdata[$this->feedback_key] = array();
}
}
Usage is pretty simple this could be your auth controller:
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Auth extends CI_Controller {
/**
* Login Page
*/
public function login()
{
if(func_to_log_user_in())
{
$this->session->set_feedback('error', 'You have been successfully logged in', 'Login Failed');
}
else
{
$this->session->set_feedback('error', 'You could not be logged in', 'Login Failed');
}
}
}
Your "view" might then look something like this:
Code:
foreach($this->session->get_feedback() as $feedback) {
echo '<div class="feedback feedback_'.$feedback['type'].'">';
if(!empty($feedback['title'])) echo '<strong>'.$feedback['title'].'</strong>';
echo '<p>'.$feedback['text'].'</p>
</div>';
}
You can then style each one using ".feedback_success" or ".feedback_error" and styles for all using ".feedback".
If you want to pop up you can use jQuery to do that, or just have them display somewhere on the page. Personally I put at the very top of the page with jQuery powered close buttons on each one.
Hope this helps, i've never shared this code before so somebody else may wish to recommend improvements!