I have a script which is passed the following array of Users:
$tmpU[0][0] = Original Login Date
$tmpU[0][1] = Last Login Date
$tmpU[0][2] = User name
I have written routines to do the following:
1) cycle thru array and check for current user
-if found, update [0][1] Last Login Date to current
-if not found, compare Current Date with Last Login
if greater than 15 minutes than empty array
2) cycle thru array again and check for empty value
-put non-empty values in new array
thus removing empty array values
3) check to make sure Current user was found/updated
-if not, add to end of array
4) sort array by value [3] user Name
This works for me, however, it seems like overkill and
I think it can be optimized. Can anyone please review the
code and identify any easier approaches?
Please someone take a look
<?php
//Current user info
$Name = "Greg";
$origDT = "20040523220004";
//$datetime is normall current date/time
$datetime = "20040523220504";
$noActivity = 15;
$iwasfound = "";
//Set Up test Data
$tmpU[0][0] = "20100523220004"; //Original Date
$tmpU[0][1] = "20100523220004"; //Last Modified Date
$tmpU[0][2] = "@Admin"; //User name
$tmpU[1][0] = "20040523220004";
$tmpU[1][1] = "20040523220004";
$tmpU[1][2] = "Zack";
$tmpU[2][0] = "20040523220004";
$tmpU[2][1] = "20040523220004";
$tmpU[2][2] = "John";
$tmpU[3][0] = "20040523220004";
$tmpU[3][1] = "20040523219604"; //15 min less than $datetime
$tmpU[3][2] = "Allison";
// Cycle thru User List
// if find Current User: update [1] to $datetime
// else if :empty inactive users > than 15 min
if (count($tmpU) > 0){
for ( $i = 0; $i < count($tmpU); $i++ ){
// Check for this user and update if found
if ($tmpU[$i][2] == $Name){
$tmpU[$i][1] = $datetime;
$iwasfound = "yes";
//Not user, so lets test returned user for
//inactivy and remove if needed
} else if((($datetime - $tmpU[$i][1])/60) >= $noActivity){
$tmpU[$i][0] = "";
$tmpU[$i][1] = "";
$tmpU[$i][2] = "";
}
}
//Cycle thru again, remove empty user fields, place in new array
$myCnt = 0;
for ( $i = 0; $i < count($tmpU); $i++ ){
if ($tmpU[$i][2] !== ""){
$tmpUsers[$myCnt][0] = $tmpU[$i][0];
$tmpUsers[$myCnt][1] = $tmpU[$i][1];
$tmpUsers[$myCnt][2] = $tmpU[$i][2];
$myCnt++;
}
}
} else {
//Just in case, to avoid error on empty list
$tmpUsers = $tmpU;
}
// Add Current User to end if NOT already in User List
if ($iwasfound == ""){
$myNewCount = count($tmpUsers);
$tmpUsers[$myNewCount][0] = $origDT;
$tmpUsers[$myNewCount][1] = $datetime;
$tmpUsers[$myNewCount][2] = $Name;
}
//Sort User List by Name
foreach ($tmpUsers as $val){
$sortarray[] = $val[2];
}
array_multisort($sortarray,$tmpUsers);
echo '<pre>';
print_r($tmpUsers);
?>