FIRST OF ALL: I'm trying to develop a class or a couple of methods (or just a way), to use in codeigniter, that is generic, easy and flexible, I need a way to pass a message to it, display, everywhere in the project, and erase it then. (and if is ajax, will be awesome)
Back in the time that i didn't used codeigniter, I use to have a class, to insert messages in a session, a very simple class, so i could display success messages and etc. Here is the class that i want to bring to codeigniter, but I'm almost sure that codeigniter has a similar solution, but i don't know what to search. I also would like I better solution than this one, because i tried to adapt to the way that codeigniter hands session, but didn't work
<?php
class Basic
{
protected $erros;
function __construct()
{
session_start();
if(!empty($_SESSION['erro'])) // Session não vazia (tem uma array dentro dela ent?o)
{
$this->erros = $_SESSION['erro'];
}else // Session VAZIA (tem que COLOCAR uma array dentro dela)
{
$this->erros = array();
$_SESSION['erro'] = $this->erros;
}
}
private function destruct()
{
unset($_SESSION['erro']);
unset($this->erros);
$this->erros = NULL;
}
private function add($string)
{
$this->erros = $_SESSION['erro']; // pega mais um
$this->erros[] = $string; // adiciona no array
$_SESSION['erro'] = $this->erros; // coloca de volta na session
}
public function add_error($data)
{
if(empty($data))return true;
if(is_array($data))
{
foreach ($data as $value)
{
$this->add($value);
}
}
else
{
$this->add($data);
}
}
public function return_errors()
{
$erros = $this->erros;
$this->destruct();
return $erros;
}
public function check_ok()
{
return ($this->erros == NULL)?true:false;
}
public function show_errors()
{
$erros = $this->erros;
print_r($erros);
}
}
?>
Here is my puny attempt to convert it to use codeigniter sessions
<?php
function __construct()
{
if(!empty($this->session->userdata('my_msgs'))) // Session n?o vazia (tem uma array dentro dela ent?o)
{
$this->msg = $this->session->userdata('my_msgs');
}else // Session VAZIA (tem que COLOCAR uma array dentro dela)
{
$this->msg = array();
$this->session->set_userdata('my_msgs', $this->msg);
}
}
function destruct()
{
$this->session->unset_userdata('my_msgs');
$this->msg = NULL;
unset($this->msg);
}
function add($string)
{
$this->msg = $this->session->userdata('my_msgs'); // pega mais um
$this->msg[] = $string; // adiciona no array
$this->session->set_userdata('my_msgs', $this->msg); // coloca de volta na session
}
function add_msg($data)
{
if(empty($data))return true;
if(is_array($data))
{
foreach ($data as $value)
{
$this->add($value);
}
}
else
{
$this->add($data);
}
}
function return_msg($keep = FALSE)
{
$msg = $this->msg;
if($keep == FALSE)
{
$this->destruct();
}
return $msg;
}
function check_ok()
{
return ($this->msg == NULL)?true:false;
}
function show_msgs()
{
$msg = $this->msg;
print_r($msg);
}
sorry about the bad English, not a native speaker