Home Email Login


POG Plugin - GetDistinct

Resources

/pluigins/plugin.getdistinct.php

This plugin allow you to get an array of distinct values from a database / class.
Example:
$test = new logEvent();
$dist = $test -> GetDistinct('unitId');
print_r($dist);

Output:
Array ( [0] => 10000 [1] => 1 [2] => 8 [3] => 2 )


class GetDistinct
{
	var $sourceObject;
	var $argv;
	var $version = '0.1';

	function Version()
	{
		return $this->version;
	}

	function AuthorPage()
	{
		return 'http://shinola.org/pages/posts/pog-plugin--getdistinct203.php';
	}

	function GetDistinct($sourceObject, $argv)
	{
		$this->sourceObject = $sourceObject;
		$this->argv = $argv;
	}

	function Execute()
	{
		$objectName = get_class($this->sourceObject);
		$column = "";
		$table = strtolower($objectName);
		if (isset($this->argv[0]))
		{
			$column = $this->argv[0];
		}
		$return = array();
		$connection = Database::Connect();
		$query = "select distinct $column from `$table`";
		$result = mysql_query($query, $connection);
		while ($row = mysql_fetch_assoc($result)) {
			array_push($return, $row[$column]);
		}
		return $return;
	}

	function SetupRender()
	{
		echo "This plugin allow you to get an array of distinct values from a database / class. <br>
		<b>Example:</b><br>
		$test = new logEvent();<br>
		$dist = $test -> GetDistinct('unitId');<br>
		print_r($dist);<br>
		<br>
		<b>Output:</b><br>
		Array ( [0] => 10000 [1] => 1 [2] => 8 [3] => 2 )<br>
		<br>
		";
		if ($this->PerformUnitTest() === false)
		{
			echo get_class($this).' failed unit test';
		}
		else
		{
			echo get_class($this).' passed unit test';
		}
	}

	function PerformUnitTest()
	{
		//test w/o arguments
		//any object
		$objectNames = unserialize($_SESSION['objectNameList']);

		//try getting a count
		if (sizeof($objectNames) > 0)
		{
			$anyObject = $objectNames[0];
			$table = strtolower($anyObject);
			$column = strtolower($anyObject) . "Id";
			include_once("../objects/class.".strtolower($anyObject).".php");
			$anyObjectInstance = new $anyObject();
			$obj1 = $anyObjectInstance->GetDistinct($column, $table);

			if (count($obj1) == 0)
			{
				return false;
			}
			return true;
		}

		//test w/ arguments
	}
}



Back

Comments

None Found

Add Comment