<?php
// create connection to database
$host = "webdev.iyaserver.com"; // database server address
$userid = "dent_student"; // database user account name
$userpw = "code4Studentuse"; // database user password
$db = "dent_health"; // database name

$mysql = new mysqli(
    $host,
    $userid,
    $userpw,
    $db);

if($mysql->connect_errno) {
    echo "db connection error : " . $mysql->connect_error;
    exit();
}
?>

<!-- these php blocks do not need to be separated, but might as well since in many pages with
    full/normal html likely this block would be lower down in the code -->

<?php

/* php variable notes:
    -  php variables have dollar signs in front of their names, like $filter
    - php variables passed through page url strings are named $_REQUEST["variablename"]
    - if a $_REQUEST variable does not exist it does NOT error, it just returns blank
*/

// going to write some php variable and $_REQUEST code here


// base query
$sql =  "     SELECT 
	                AVG(Age) AS AverageAge,
	                MIN(Age) AS LowAge,
	                MAX(Age) AS HighAge,
	                COUNT(Name) AS NumberPatients
	              FROM healthcare_dataset
	              WHERE MedicalCondition='Diabetes'
	";

$results = $mysql->query($sql);

if(!$results) {
    echo "SQL error: ". $mysql->error . " running query <hr>" . $sql . "<hr>";
    exit();
}

// since only query in the page, and just a single line query (one row) that
// does not need a loop, pre-load the query columns into $currentrow
$currentrow = $results->fetch_assoc();

// dump columns into source code of page to debug / see column names and data
echo "<!--<pre> ";
var_dump($currentrow);
echo "</pre>-->";
?>



<!-- this is a javascript block in the page... NOT php ... but that doesn't mean we
    can't choose to output/inject content into it with php -->

<script><!-- javascript block. setting up variables -->
    // Note that javascript (and jQuery) variables have no symbol in front of the variable name
    var condition = "Diabetes";
    var patients = 0;
    var highage = 0;
    var lowage = 0;
    var avgage = 0;
</script>

<style>
    .box { width: 30px; height: 15px; border: 1px solid teal;}
    .box2 { width: 100px; height:15px; border: 1px solid teal; }
</style>

<div id="chart">

    Patients summary report:
    <hr>
    Condition:  <div class="box2" id="condition"></div>
    <br>
    Number of Patients: <div class="box" id="patients"></div>
    <br>
    Average age:  <div class="box" id="lowage"></div>
    <br>
    Lowest age:  <div class="box" id="lowage"></div>
    <br>
    Highest age: <div class="box" id="highage"></div>

</div>



<script src="https://code.jquery.com/jquery-3.7.1.js"
integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4="
crossorigin="anonymous"></script>
<script><!-- jQuery block -->
$(document).ready(function(){
    // jQuery code
    // output in alert value of avgage variable
 //   alert( "avgage =" +  avgage );

    // write variables to div boxes (by id)

    // then go back to php, and put output php values in place of 0s

});
</script>



