Page 1 of 1

Sorting encryted data

Posted: Wed Sep 13, 2006 6:06 am
by acidHL
Hi Guys,

I have some code that returns encrypted data from a database, as follows:

Code: Select all

$sql = $database->query("SELECT * FROM `email` WHERE `CompanyID`='{$_SESSION['CLIENT']['ID']}' AND `Type` != 'Forwarder' ORDER BY Type ASC, Address ASC");

while($email = $database->fetch_assoc($sql))
{
	
... etc
Currently certain fields are encrypted and a I have a small object called crypt() that decodes it correctly.
So when I output a value that is encrypted I do:

Code: Select all

echo $crypt->decrypt($email['Address']);
The problem I have is that I cant sort the data properly as the SQL statement above simply sorts the enrypted data.
Can anybody suggest the simplest way I could decrypt the data and sort it before it is output?

Posted: Wed Sep 13, 2006 2:03 pm
by feyd
Store each resultant record into as an array element in a larger array. Decrypt the various columns then sort as needed.

Posted: Thu Sep 14, 2006 6:37 am
by acidHL
Thats what I was thinking at first, just wanted to see if there was an easier way I hadn't thought of.

Thanks feyd!

Posted: Thu Sep 14, 2006 11:16 am
by acidHL
Am I right in assuming array_multisort() is the way to go?

I was about to test the following and the web server died :x

Code: Select all

$sql = $database->query("SELECT * FROM `email` WHERE `CompanyID`='{$_SESSION['CLIENT']['ID']}' AND `Type` != 'Forwarder'");
if ($database->num_rows($sql) < 1)
{
	echo '<tr><td colspan="3"><em>This client has no e-mail accounts.</em></td></tr>';
}
						
// Fetch all of the items into an array:
while ($arr_AddressHolder_encoded = $database->fetch_assoc($sql))
{
	foreach ($arr_AddressHolder_encoded as $key => $value)
	{
		if ($key == "ID" OR $key == "CompanyID" OR $key == "Type" OR $key == "HasSpam")
		{
			$item[$key] = $value;
		} else {
			$item[$key] = $crypt->decrypt($value);
		}
	}
	$arr_AddressHolder[] = $item;
	}

	array_multisort("CompanyID", SORT_DESC, $arr_AddressHolder);
				
	//while($email = $arr_AddressHolder)
	foreach($arr_AddressHolder as $email)
	{

.... etc
I'm not sure i've passed the correct parameters to array_multisort() - I found the php.net manual a little confusing when I tried to apply its example to my code.

Hopefully the server will be back up soo an I can test it properly!