Sorting encryted data

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
acidHL
Forum Commoner
Posts: 41
Joined: Wed Dec 07, 2005 7:38 am

Sorting encryted data

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Store each resultant record into as an array element in a larger array. Decrypt the various columns then sort as needed.
acidHL
Forum Commoner
Posts: 41
Joined: Wed Dec 07, 2005 7:38 am

Post 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!
acidHL
Forum Commoner
Posts: 41
Joined: Wed Dec 07, 2005 7:38 am

Post 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!
Post Reply