Menu
I have been working on this for about 3 days now. I have watched way too many videos and read too many resources. I am incredibly confused on HOW to just break this single php file (199 lines of code) into a model, view, controller structure! I appreciate any help that anyone is willing to offer! Willing to share file, but very new to this forum and not sure the process of how to accomplish this…
<?php
require('model/database.php');
require('addresses_db.php');
//Connect to Database
$hostname = "localhost";
$username = "ecpi_user";
$password = "Password1";
$dbname = "cis224_midterm";
$conn = mysqli_connect($hostname, $username, $password, $dbname);
//Establish variables to support add/edit/delete
$AddressNo = -1;
$First = "";
$Last = "";
$Street = "";
$City = "";
$State = "";
$Zip = "";
//Variables to determine the type of operation
$add = false;
$edit = false;
$update = false;
$delete = false;
//
if (isset($_POST['AddressNo'])) {
$AddressNo = $_POST['AddressNo'];
$add = isset($_POST['add']);
$update = isset($_POST['update']);
$edit = isset($_POST['edit']);
$delete = isset($_POST['delete']);
}
if ($add) {
//Need to add new address info
$First = $_POST['First'];
$Last = $_POST['Last'];
$Street = $_POST['Street'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$addQuery = "INSERT INTO
addresses (First, Last, Street, City, State, Zip)
VALUES ('$First', '$Last', '$Street', '$City', '$State', '$Zip')";
mysqli_query($conn, $addQuery);
//Clear the fields
$AddressNo = -1;
$First = "";
$Last = "";
$Street = "";
$City = "";
$State = "";
$Zip = "";
}
else if($edit) {
//Get the address information
$selQuery = "SELECT *FROM addresses WHERE AddressNo = $AddressNo";
$result = mysqli_query($conn, $selQuery);
$addresses = mysqli_fetch_assoc($result);
//Fill in the values to allow for edit
$First = $addresses['First'];
$Last = $addresses['Last'];
$Street = $addresses['Street'];
$City = $addresses['City'];
$State = $addresses['State'];
$Zip = $addresses['Zip'];
}
else if($update) {
//Updated values submitted
$First = $_POST['First'];
$Last = $_POST['Last'];
$Street = $_POST['Street'];
$City = $_POST['City'];
$State = $_POST['State'];
$Zip = $_POST['Zip'];
$updQuery = "UPDATE addresses SET
First = '$First', Last = '$Last',
Street = '$Street', City = '$City', State = '$State', Zip = '$Zip'
WHERE AddressNo = $AddressNo";
mysqli_query($conn, $updQuery);
//Clear the fields
$AddressNo = -1;
$First = "";
$Last = "";
$Street = "";
$City = "";
$State = "";
$Zip = "";
}
else if($delete) {
//Need to delete the selected user
$delQuery = "DELETE FROM addresses WHERE AddressNo = $AddressNo";
mysqli_query($conn, $delQuery);
$AddressNo = -1;
}
//Query for all users
$query = "SELECT * FROM addresses";
$result = mysqli_query($conn, $query);
?>
<style>
table {
border-spacing: 5px;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
text-align: center;
}
th {
background-color:lightskyblue;
}
tr:nth-child(even) {
background-color:whitesmoke;
}
tr:nth-child(odd) {
background-color:lightgray;
}
</style>
<html>
<head>
<title>Week3 Midterm - Heather Coleman</title>
</head>
<body>
<h2>Current User Addresses:</h2>
<table>
<tr style="font-size:large;">
<th>Address No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip</th>
<th></th>
<th></th>
</tr>
<?php while($row = mysqli_fetch_array($result)):;?>
<tr>
<td><?php echo $row["AddressNo"];?></td>
<td><?php echo $row["First"];?></td>
<td><?php echo $row["Last"];?></td>
<td><?php echo $row["Street"];?></td>
<td><?php echo $row["City"];?></td>
<td><?php echo $row["State"];?></td>
<td><?php echo $row["Zip"];?></td>
<td>
<form method='POST'>
<input type="submit" value="Edit" name="edit">
<input type="hidden"
value="<?php echo $row["AddressNo"]; ?>"
name="AddressNo">
</form>
</td>
<td>
<form method='POST'>
<input type="submit" value="Delete" name="delete">
<input type="hidden"
value="<?php echo $row["AddressNo"]; ?>"
name="AddressNo">
</form>
</td>
</tr>
<?php endwhile;?>
</table>
<form method='POST'>
<input type="hidden" value="<?php echo $AddressNo; ?>" name="AddressNo">
<h3>Enter your first name: <input type="text" name="First"
value="<?php echo $First; ?>"></h3>
<h3>Enter your last name: <input type="text" name="Last"
value="<?php echo $Last; ?>"></h3>
<h3>Enter your street address: <input type="text" name="Street"
value="<?php echo $Street; ?>"></h3>
<h3>Enter your city: <input type="text" name="City"
value="<?php echo $City; ?>"></h3>
<h3>Enter your state: <input type="text" name="State"
value="<?php echo $State; ?>"></h3>
<h3>Enter your zip code: <input type="text" name="Zip"
value="<?php echo $Zip; ?>"></h3>
<?php if (!$edit): ?>
<input type="submit" value="Add User" name="add">
<?php else: ?>
<input type="submit" value="Update User" name="update">
<?php endif; ?>
</form>
</body>
</html>