コード例 #1
0
function queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck)
{
    // connecting to db
    DatabaseConnect();
    $query = "SELECT *  FROM children where child_id='{$id}' and existsinremotedb='{$isInDBCheck}' ;";
    $result = pg_query($query) or die('Query failed: ' . pg_last_error());
    // user node
    unset($response["success"]);
    unset($response["message"]);
    $response["children"] = array();
    $idColName = "child_id";
    $FnameColName = "child_firstname";
    $LnameColName = "child_lastname";
    $genderColName = "child_gender";
    $birthdateColName = "child_birthdate";
    $addressColName = "child_address";
    $postalcodeColName = "child_postalcode";
    $phonenumColName = "child_phonenum";
    $existsColName = "existsinremotedb";
    $lastModifiedColName = "lastmodified";
    // temp user array
    // In $row[] the actual column names from the remote database must be within the squre brackets
    $children = array();
    $children["{$idColName}"] = $row["{$idColName}"];
    $children["{$FnameColName}"] = $row["{$FnameColName}"];
    $children["{$LnameColName}"] = $row["{$LnameColName}"];
    $children["{$genderColName}"] = $row["{$genderColName}"];
    $children["{$birthdateColName}"] = $row["{$birthdateColName}"];
    $children["{$addressColName}"] = $row["{$addressColName}"];
    $children["{$postalcodeColName}"] = $row["{$postalcodeColName}"];
    $children["{$phonenumColName}"] = $row["{$phonenumColName}"];
    $children["{$existsColName}"] = $row["{$existsColName}"];
    $children["{$lastModifiedColName}"] = $row["{$lastModifiedColName}"];
    // push single user into final response array
    array_push($response["children"], $children);
    $isNewer = localDBDateisNewer($children["{$lastModifiedColName}"], $lastmodifiedCheck);
    //	Primary keys is child_id so they are not allowed to be changed
    //If data in the local database is newer
    if ($isNewer) {
        //If first name has been updated
        if (isDifFromRemoteDB($children["{$FnameColName}"], $firstnameCheck)) {
            updateRemoteDB($id, $children["{$FnameColName}"], $firstnameCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$LnameColName}"], $lastnameCheck)) {
            updateRemoteDB($id, $children["{$LnameColName}"], $lastnameCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$genderColName}"], $genderCheck)) {
            updateRemoteDB($id, $children["{$genderColName}"], $genderCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$birthdateColName}"], $birthdateCheck)) {
            updateRemoteDB($id, $children["{$birthdateColName}"], $birthdateCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$addressColName}"], $addressCheck)) {
            updateRemoteDB($id, $children["{$addressColName}"], $addressCheck);
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$postalcodeColName}"], $postalcodeCheck)) {
            updateRemoteDB($id, $children["{$postalcodeColName}"], $postalcodeCheck);
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($children["{$phonenumColName}"], $phonenumCheck)) {
            updateRemoteDB($id, $children["{$phonenumColName}"], $phonenumCheck);
            queryRemoteDBForUpdatedValues($id, $firstnameCheck, $lastnameCheck, $genderCheck, $birthdateCheck, $addressCheck, $postalcodeCheck, $phonenumCheck, $isInDBCheck, $lastmodifiedCheck);
        } else {
            updateRemoteDB($id, $children["{$lastModifiedColName}"], $lastmModifiedCheck);
        }
    } elseif (!$isNewer) {
        array_push($response["children"], $children);
        // success tag for android application to handle
        $response["success"] = 4;
        $response["message"] = "Local DB is not newer";
        echo json_encode($response);
        unsetResponseArray();
    } else {
        unset($response["success"]);
        unset($response["message"]);
        $response["success"] = 3;
        $respone["message"] = "No updates needed";
        // echoing JSON response
        echo json_encode($response);
        unsetResponseArray();
    }
}
コード例 #2
0
function queryRemoteDBForUpdatedValues($idCheck, $nameCheck, $addressCheck, $phonenumCheck, $descriptionCheck, $isInDBCheck, $lastmodifiedCheck)
{
    // connecting to db
    DatabaseConnect();
    $query = "SELECT *  FROM institutions where institution_id='{$id}' and existsinremotedb='{$isInDBCheck}' ;";
    $result = pg_query($query) or die('Query failed: ' . pg_last_error());
    $response["institutions"] = array();
    $idColName = "institution_id";
    $nameColName = "institution_name";
    $addressColName = "institution_address";
    $phonenumColName = "institution_phonenum";
    $descriptionColName = "institution_description";
    $existsColName = "existsinremotedb";
    $lastModifiedColName = "lastmodified";
    $institutions = array();
    $institutions["{$idColName}"] = $row["{$idColName}"];
    $institutions["{$nameColName}"] = $row["{$nameColName}"];
    $institutions["{$addressColName}"] = $row["{$addressColName}"];
    $institutions["{$phonenumColName}"] = $row["{$phonenumColName}"];
    $institutions["{$descriptionColName}"] = $row["{$descriptionColName}"];
    $institutions["{$existsColName}"] = $row["{$existsColName}"];
    $institutions["{$lastModifiedColName}"] = $row["{$lastModifiedColName}"];
    // push single user into final response array
    array_push($response["institutions"], $institutions);
    $isNewer = localDBDateisNewer($institutions["{$lastModifiedColName}"], $lastmodifiedCheck);
    /*
     *	Primary keys are username, first name, and last name so they are not allowed to be changed
     */
    //If data in the local database is newer
    if ($isNewer) {
        //If password has been updated
        if (isDifFromRemoteDB($institutions["{$nameColName}"], $nameCheck)) {
            updateRemoteDB($idCheck, $institutions["{$nameColName}"], $nameCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($idCheck, $nameCheck, $addressCheck, $phonenumCheck, $descriptionCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($institutions["{$addressColName}"], $addressCheck)) {
            updateRemoteDB($idCheck, $institutions["{$addressColName}"], $addressCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($idCheck, $nameCheck, $addressCheck, $phonenumCheck, $descriptionCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($institutions["{$phonenumColName}"], $phonenumCheck)) {
            updateRemoteDB($idCheck, $institutions["{$phonenumColName}"], $phonenumCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($idCheck, $nameCheck, $addressCheck, $phonenumCheck, $descriptionCheck, $isInDBCheck, $lastmodifiedCheck);
        } elseif (isDifFromRemoteDB($institutions["{$descriptionColName}"], $descriptionCheck)) {
            updateRemoteDB($idCheck, $institutions["{$descriptionColName}"], $descriptionCheck);
            //Recursive call to check for any other updated values
            queryRemoteDBForUpdatedValues($idCheck, $nameCheck, $addressCheck, $phonenumCheck, $descriptionCheck, $isInDBCheck, $lastmodifiedCheck);
        } else {
            updateRemoteDB($idCheck, $institutions["{$lastModifiedColName}"], $lastmModifiedCheck);
        }
    } elseif (!$isNewer) {
        array_push($response["institutions"], $institutions);
        // success tag for android application to handle
        $response["success"] = 4;
        $response["message"] = "Local DB is not newer";
        echo json_encode($response);
        unsetResponseArray();
    } else {
        $response["success"] = 3;
        $respone["message"] = "No updates needed";
        // echoing JSON response
        echo json_encode($response);
        unsetResponseArray();
    }
}