Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
3 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ); | ||
} |