Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server-side: added new logic Read from database #1

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.txt
*.txt
*.vscode
62 changes: 62 additions & 0 deletions src/includes/create_record.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
include('db.ini.php');

if(!$conn) {
die("Connection Failed: " . mysqli_connect_error());
}

$json = file_get_contents('php://input');
$data = json_decode($json);

$sql = "INSERT INTO users (first_name, last_name, age, civil_status, occupation, address) VALUES('{$data->first_name}', '{$data->last_name}', {$data->user_age}, '{$data->civil_status}', '{$data->user_occupation}', '{$data->user_address}')";

if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);

$covid = "INSERT INTO covid (user_id, covid_status, days, covid_case) VALUES($last_id, '{$data->covid_status}', {$data->quarantine_days}, '{$data->covid_case}')";

if(mysqli_query($conn, $covid)) {
$last_id_covid = mysqli_insert_id($conn);
$travel_json = $data->travel_history;
$contact_json = $data->contact_history;
foreach ($travel_json as $index) {
$travel = "INSERT INTO travel_history (user_travel, location, travel_date) VALUES($last_id_covid, '{$index->travel_location}', '{$index->travel_date}')";

if (mysqli_query($conn, $travel) ){
// echo 'Sucessfully added new record to travel history table';
} else {
echo "Error: " . $travel . "<br>" . mysqli_error($conn);
}

}


foreach ($contact_json as $contact_index) {
$contact = "INSERT INTO contact_trace (user_case_id, person_name, contact_date, address) VALUES($last_id_covid, '{$contact_index->interact_name}', '{$contact_index->interact_date}', '{$contact_index->interact_location}')";

if (mysqli_query($conn, $contact) ){
// echo 'Sucessfully added new record to contact trace table';
} else {
echo "Error: " . $contact . "<br>" . mysqli_error($conn);
}

}
// echo "Successfully added new record to covid table";
} else {
echo "Error: " . $covid . "<br>" . mysqli_error($conn);
}

// echo "New record created successfully ". $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


$returnarr[] = array("new_id" => $last_id, "travel" => $data->travel_history, "contact" => $data->contact_history);
echo json_encode($returnarr);
mysqli_close($conn);




// echo $data;
8 changes: 8 additions & 0 deletions src/includes/db.ini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

$serverName = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbName = "pandemic";

$conn = mysqli_connect($serverName, $dbUsername, $dbPassword, $dbName);
25 changes: 25 additions & 0 deletions src/includes/delete_history.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
include('db.ini.php');

$json = file_get_contents('php://input');
$data = json_decode($json);
echo $data->id;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
if ($data->table == 'interact-detail') {
$sql = "DELETE FROM contact_trace WHERE contact_id={$data->id}";
} else {
$sql = "DELETE FROM travel_history WHERE travel_id={$data->id}";
}

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
20 changes: 20 additions & 0 deletions src/includes/delete_record.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
include('db.ini.php');
$json = file_get_contents('php://input');
$data = json_decode($json);
echo $data->id;
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

// sql to delete a record
$sql = "DELETE FROM users WHERE id={$data->id}";

if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>
120 changes: 120 additions & 0 deletions src/includes/detailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

