My first web game v.PHP yah!
And below are my php codes.
Page_1
<?php
/*
-----
This script makes you drink more merrily.
-----
*/
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Drinking Game!</title>
</head>
<body>
<h1 align="center">Drink or Dare!</h1>
<p align="center"><img src="beer.png" alt="Beer"/></p>
<p align="center">How many players are here? <br/><strong>Index</strong> each one of you and
<strong>submit</strong> the total number yo!</p>
<form align="center" action="/web_02/player_2.php" method="post">
<div>
<label for="players">Players #:</label>
<input type="number" min="1" max="20" step="1" name="players" />
</div>
<div class="button">
<button type="submit">submit.</button>
</div>
</form>
</body>
</html>
Page_2
<?php
session_start();
if ( empty($_POST['players']) ) {
echo "Please enter the number of players!";
} else {
$players = $_POST['players'];
$aPlayer = rand(1, $players);
do {
$bPlayer = rand(1, $players);
} while ($bPlayer == $aPlayer);
$_SESSION['aPlayer'] = $aPlayer;
$_SESSION['bPlayer'] = $bPlayer;
$title = "Congrats Player Num<strong>" . $aPlayer . "</strong>, you're chosen!";
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title>
<?php
if ( empty($_POST['players']) ) {
echo "Oops!";
} else {
echo "Hi Player$aPlayer!";
}
?>
</title>
</head>
<body>
<p align="center"><?php echo $title ?><br/>
Now please fill out the form below.</p>
<form align="center" action="/web_02/form_2.php" method="post">
<div>
<label for="name">Your name:</label>
<input type="text" name="name" />
</div>
<div>
<label for="amount">How many glasses will you drink:</label>
<input type="number" min="0" max="4" step="1" name="amount" />
</div>
<div>
<label for="partA">Favorite human part:</label>
<input type="text" name="partA" />
</div>
<div>
<label for="partB">Less favorite human part:</label>
<input type="text" name="partB" />
</div>
<div class="button">
<button type="submit">I made my mind.</button>
</div>
</form>
</body>
</html>
Page_3
<?php
session_start();
$playerA = $_SESSION['aPlayer'];
$playerB = $_SESSION['bPlayer'];
$amount = $_POST['amount'];
$partA = $_POST['partA'];
$partB = $_POST['partB'];
$actions = array(
4 => 'blow to',
3 => 'touch',
2 => 'kiss',
1 => 'bite',
0 => 'lick'
);
$do = $actions[$amount];
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Obey the order!</title>
</head>
<body>
<h1 align="center">
<?php
echo "Player$playerA, please $do the $partA of Player$playerB.<br/>
Or you can choose to let Player$playerB $do your $partB.<br/>
<br/>
Don't be shy, Player$playerA and Player$playerB.";
$name = "$playerA";
$message = " chose drink $amount beer(s),<br/>
so he/she has to either $do the $partA of Player$playerB, <br/>
or let Player$playerB $do his/her $partB.<br/><br/>";
$dsn = 'mysql:dbname=jhclaura_newbie;host=mysql.jhclaura.com';
$user = 'jhclauracom';
$password = 'NpE^yQ7c';
/* Create PDO object */
$pdo = new PDO($dsn, $user, $password);
/* Write the SQL */
$sql = "INSERT INTO drinkResult (name,message) VALUES(?,?)";
/* prepare the SQL statement and return a PDOStatement object */
$statement = $pdo->prepare($sql);
/* Execute the SQL with the data */
if ($statement->execute(array($name,$message))){ // returns true if it worked
// echo "Success! New message added.";
} else {
echo "There was an error and the post could not be added.<br/>n";
// Debug: show the error message
print_r( $statement->errorInfo());
}
?>
</h1>
<p align="center">
<a href="drinking_game.php">Again!</a></li>
</p>
<p align="center"><strong>History of DrinkingGame</strong>
</p>
<p align="center">
<?php
$getSql = 'SELECT * FROM drinkResult';
$statement = $pdo->query($getSql);
$result = $statement->fetchAll();
foreach ($result as $row) {
echo "Player";
echo $row['name'];
echo $row['message'];
echo "n";
}
?>
</p>
</body>
</html>
