[eluser]Zeff[/eluser]
Hi fellows,
In a project, sample data is collected from 5 to 20 samples. I made several methods and according views (mainly forms) to gather this data. But users should be able to set sample size (a static var), so forms can accordingly consist of 5 up to 20 input elements...
My idea was to set this static class var from within the view via JQuery ajax load within a 'mini form' that updates the static var.
My OOP knowledge in PHP is not that strong, so I need some help getting end setting this class wide var...
Does anyone has experience in this matter? I think I will have to write setter and getter methods to set/read the static var that will have to be called from within JQuery load functions.
Other workarounds are welcome ;-)
Thanks!
Zeff
[eluser]InsiteFX[/eluser]
This is a pet project of mine that I am still working on!
It works but is not fully debuged and I am still adding to it, It allows for storing static Properties.
There is also a helper for this which you can call the methods form anywhere's views etc.
Code below:
[eluser]InsiteFX[/eluser]
./application/libraries/Property.php
This should be autoloaded in ./application/config/autoload.php
Read the Class comments for usage:
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 5/10/2012
* Time : 4:02:10 AM
* The Learn CodeIgniter Development Team.
* ------------------------------------------------------------------------
*
* Class Properties
*
* @package Package Application
* @subpackage Subpackage properties
* @category category properties
* @author Raymond L King Sr.
* @copyright Copyright (c) 2009 - 2012, Custom Software Designers, LLC.
* @link http://example.com
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/
/**
* SETUP:
*
* place this library in ./application/libraries/Property.php
*
* Autoload: ./application/libraries/Property.php
* Autoload: ./application/helpers/property_helper.php
*
* TESTING:
*
* In your Controller index method add.
*
* property_set('1', 'test1', 'test1');
* property_set('1', 'test2', 'test1');
* property_set('1', 'test3', 'test1');
*
* property_set('2', 'test4', 'test2');
* property_set('2', 'test5', 'test2');
* property_set('2', 'test6', 'test2');
*
* $result = property_get('1', 'test3');
* $result1 = property_get_index('2');
* $exist = property_exists('2', 'test1');
*
* $result2 = property_delete('1', 'test3');
*
* // Remove the remarks // to reset and clear out the properties array.
* // property_clear();
*
* DEBUGING:
*
* var_dump($this->property->debug_properties());
*
* $this->property->var_debug($this->property->debug_properties(), $result, $result1, $exist, $result2);
*
* USAGE:
*
* set: $this->property->set('1', 'test1', 'test1');
* get: $result = $this->property->get('1', 'test3');
* get_index $result = $this->property->get_index('2');
* exists: $result = $this->property->exists('2', 'test1');
* clear: $this->property->clear();
* delete: $result2 = $this->property->delete('1', 'test3');
*
*/
class Property {
/**
* -----------------------------------------------------------------------
* Class variables - public, private, protected and static.
* -----------------------------------------------------------------------
*/
/**
* array ( $index => array ( $key => $val ));
*/
private static $properties = array(array());
// -----------------------------------------------------------------------
/**
* __construct()
*
* Constructor PHP 5+ NOTE: Not needed if not setting values!
*
* @access public
* @return void
*/
public function __construct()
{
log_message('debug', 'Property Class Initialized');
}
// -----------------------------------------------------------------------
/**
* set()
*
* Set a property index and key, value pair.
*
* @access public
* @param string - $index - The property index
* @param string - $key - The property key
* @param string - $val - The property value
* @return void
*/
public function set($index, $key, $val)
{
if ( ! isset($this->properties[$index][$key]))
{
$this->properties[$index][$key] = $val;
}
}
// -----------------------------------------------------------------------
/**
* get()
*
* Gets a property and value.
*
* @access public
* @param string - $index - The property index
* @param string - $key - The property key
* @return mixed - Property string or boolean FALSE
*/
public function get($index, $key)
{
if (isset($this->properties[$index][$key]))
{
return $this->properties[$index][$key];
}
return FALSE;
}
// -----------------------------------------------------------------------
/**
* get_index()
*
* Gets the array for this index.
*
* @access public
* @param string - $index
* @return mixed
*/
public function get_index($index = NULL)
{
if ($index != NULL)
{
return $this->properties[$index];
}
return FALSE;
}
[eluser]InsiteFX[/eluser]
Continued:
Code: // -----------------------------------------------------------------------
/**
* exists()
*
* Checks to see if a property exists.
*
* @access public
* @param string - $index - The property index
* @param string - $key - The property key
* @return bool - TRUE if the property exists otherwise FALSE.
*/
public function exists($index, $key)
{
return isset($this->properties[$index][$key]);
}
// -----------------------------------------------------------------------
/**
* clear()
*
* Clears out and resets the property arrays.
*
* @access public
* @return void
*/
public function clear()
{
$this->properties = array(array());
}
// -----------------------------------------------------------------------
/**
* delete()
*
* Deletes a property index key.
*
* @access public
* @param string - $index - The property index
* @param string - $key - The property key
* @return bool
*/
public function delete($index, $key)
{
if (isset($this->properties[$index][$key]))
{
unset($this->properties[$index][$key]);
return TRUE;
}
return FALSE;
}
// -----------------------------------------------------------------------
/**
* debug_properties()
*
* Debug the properties arrays.
*
* @access public
* @return array
*/
public function debug_properties()
{
return $this->properties;
}
// -----------------------------------------------------------------------
/**
* Debug Helper
*
* Outputs the given variable(s) with formatting and location.
*
* @access public
* @param mixed - variables to be output.
* @return void
*/
public function var_debug()
{
list ($callee) = debug_backtrace();
$arguments = func_get_args();
$total_arguments = count($arguments);
echo '<fieldset>';
echo '<legend>' . $callee['file'] . ' @ line: ' . $callee['line'] . '</legend><pre>';
$i = 0;
foreach ($arguments as $argument)
{
echo '<strong>Debug #' . (++$i) . ' of ' . $total_arguments . '</strong>: '."<br />";
var_dump($argument);
echo "\n";
}
echo "</pre>";
echo "</fieldset><br />";
exit ('Finished dumping debug variables.');
}
} // End of Class
/* -------------------------------------------------------------------------
* End of file Property.php
* Location: ./application/libraries/Property.php
* -------------------------------------------------------------------------
*/
[eluser]InsiteFX[/eluser]
Helper: - ./application/helpers/property_helper.php
This should also be autoloaded.
Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* ------------------------------------------------------------------------
* Created by Php Designer 8.
* Date : 5/20/2012
* Time : 11:29:08 AM
* Author: Raymond L King Sr.
* The Learn CodeIgniter Development Team.
*
* Helper Name
*
* ------------------------------------------------------------------------
* To change this template use File | Settings | File Templates.
* ------------------------------------------------------------------------
*/
// ------------------------------------------------------------------------
/**
* property_set()
*
* Sets a new property.
*
* Usage: property_set($index, $key, $val);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_set'))
{
function property_set($index = '', $key = '', $val = '')
{
$CI = get_instance();
$CI->property->set($index, $key, $val);
}
}
// ------------------------------------------------------------------------
/**
* property_get()
*
* Gets a property value.
*
* Usage: $result = property_get($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_get'))
{
function property_get($index = '', $key = '')
{
$CI = get_instance();
return $CI->property->get($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* property_get_index()
*
* Get a property index.
*
* Usage: $result = property_get_index($index);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_get_index'))
{
function property_get_index($index = NULL)
{
$CI = get_instance();
return $CI->property->get_index($index);
}
}
// ------------------------------------------------------------------------
/**
* property_exists()
*
* Checks to see if a property exists.
*
* Usage: $result = property_exists($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_exists'))
{
function property_exists($index = '', $key = '')
{
$CI = get_instance();
return $CI->property->exists($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* property_delete()
*
* Deletes a property.
*
* Usage: $reult = property_delete($index, $key);
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_delete'))
{
function property_delete($index = '', $key = '')
{
$CI = get_instance();
return $CI->property->delete($index, $key);
}
}
// ------------------------------------------------------------------------
/**
* property_clear()
*
* Resets and clears out the properties array.
*
* Usage: property_clear();
*
* @access public
* @param array
* @return mixed depends on what the array contains
*/
if ( ! function_exists('property_clear'))
{
function property_clear()
{
$CI = get_instance();
$CI->property->clear();
}
}
/* ------------------------------------------------------------------------
* End of file property_helper.php
* Location: ./application/helpers/property_helper.php
* ------------------------------------------------------------------------
*/
[eluser]InsiteFX[/eluser]
Darn, forgot no code forum sorry!
MOD's please move.
[eluser]Zeff[/eluser]
Hi InsiteFX, thanks for your reply and early code release! I will give it a try and test it.
Will keep you informed!
Zeff
|