Skip to content

Commit

Permalink
php files
Browse files Browse the repository at this point in the history
  • Loading branch information
reed-jones committed Mar 22, 2017
1 parent e21e8df commit 063e5f9
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
35 changes: 35 additions & 0 deletions addHighScores_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
//Connect to DB
require("../connect.php");

$name = $_POST['n'];
$time = intval($_POST['t']);
$difficulty = intval($_POST['d']);

if ($name === NULL || $time === NULL || $difficulty === NULL){
exit();
//exit("null values found");
}
if (is_nan($time) || !is_numeric($time) || $time < 0){
exit();
//exit("invalid difficulty");
}
if (is_nan($difficulty) || $difficulty > 2 || $difficulty < 0){
exit();
//exit("invalid difficulty");
}

// set default name if no name was supplied
if($name == "") {
$name = "Unknown";
}

// if name is too long for the database, only grab the first 25 letters (since database limit is a varchar(25))
$name = substr($name, 0, 25);

// Prepare a query for execution
$result = pg_prepare($con, "my_query", 'INSERT INTO _Table_(name, time, difficulty) VALUES($1,$2,$3)');
// Execute the prepared query.
$result = pg_execute($con, "my_query", array($name, $time, $difficulty));

?>
5 changes: 5 additions & 0 deletions connect_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
// connection to postgresql database
$conn_string = "host=localhost dbname=DB_NAME user=DB_USERNAME password=PASSWORD";
$con = pg_connect($conn_string) or die ("Could Not Connect to High Scores Database");
?>
94 changes: 94 additions & 0 deletions getHighScores_example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
//Connect to DB
require("../connect.php");
?>

<!--Build HTML table to hold highscores. This implimentation uses bootstrap -->
<div class="row">
<div class="" id="diffEasy">
<h4>Easy</h4>
<hr>
<div class="row">
<div class="col-xs-6">
<h4>Name</h4>
</div>
<div class="col-xs-6">
<h4>Time</h4>
</div>

<!-- recieve top 10 ranked Easy players -->
<?php $result = pg_query($con, "SELECT name, time FROM _Table_ WHERE difficulty = 0 ORDER BY time, id LIMIT 10");
if (!$result) {
echo "An error occurred.\n";
exit;
}

while ($row = pg_fetch_array($result)) { ?>
<div class="row">
<div class="col-xs-6">
<?php echo $row['name']; ?>
</div>
<div class="col-xs-6">
<?php echo $row['time']; ?>
</div>
</div>
<?php } ?>

</div>
</div>
<div class="" id="diffMedium">
<h4>Medium</h4>
<hr>
<div class="row">
<div class="col-xs-6">
<h4>Name</h4>
</div>
<div class="col-xs-6">
<h4>Time</h4>
</div>

<!--Recieve top 10 ranked Medium players-->
<?php $result = pg_query($con, "SELECT name, time FROM _Table_ WHERE difficulty = 1 ORDER BY time, id LIMIT 10");
while ($row = pg_fetch_array($result)) { ?>

<div class="row">
<div class="col-xs-6">
<?php echo $row['name']; ?>
</div>
<div class="col-xs-6">
<?php echo $row['time']; ?>
</div>
</div>
<?php } ?>
</div>
</div>
<div class="" id="diffHard">
<h4>Hard</h4>
<hr>
<div class="row">
<div class="col-xs-6">
<h4>Name</h4>
</div>
<div class="col-xs-6">
<h4>Time</h4>
</div>

<!--Recieved top 10 ranked Difficult players-->
<?php $result = pg_query($con, "SELECT name, time FROM _Table_ WHERE difficulty = 2 ORDER BY time, id LIMIT 10");
while ($row = pg_fetch_array($result)) { ?>
<div class="row">
<div class="col-xs-6">
<?php echo $row['name']; ?>
</div>
<div class="col-xs-6">
<?php echo $row['time']; ?>
</div>
</div>
<?php } ?>
</div>
</div>
</div>




0 comments on commit 063e5f9

Please sign in to comment.