Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design an app / site that allow users to vote on their favorite baby names and d

ID: 3777186 • 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, AJAX, and MySQL and build upon your existing knowledge of HTML, CSS, Bootstrap,JavaScript, and jQuery.

Milestone 1: create and populate a database table for baby names

o Create a table (‘BABYNAMES’) for storing the most popular baby names

o Populate the table using the raw content from yob2015.txt.

o Display the table’s contents on a web page.

Milestone 2: implement basic functionality
10. Write a PHP script that allows users to vote on their favorite boy/girl baby name. Basically it should:

o Display a form containing: a way to select boy or girl, a text input for typing in the name, and a Submit button.

o Once the Submit button is pressed, record the user’s input into a table (separate from‘BABYNAMES’), which you use to keep track of the votes received so far.

o Display the most popular names so far (in decreasing order by number of votes). § See https://www.ssa.gov/oact/babynames/ for an example of such table

(limited to the top 10 baby names).

Here's what I have so far:

<?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();
}
?>

Explanation / Answer

Use the below php code to achive the milestone:

tabletest.php
require_once './php/db_connect.php';
?>

DB Table Test

Database Table Test
// To 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 ;';
?>
Step One Creating the table
if($db->query($createStmt)) {
echo '
Table creation successful.
' . PHP_EOL;
} else {
echo '
Table creation failed: (' . $db->errno . ') ' . $db->error . '
' . PHP_EOL;
exit(); // Prevents the rest of the file from running
}
?>
// To Add two rows to the table
$insertStmt = 'INSERT INTO `TEST` (`id`, `value`)' . PHP_EOL
. ' VALUES (NULL, 'Test 1'),' . PHP_EOL
. ' (NULL, 'Lorem Ipsum');';
?>
Step Two Inserting into the table
if($db->query($insertStmt)) {
echo '
Values inserted successfully.
' . PHP_EOL;
} else {
echo '
Value insertion failed: (' . $db->errno . ') ' . $db->error . '
' . PHP_EOL;
exit();
}
?>
// To Get the rows from the table
$selectStmt = 'SELECT * FROM `TEST`;';
?>
Step Three Retrieving the rows
$result = $db->query($selectStmt);
if($result->num_rows > 0) {
echo '' . PHP_EOL;
while($row = $result->fetch_assoc()) {
echo '
id: ' . $row["id"] . ' - value: ' . $row["value"] . '
' . PHP_EOL;
}
echo '' . PHP_EOL;
} else {
echo '
No Results
' . PHP_EOL;
}
?>
// We will Drop the TEST table now since we are done with it
$dropStmt = 'DROP TABLE `TEST`;';
?>
Step Four Dropping the table
if($db->query($dropStmt)) {
echo '
Table drop successful.
' . PHP_EOL;
} else {
echo '
Table drop failed: (' . $db->errno . ') ' . $db->error . '
' . PHP_EOL;
exit();
}
?>

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote