PHP Classes
elePHPant
Icontem

GetDataCustom method ?

Recommend this page to a friend!

      Session Store  >  All threads  >  GetDataCustom method ?  >  (Un) Subscribe thread alerts  
Subject:GetDataCustom method ?
Summary:GetDataCustom method ?
Messages:2
Author:BratSadLove
Date:2015-11-16 02:17:17
 

 


  1. GetDataCustom method ?   Reply   Report abuse  
BratSadLove - 2015-11-16 02:17:17
I write:

print_r(SessionDataStore::GetDataCustom('zipcode'));


But not return!

  2. Re: GetDataCustom method ?   Reply   Report abuse  
Anthony Amolochitis - 2015-11-16 14:13:57 - In reply to message 1 from BratSadLove
I have just tested it and it works. You have to start a session to use the session store.

=================== example ==================

<?php session_start();

class SessionDataStore
{
const Store = 'SessionDataStoreArrayIndex';

public static function Create()
{
if( ! self::IssetStore() )
{ self::Reset(); }
}

public static function Reset()
{
$_SESSION[self::Store] = array() ;
}

/** @return boolean */
public static function IssetStore()
{
if( !isset($_SESSION[self::Store]) || !is_array($_SESSION[self::Store]) )
{ return false ; }
return true ;
}

/** Push data onto the stack */
public static function AddData($data)
{
if( ! self::IssetStore() )
{ self::Create(); }

if( !in_array( $data , $_SESSION[self::Store]) )
{
array_push( $_SESSION[self::Store] , $data );
}
}

/** Get array data from the stack. Null if nothing in it. */
public static function GetData()
{
if( ! self::IssetStore() )
{ return null ;}
return $_SESSION[self::Store];
}

/** @return boolean */
public static function HasData()
{
if( ! self::IssetStore() )
{ return false ;}
return ( count($_SESSION[self::Store]) > 0 ) ? true : false ;
}


/** Push data onto the stack into custom array index */
public static function AddDataCustom( $data , $index )
{
if( ! isset( $_SESSION[self::Store][$index] ) )
{ $_SESSION[self::Store][$index] = array() ; }

if( !in_array( $data , $_SESSION[self::Store][$index] ) )
{
array_push( $_SESSION[self::Store][$index] , $data );
}
}

/** Get your data from your custom store*/
public static function GetDataCustom( $index )
{
if( ! isset( $_SESSION[self::Store][$index] ) )
{ return null ;}
return $_SESSION[self::Store][$index];
}
}


/**
* Example usage with custom storage
*/

$SessionDataStore = new SessionDataStore();

$data = new stdClass();
$data->name = 'Jimmy John';
$data->web = 'www.amospace.com' ;

$index = 'custIndex';
$SessionDataStore->AddDataCustom( json_encode($data) , $index);

$val = $SessionDataStore->GetDataCustom($index);

print_r($val);

?>