Skip to content

Instantly share code, notes, and snippets.

@d8ta
Last active August 29, 2015 14:17
Show Gist options
  • Save d8ta/b2bc72dfffd2354714f9 to your computer and use it in GitHub Desktop.
Save d8ta/b2bc72dfffd2354714f9 to your computer and use it in GitHub Desktop.
<?php
$DB_NAME = "test";
$DB_USER = "root";
$DB_PASS = "root";
$DSN = "mysql:dbname=$DB_NAME;host=localhost";
?>
<?php
include "config.php";
session_start();
if ( !$DB_NAME )
{
die("No database connection or database does not exist");
}
try {
$dbh = new PDO($DSN, $DB_USER, $DB_PASS);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
$dbh->exec('SET CHARACTER SET utf8') ;
} catch (Exception $e) {
die("Problem connecting to database $DB_NAME as $DB_USER: " . $e->getMessage() );
}
?>
<!DOCTYPE html>
<html>
<head>
<link type='text/css' rel='stylesheet' href='style.css'/>
<title>Coin Flips</title>
</head>
<body>
<p>We are going to flip a coin until we get three heads in a row!</p>
<?php
include "function.php";
//counting all flips
$coinFlip = 0;
//counting all heads
$headCount = 0;
//while condition
while ($headCount < 3) {
$coinFlip ++;
$flip = mt_rand(0, 1); // randomiser, if 0 it should be Tail if 1 it should be Head
if ($flip == 1) {
$headCount ++;
echo "<div class=\"coin\"> H </div>";
}
else {
$headCount = 0;
echo "<div class=\"coin\"> T </div>";
}
}
print "<p> It took {$coinFlip} coinflips to get three H in a row";
if(isset($_SESSION['USER']))
{
//write flips to the DB
$sql = "INSERT INTO 'Coins'('allFlips') VALUES ($$coinFlip)";
$dbh->exec($sql);
echo "New record created successfully";
}
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment