Skip to content

Commit

Permalink
Merge pull request #5 from anvk/random
Browse files Browse the repository at this point in the history
first sketch to add a random decision
  • Loading branch information
cindyli committed Jan 22, 2013
2 parents d476f43 + 23241e5 commit dd6645e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
6 changes: 6 additions & 0 deletions add_places.php
Expand Up @@ -44,6 +44,12 @@
<button type="submit" class="btn" value="submit">Submit</button>
</form>
</li>
<?php if($username == $special_user) { ?>
<li class="divider-vertical"></li>
<li>
<button type="submit" class="btn" value="submit">Pick a random place from the list</button>
</li>
<?php } ?>
</ul>
</div>
</div>
Expand Down
2 changes: 2 additions & 0 deletions include/config.php
Expand Up @@ -5,6 +5,8 @@
$mysql_password = "root";
$mysql_database = "php_overview";

$special_user = "alexcindy";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password)
or die("Error connecting to mysql host: " . mysql_error());
mysql_select_db($mysql_database, $bd) or die("Error connecting to database: " . mysql_error());
Expand Down
6 changes: 2 additions & 4 deletions login.php
Expand Up @@ -3,8 +3,7 @@

session_start();

if(isset($_POST["username"]) && trim($_POST["username"]) != "")
{
if(isset($_POST["username"]) && trim($_POST["username"]) != "") {
// username and password sent from Form
$myusername=addslashes($_POST['username']);

Expand All @@ -13,8 +12,7 @@
$row = mysql_fetch_array($result);

// If result matched $myusername, table should have at least 1 row
if(mysql_num_rows($result) == 0)
{
if(mysql_num_rows($result) == 0) {
// If we did not find a user then let's add to the DB
$sql = sprintf("INSERT INTO users(username) VALUES ('%s')", $myusername);
$result = mysql_query($sql);
Expand Down
36 changes: 36 additions & 0 deletions random.php
@@ -0,0 +1,36 @@
<?php
include_once("include/config.php");

session_start();

if(isset($_SESSION["login_user"]) && trim($_SESSION["login_user"]) == $special_user) {
$sql = "SELECT * FROM places ORDER BY RAND() LIMIT 0,1;";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

// If result matched $myusername, table should have at least 1 row
if(mysql_num_rows($result) != 0) {
$place = $row['place_name'];
}
} else {
header("location: login.php");
exit;
}

?>

<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="css/styles.css">
<div class="navbar">
<div class="navbar-inner">
<ul class="nav">
<li>
<button type="submit" class="btn" value="submit">Go Back</button>
</li>
</ul>
</div>
</div>

<div class="well alert-success">
Aha! We are going to <h2><?php echo $place; ?></h2>
</div>

0 comments on commit dd6645e

Please sign in to comment.