function detailed () {
include('db.ini.php');
// $serverName = "localhost";
// $dbUsername = "root";
// $dbPassword = "";
// $dbName = "pandemic";

// $conn = mysqli_connect($serverName, $dbUsername, $dbPassword, $dbName);
if(!$conn) {
die("Connection Failed: " . mysqli_connect_error());
}
$json = file_get_contents('php://input');
$data = json_decode($json);
$sql = "SELECT * FROM users WHERE id={$data->userId}";
$covidResults;
$returnArray = array();
$results = mysqli_query($conn, $sql);


if (mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {

$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$age = $row['age'];
$occupation = $row['occupation'];
$civil_status = $row['civil_status'];
$travel_data = array();
$contact_data = array();
// $days;
// $covid_status;
// $covid_case;
// $travel_location;
// $travel_date;
// $contact_person;
// $contact_date;
// $contact_address;
// <li>First Name: <span clsas="info-first">Susan</span></li>
// <li>Last Name: <span clsas="info-last">Williams</span></li>
// <li>Age: <span clsas="info-age">25</span></li>
// <li>Occupation: <span clsas="info-occupation">Stock Investor</span> </li>
// <li>Civil Status: <span clsas="info-civil">Single</span></li>
// <li>Status: <span clsas="info-status">Positive</span></li>
// <li>Day of Quarantine: <spa clsas="info-day">14days</spa></li>
// <li>Covid Case: <span clsas="info-case">Asymptomatic</span></li>
// ?
// echo '<li>First Name: <span class="info-first">'. $row['first_name'] .'</span></li>
// <li>Last Name: <span class="info-last">'. $row['last_name'] .'</span></li>
// <li>Age: <span class="info-age">'. $row['age'] .'</span></li>
// <li>Occupation: <span class="info-occupation">'. $row['occupation'] .'</span></li>
// <li>Civil Status: <span class="info-civil">'. $row['civil_status'] .'</span></li>';

$covidResults = mysqli_query($conn, "SELECT * FROM covid JOIN users ON users.id=covid.user_id WHERE users.id={$row['id']}");


if(mysqli_num_rows($covidResults) > 0) {
while($row1 = mysqli_fetch_assoc($covidResults)){
$travelResults = mysqli_query($conn, "SELECT * FROM travel_history JOIN covid ON covid.case_id=travel_history.user_travel WHERE travel_history.user_travel={$row1['case_id']}");
$contactResults = mysqli_query($conn, "SELECT * FROM contact_trace JOIN covid ON covid.case_id=contact_trace.user_case_id WHERE contact_trace.user_case_id={$row1['case_id']}");

if (mysqli_num_rows($travelResults) > 0) {
while($row2 = mysqli_fetch_assoc($travelResults)) {
$travel_data[] = array("travel_location"=> $row2['location'],
"travel_date" => $row2['travel_date'],
);
// $travel_location = $row2['location'];
// $travel_date = $row2['travel_date'];
}
}

if (mysqli_num_rows($contactResults) > 0) {
while($row3 = mysqli_fetch_assoc($contactResults)) {
$contact_data[] = array("contact_person" => $row3['person_name'],
"contact_date" => $row3['contact_date'],
"contact_address" => $row3['address'],

);
// $contact_person = $row3['person_name'];
// $contact_date = $row3['contact_date'];
// $contact_address = $row3['address'];
}
}

// echo '<li>Covid Status: <span class="info-status">'. $row1['covid_status'] .'</span></li>
// <li>Day of Quarantine: <span class="info-day">'. $row['days'] .'</span></li>
// <li>Covid Case: <span class="info-case">'. $row['covid_case'] .'</span></li>';
$covid_case = $row1['covid_case'];
$days = $row1['days'];
$covid_status = $row1['covid_status'];
}
}


$return_arr[] = array("id" => $id,
"first_name" => $first_name,
"last_name" => $last_name,
"age" => $age,
"occupation" => $occupation,
"civil_status" => $civil_status,
"days" => $days,
"covid_case" => $covid_case,
"covid_status" => $covid_status,
"contact_data" => $contact_data,
"travel_data" => $travel_data,
);

}

echo json_encode($return_arr);
} else {
echo '<h1>no rows</h1>';
}
mysqli_close($conn);
}


detailed();
135 changes: 135 additions & 0 deletions src/includes/search.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php
include('db.ini.php');
if(!$conn) {
die("Connection Failed: " . mysqli_connect_error());
}
$json = file_get_contents('php://input');
$data = json_decode($json);
// $val = $_POST['search-value'];
$valPiece = explode(" ", $data->val);
if($valPiece[0] != 'patience' and $valPiece[0] != 'under' and $valPiece[0] != 'legal' ) {
if(count($valPiece) < 2) {
$sql = "SELECT * FROM users WHERE first_name LIKE '%{$valPiece[0]}%' or last_name LIKE '%{$valPiece[0]}%'";
} else {
$sql = "SELECT * FROM users WHERE first_name LIKE '%{$valPiece[0]}%' or last_name LIKE '%{$valPiece[1]}%'";
}
}else {
if($valPiece[0] == 'under') {
$sql = "SELECT * FROM users WHERE age < 18";
} else if ($valPiece[0] == 'legal') {
$sql = "SELECT * FROM users WHERE age > 18";
} else if ($valPiece[0] == 'patience') {
$sql = "SELECT * FROM users JOIN covid ON covid.user_id=users.id WHERE covid_status='Positive' or covid_status='Under Investigation'";
}
}


$results = mysqli_query($conn, $sql);
$id;
$first_name;
$last_name;
$age;
$occupation;
$civil_status;
$travel_data;
$contact_data;
$res;
$i = 0;

if (mysqli_num_rows($results) > 0) {
while($row = mysqli_fetch_assoc($results)) {


// $id[] = $row['id'];
// $first_name[] = $row['first_name'];
// $last_name = $row['last_name'];
// $age = $row['age'];
// $occupation = $row['occupation'];
// $civil_status = $row['civil_status'];
$travel_data = array();
$contact_data = array();


// $days;
// $covid_status;
// $covid_case;
// $travel_location;
// $travel_date;
// $contact_person;
// $contact_date;
// $contact_address;
// <li>First Name: <span clsas="info-first">Susan</span></li>
// <li>Last Name: <span clsas="info-last">Williams</span></li>
// <li>Age: <span clsas="info-age">25</span></li>
// <li>Occupation: <span clsas="info-occupation">Stock Investor</span> </li>
// <li>Civil Status: <span clsas="info-civil">Single</span></li>
// <li>Status: <span clsas="info-status">Positive</span></li>
// <li>Day of Quarantine: <spa clsas="info-day">14days</spa></li>
// <li>Covid Case: <span clsas="info-case">Asymptomatic</span></li>
// ?
// echo '<li>First Name: <span class="info-first">'. $row['first_name'] .'</span></li>
// <li>Last Name: <span class="info-last">'. $row['last_name'] .'</span></li>
// <li>Age: <span class="info-age">'. $row['age'] .'</span></li>
// <li>Occupation: <span class="info-occupation">'. $row['occupation'] .'</span></li>
// <li>Civil Status: <span class="info-civil">'. $row['civil_status'] .'</span></li>';

$covidResults = mysqli_query($conn, "SELECT * FROM covid JOIN users ON users.id=covid.user_id WHERE users.id={$row['id']}");


if(mysqli_num_rows($covidResults) > 0) {
while($row1 = mysqli_fetch_assoc($covidResults)){
$travelResults = mysqli_query($conn, "SELECT * FROM travel_history JOIN covid ON covid.case_id=travel_history.user_travel WHERE travel_history.user_travel={$row1['case_id']}");
$contactResults = mysqli_query($conn, "SELECT * FROM contact_trace JOIN covid ON covid.case_id=contact_trace.user_case_id WHERE contact_trace.user_case_id={$row1['case_id']}");

if (mysqli_num_rows($travelResults) > 0) {
while($row2 = mysqli_fetch_assoc($travelResults)) {
$travel_data[] = array("travel_location"=> $row2['location'],
"travel_date" => $row2['travel_date'],
);
// $travel_location = $row2['location'];
// $travel_date = $row2['travel_date'];
}
}

if (mysqli_num_rows($contactResults) > 0) {
while($row3 = mysqli_fetch_assoc($contactResults)) {
$contact_data[] = array("contact_person" => $row3['person_name'],
"contact_date" => $row3['contact_date'],
"contact_address" => $row3['address'],

);
// $contact_person = $row3['person_name'];
// $contact_date = $row3['contact_date'];
// $contact_address = $row3['address'];
}
}

// echo '<li>Covid Status: <span class="info-status">'. $row1['covid_status'] .'</span></li>
// <li>Day of Quarantine: <span class="info-day">'. $row['days'] .'</span></li>
// <li>Covid Case: <span class="info-case">'. $row['covid_case'] .'</span></li>';

$res[] = array(
"new_id"=>$row['id'],
"first_name" => $row['first_name'],
"last_name" => $row['last_name'],
"age" => $row['age'],
"address" => $row['address'],
"occupation" => $row['occupation'],
"civil_status" => $row['civil_status'],
"covid_case" => $row1['covid_case'],
"days" => $row1['days'],
"covid_status" => $row1['covid_status'],
"contact_data" => $contact_data,
"travel_data" => $travel_data,
);
}
}



}
echo json_encode($res);
} else {
echo '<h1>no rows</h1>';
}
mysqli_close($conn);
Loading