Skip to content

Commit

Permalink
added sample object and API file.
Browse files Browse the repository at this point in the history
  • Loading branch information
joelsalisbury committed Dec 7, 2016
1 parent feb7c27 commit 97b1c1e
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/antibiotic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

class Diagnosis
25 changes: 25 additions & 0 deletions api/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
function getDatabase() {
// tells php that the database lives on the same server as the php file
//$dbhost = "localhost";
$dbhost = "develop.digitalmediauconn.org";

// this is the username
$dbuser = "abxrls";

// this is the password
$dbpass = "abxrls";

// name of the DB that we set up together
$dbname = "ccmc_antibiotics";

// this just makes a little config line for PHP to read
$mysql_conn_string = "mysql:host=$dbhost;dbname=$dbname";

//setup a new connection using the stuff up above
$database = new PDO($mysql_conn_string, $dbuser, $dbpass);
$database->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


return $database;
}
11 changes: 11 additions & 0 deletions api/diag_info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

require_once 'diagnosis.php';

$diag_id = $_GET['id'];

$diag = new Diagnosis($diag_id);

echo "<pre>";
print_r($diag->get());
echo "</pre>";
50 changes: 50 additions & 0 deletions api/diagnosis.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
include 'database.php';
class Diagnosis {

protected $id;
protected $name;
protected $notes;
protected $relatedAntibiotics;

public function __construct($id=0) {
if ($id !== 0) {
$db = getDatabase();
$query = $db->prepare( "SELECT * from diagnosis WHERE id = :id " ); // return all fields (*) from the table WHERE the id is the var $id passed to the function
$query->bindParam( ':id', $id );
$query->execute(); // Run the SQL query on our database
$query->setFetchMode( PDO::FETCH_OBJ ); // A PHP method to tell the database we want to return all the data as objects with each table represented as a property of that object. This will be the case for most of our calls that return information from the database. http://php.net/manual/en/pdo.constants.php
$data = $query->fetchAll();

$data = $data[0];

// Build our local object
$this->id = $data->id;
$this->name = $data->name;
$this->notes = $data->notes;
$this->buildRelatedAntibiotics();
}
}

private function buildRelatedAntibiotics () {
$db = getDatabase();
$query = $db->prepare( "SELECT * from diagnosis_antibiotic WHERE diagnosis_id = :id " ); // return all fields (*) from the table WHERE the id is the var $id of current diagnosis
$query->bindParam( ':id', $this->id );
$query->execute(); // Run the SQL query on our database
$query->setFetchMode( PDO::FETCH_OBJ );
$data = $query->fetchAll();


$this->relatedAntibiotics = $data;
}

public function get () {
$res['id'] = $this->id;
$res['name'] = $this->name;
$res['notes'] = $this->notes;
$res['relatedAntibiotics'] = $this->relatedAntibiotics;

return $res;
}

}

0 comments on commit 97b1c1e

Please sign in to comment.