I've been learning PHP and I've just gotten into using OOP. Classes/objects/methods etc. My end goal is to take an old Mad libs program I did with PHP, and convert it to OOP. The gist of the project was allowing the user to enter a verb, adverb, adjective, and noun and storing it in the DB, then displaying it on the page as a Mad lib (along with the rest of the DB).
I was hoping someone could review my source and let me know how to properly use the setters and getters, right now I think my code is just using POST to pass the user entered data to the method?? But if I take it out, it doesn't work anymore.
Basically I'm pretty lost right now and would appreciate any and all advice on how I can improve/fix this code. Right now it does work (including insertion and query of DB), but i'm not sure if I did It properly.
General guidelines Im following to covert to OOP (Inside Madlibs Class):
Create instance vars for holding a noun, verb, adjective, adverb, and story
Create getters and setters for all instance vars
Create method for inserting the new instance vars into DB
Create method for querying the stories that returns a results set newest to oldest
Create a method that takes the results set (from the query) as an argument, and returns the results onto page
Madlibs Class Code:
<?php
class MadLibs {
private $noun; // String
private $verb; // String
private $adjective; // String
private $adverb; // String
private $story; // String - entire madlib story
// Getters
public function getNoun() {
return $this->noun;
}
public function getVerb() {
return $this->verb;
}
public function getAdjective() {
return $this->adjective;
}
public function getAdverb() {
return $this->adverb;
}
public function getStory() {
return $this->story;
}
// Setters
public function setNoun($noun) {
$this->noun = $noun;
}
public function setVerb($verb) {
$this->verb = $verb;
}
public function setAdjective($adjective) {
$this->adjective = $adjective;
}
public function setAdverb($adverb) {
$this->adverb = $adverb;
}
public function setStory($story) {
$this->story = $story;
}
// method for inserting the new properties into mad libs database table
public function insertMadLibs($noun, $verb, $adjective, $adverb, $story) {
$story = "Have you seen the $adjective $noun? They got up and started to $verb $adverb!";
$dbc = mysqli_connect('localhost', 'root', '', 'project1')
or die('Error connecting to DB');
$query = "INSERT INTO Madlibs (id, noun, verb, adjective, adverb, story, dateAdded)" .
"VALUES (0, '$noun', '$verb', '$adjective', '$adverb', '$story', NOW())";
$result = mysqli_query($dbc, $query)
or die('Error querying DB');
mysqli_close($dbc);
echo '<span id="success">Success!</span>';
}
// method for querying the stories that return results set newest to oldest
public function fetchStory() {
$dbc = mysqli_connect('localhost', 'root', '', 'project1')
or die('Error connecting to DB.');
$query = "SELECT * FROM Madlibs ORDER BY dateAdded DESC";
$result = mysqli_query($dbc, $query)
or die('Error querying DB.');
mysqli_close($dbc);
return $result;
}
// method that takes the results set (from the query) as an argument, and returns the results in a formatted HTML table
public function displayStory($result) {
$dbc = mysqli_connect('localhost', 'root', '', 'project1')
or die('Error connecting to DB.');
while ($row = mysqli_fetch_array($result)) {
echo '<div id="comments"><p>Have you seen the <b>' . $row['adjective'] . ' ' . $row['noun'] . '</b>? <br />';
echo 'They got up and started to <b>' . $row['verb'] . ' ' . $row['adverb'] . '</b>! </p>';
echo '<span id="footer">Date: ' . $row['dateAdded'] . '</span></div>';
}
mysqli_close($dbc);
}
}
?>
index file where calls are made:
<!DOCTYPE html>
<html>
<head>
<title>Mad libs</title>
<link rel="stylesheet" href="styles.css" type="text/css" />
</head>
<body>
<?php
require_once('MadLibs.php');
$mad_libs = new MadLibs();
$mad_libs->setNoun($_POST['noun']);
$mad_libs->setVerb($_POST['verb']);
$mad_libs->setAdjective($_POST['adjective']);
$mad_libs->setAdverb($_POST['adverb']);
// How do I remove these and still have the program function??
// Get entered info form form
$noun = $_POST['noun'];
$verb = $_POST['verb'];
$adjective = $_POST['adjective'];
$adverb = $_POST['adverb'];
if (isset($_POST['submit'])) {
if ( (!empty($noun)) && (!empty($verb)) && (!empty($adjective)) && (!empty($adverb)) ) {
$mad_libs->insertMadLibs($noun, $verb, $adjective, $adverb, $story);
} else {
echo '<span id="error">Please fill out all fields!</span>';
}
}
?>
<div id="wrapper">
<h1>Mad Libs</h1>
<hr>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="noun">Enter a <b>Noun</b>: </label>
<input type="text" name="noun" id="noun" class="input" value="<?php echo $noun; ?>"/>
<br />
<label for="verb">Enter a <b>Verb</b> (Present Tense): </label>
<input type="text" name="verb" id="verb" class="input" value="<?php echo $verb; ?>"/>
<br />
<label for="adjective">Enter an <b>Adjective</b>: </label>
<input type="text" name="adjective" id="adjective" class="input" value="<?php echo $adjective; ?>"/>
<br />
<label for="adverb">Enter an <b>Adverb</b>: </label>
<input type="text" name="adverb" id="adverb" class="input" value="<?php echo $adverb; ?>"/>
<br />
<input name="submit" id="submit" type="submit" value="Submit"/>
</form>
</div>
<?php
$result = $mad_libs->fetchStory();
$mad_libs->displayStory($result);
?>
</body>
</html>
$query = "INSERT INTO Madlibs (id, noun, verb, adjective, adverb, story, dateAdded)" . "VALUES (0, '$noun', '$verb', '$adjective', '$adverb', '$story', NOW())";
\$\endgroup\$