Design an app / site that allow users to vote on their favorite baby names and d
ID: 3916073 • Letter: D
Question
Design an app / site that allow users to vote on their favorite baby names and displays updated statistics of the most popular baby names. It should demonstrate your knowledge of (primarily) PHP, and MySQL and build upon your existing knowledge of HTML, CSS, Bootstrap, JavaScript, and jQuery.
-----------------------------------------------TABLETEST.PHP-------------------------------------------
<?php
require_once './php/db_connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>DB Table Test</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Database Table Test</h1>
</div>
<?php
// Create table with two columns: id and value
$createStmt = 'CREATE TABLE `TEST` (' . PHP_EOL
. ' `id` int(11) NOT NULL AUTO_INCREMENT,' . PHP_EOL
. ' `value` varchar(50) DEFAULT NULL,' . PHP_EOL
. ' PRIMARY KEY (`id`)' . PHP_EOL
. ') ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
?>
<div id="step-one" class="well">
<h3>Step One <small>Creating the table</small></h3>
<pre><?php echo $createStmt; ?></pre>
<?php
if($db->query($createStmt)) {
echo ' <div class="alert alert-success">Table creation successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table creation failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit(); // Prevents the rest of the file from running
}
?>
</div>
<?php
// Add two rows to the table
$insertStmt = 'INSERT INTO `TEST` (`id`, `value`)' . PHP_EOL
. ' VALUES (NULL, 'Test 1'),' . PHP_EOL
. ' (NULL, 'Lorem Ipsum');';
?>
<div id="step-two" class="well">
<h3>Step Two <small>Inserting into the table</small></h3>
<pre><?php echo $insertStmt; ?></pre>
<?php
if($db->query($insertStmt)) {
echo ' <div class="alert alert-success">Values inserted successfully.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Value insertion failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit();
}
?>
</div>
<?php
// Get the rows from the table
$selectStmt = 'SELECT * FROM `TEST`;';
?>
<div id="step-three" class="well">
<h3>Step Three <small>Retrieving the rows</small></h3>
<pre><?php echo $selectStmt; ?></pre>
<?php
$result = $db->query($selectStmt);
if($result->num_rows > 0) {
echo ' <div class="alert alert-success">' . PHP_EOL;
while($row = $result->fetch_assoc()) {
echo ' <p>id: ' . $row["id"] . ' - value: ' . $row["value"] . '</p>' . PHP_EOL;
}
echo ' </div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-success">No Results</div>' . PHP_EOL;
}
?>
</div>
<?php
// Drop the TEST table now that we're done with it
$dropStmt = 'DROP TABLE `TEST`;';
?>
<div id="step-four" class="well">
<h3>Step Four <small>Dropping the table</small></h3>
<pre><?php echo $dropStmt; ?></pre>
<?php
if($db->query($dropStmt)) {
echo ' <div class="alert alert-success">Table drop successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table drop failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit();
}
?>
</div>
</div>
</body>
</html>
---------------------------------------db_connect.php------------------------------------------
<?php
// Do not change the following two lines.
$teamURL = dirname($_SERVER['PHP_SELF']) . DIRECTORY_SEPARATOR;
$server_root = dirname($_SERVER['PHP_SELF']);
// You will need to require this file on EVERY php file that uses the database.
// Be sure to use $db->close(); at the end of each php file that includes this!
$dbhost = 'localhost'; // Most likely will not need to be changed
$dbname = 'FAU Username'; // Needs to be changed to your designated table database name
$dbuser = 'FAU Username'; // Needs to be changed to reflect your LAMP server credentials
$dbpass = 'DB Password'; // Needs to be changed to reflect your LAMP server credentials
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($db->connect_errno > 0) {
die('Unable to connect to database [' . $db->connect_error . ']');
}
Explanation / Answer
<?php
require_once './php/db_connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Baby Names</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="page-header">
<h1>Baby Names</h1>
</div>
<?php
// Create table with four columns: id, name, gender and no of occurences of the name
$createStmt = 'CREATE TABLE IF NOT EXISTS `babynames` (' . PHP_EOL
//. ' `id` int(11) NOT NULL AUTO_INCREMENT,' . PHP_EOL
. ' `name` varchar(15) DEFAULT NULL,' . PHP_EOL
. ' `gender` char(1) DEFAULT NULL,' . PHP_EOL
. ' `occurences` int(10)' . PHP_EOL
//. ' PRIMARY KEY (`id`)' . PHP_EOL
. ') ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
if($db->query($createStmt)) {
echo ' <div class="alert alert-success">Table creation successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table creation failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit(); // Prevents the rest of the file from running
}
// Read data from File and insert into database
$file = fopen("./data/yob2016.txt", "r");
if($file){
while (!feof($file)) {
$data = explode(",", fgets($file));
$insert_stmt = 'INSERT INTO `babynames` (`name`, `gender`, `occurences`)'.PHP_EOL
.'VALUES (''.$data[0].'', ''.$data[1].'', '.(int)$data[2].');';
if(!$db->query($insert_stmt)){
die('Error while inserting data to database');
}
}
}
fclose($file);
// Read the data names from Table and display on webpage
$select_statement = 'SELECT * FROM `babynames;';
$result = $db->query($select_statement);
if($result->num_rows>0){
?>
<table class="table table-responsive">
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Occurences</th>
</thead>
<tbody>
<?php
while($row=$result->fetch_assoc()){
?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['gender']; ?></td>
<td><?php echo $row['occurences']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php
// Drop the TEST table now that we're done with it
$dropStmt = 'DROP TABLE `babynames`;';
?>
<?php
if($db->query($dropStmt)) {
echo ' <div class="alert alert-success">Table drop successful.</div>' . PHP_EOL;
} else {
echo ' <div class="alert alert-danger">Table drop failed: (' . $db->errno . ') ' . $db->error . '</div>' . PHP_EOL;
exit();
}
?>
</div>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.