Created
January 14, 2016 10:00
-
-
Save larikraun/e55fa332e9bf14f586da to your computer and use it in GitHub Desktop.
Update
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Omolara Adejuwon | |
* Date: 18/11/2015 | |
* Time: 18:22 | |
*/ | |
require_once("./DataSyncHandler.php"); | |
$dataSyncHandler = new DataSyncHandler(); | |
header("Content-Type: application/json"); //set content type to application/json | |
if (isset($_GET["operation"])) { | |
$operation = $_GET["operation"]; | |
switch ($operation) { | |
case "compare": | |
if (isset($_GET["tableName"]) && isset($_GET["ids"])) { | |
$tableName = $_GET["tableName"]; | |
$ids = $_GET["ids"]; | |
header("HTTP/1.1 200 OK"); | |
echo json_encode(array("response_code" => 200, "data" => $dataSyncHandler->compareData($tableName, $ids))); | |
} else { | |
header("HTTP/1.1 400 Bad Request"); | |
echo json_encode(array("response_code" => 400, "message" => "Some parameters are missing")); | |
} | |
break; | |
case "upload": | |
if (isset($_GET["tableName"])) { | |
$tableName = $_GET["tableName"]; | |
$json = file_get_contents("php://input"); | |
$dataArray = json_decode($json, true); | |
$response = array(); | |
if (is_array($dataArray) || is_object($dataArray)) { | |
print_r($dataArray["data"]); | |
$response = array(); | |
for ($x = 0; $x < count($dataArray["data"]); $x++) { | |
echo "<br>"; | |
$response = $dataSyncHandler->getAndInsertDataFromDevice($tableName, $dataArray["data"][$x]); | |
// print_r(array_values($dataArray[$key][0])); | |
} | |
} | |
header("HTTP/1.1 200 Bad Request"); | |
echo json_encode(array("response_code" => 200, "data" => $response)); | |
} else { | |
header("HTTP/1.1 400 Bad Request"); | |
echo json_encode(array("response_code" => 400, "message" => "Some parameters are missing")); | |
} | |
break; | |
case "download": | |
if (isset($_GET["tableName"]) && isset($_GET["ids"])) { | |
$tableName = $_GET["tableName"]; | |
$ids = $_GET["ids"]; | |
header("HTTP/1.1 200 OK"); | |
echo json_encode(array("response_code" => 200, "data" => $dataSyncHandler->sendDataToDevice($tableName, $ids))); | |
} else { | |
header("HTTP/1.1 400 Bad Request"); | |
echo json_encode(array("response_code" => 400, "message" => "Some parameters are missing")); | |
} | |
break; | |
} | |
} else { | |
header("HTTP/1.1 400 Bad Request"); | |
echo json_encode(array("response_code" => 400, "message" => "Some parameters are missing")); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Created by PhpStorm. | |
* User: Omolara Adejuwon | |
* Date: 18/11/2015 | |
* Time: 11:10 | |
*/ | |
require_once("./db_function.php"); | |
class DataSyncHandler | |
{ | |
private $tableName; | |
private $db; | |
public function __construct($tableName = "users") | |
{ | |
//$this->tableName = $tableName; | |
$this->db = new DB_Function(); | |
} | |
public function compareData($tableName = "users", $idsFromDevice = "35,10,1,5,32,9") | |
{ | |
$idsFromDeviceArray = explode(",", $idsFromDevice); | |
$idResource = $this->db->getIDs($tableName); | |
$idsArray = array(); | |
while ($row = mysqli_fetch_assoc($idResource)) { | |
$idsArray[] = $row["id"]; | |
} | |
$a = $this->flip_isset_diff($idsFromDeviceArray, $idsArray);//returns missing ids from server | |
$b = $this->large_array_diff($idsFromDeviceArray, $idsArray); | |
$c = $this->my_array_diff($idsFromDeviceArray, $idsArray); | |
$d = $this->flip_isset_diff($idsArray, $idsFromDeviceArray); //returns missing ids from device | |
$e = $this->large_array_diff($idsArray, $idsFromDeviceArray); | |
$f = $this->my_array_diff($idsArray, $idsFromDeviceArray); | |
return array("flip" => $a, "diff" => $b, "my_diff" => $c, "flip_device" => $d, "diff_device" => $e, "my_diff_device" => $f); | |
} | |
function flip_isset_diff($b, $a) | |
{ | |
$start = microtime(true); | |
$at = array_flip($a); | |
$d = array(); | |
foreach ($b as $i) | |
if (!isset($at[$i])) | |
$d[] = $i; | |
echo (microtime(true) - $start) . PHP_EOL; | |
return $d; | |
} | |
function large_array_diff($b, $a) | |
{ | |
// Flipping | |
$start = microtime(true); | |
$at = array_flip($a); | |
$bt = array_flip($b); | |
// checking | |
$d = array_keys(array_diff_key($bt, $at)); | |
echo (microtime(true) - $start) . PHP_EOL; | |
return $d; | |
} | |
function my_array_diff($b, $a) | |
{ | |
$start = microtime(true); | |
$d = array_values(array_diff($b, $a)); | |
echo (microtime(true) - $start) . PHP_EOL; | |
return $d; | |
} | |
function getAndInsertDataFromDevice($tableName = "users", $dataFromDevice) | |
{ | |
$columns = implode(", ", array_keys($dataFromDevice)); | |
$escaped_values = array_map(array($this, 'escape'), $dataFromDevice); | |
$values = "'" . implode("','", $escaped_values) . "'"; | |
return $this->db->insertData($tableName, $columns, $values); | |
} | |
function escape($array) | |
{ | |
return mysql_real_escape_string($this->db->mysqli, $array); | |
} | |
function sendDataToDevice($tableName = "users", $idsFromDevice = "12,26,29") | |
{ | |
$idResource = $this->db->getDataWhereID($tableName, $idsFromDevice); | |
$idsArray = array(); | |
while ($row = mysqli_fetch_assoc($idResource)) { | |
$idsArray[] = $row; | |
} | |
return $idsArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment