Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
barebones phonebook
  • Loading branch information
Joel Salisbury committed Apr 5, 2017
1 parent c1a1808 commit e7bebbb
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
25 changes: 25 additions & 0 deletions database.php
@@ -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 = "phonebk";

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

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

// 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;
}
41 changes: 41 additions & 0 deletions person.php
@@ -0,0 +1,41 @@
<?php
require_once 'database.php';
class Person {

protected $id;
protected $firstname;
protected $lastname;
protected $email;
protected $phone;

public function __construct($id=0) {
if ($id !== 0) {
$db = getDatabase();
$query = $db->prepare( "SELECT * from people 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->firstname = $data->firstname;
$this->lastname = $data->lastname;
$this->phone = $data->phone;
$this->email = $data->email;
}
}


public function get () {
$res['id'] = $this->id;
$res['firstname'] = $this->firstname;
$res['lastnamename'] = $this->lastname;
$res['email'] = $this->email;
$res['phone'] = $this->phone;
return $res;
}

}
38 changes: 38 additions & 0 deletions phonebook.php
@@ -0,0 +1,38 @@
<?php
header( "Access-Control-Allow-Origin: *");
header('Content-Type: application/json');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");

require_once 'database.php';
require_once 'person.php';


if( isset( $_REQUEST['do'] ) ){
switch($_REQUEST['do']) {
case "getPerson":
$did = $_GET['id'];
$person = new Person($did);
$data = $person->get();
break;

case 'getAllPeople':
$db = getDatabase();
$query = $db->prepare( "SELECT * from people" );
$query->execute();
$query->setFetchMode( PDO::FETCH_OBJ );
$data = $query->fetchAll();
break;


}

prepareResponse($data);

} else {
die("You need to specify an operation.");
}


function prepareResponse( $arr ) {
echo json_encode( $arr );
}

0 comments on commit e7bebbb

Please sign in to comment.