/    Sign up×
Bounties /Pin to ProfileBookmark

Beginner trying to create MVC structure from pre-existing single PHP file???

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>  
to post a answer
PHP

3 Replies

Davinci•003 auto-replyCreating an MVC structure from a pre-existing single PHP file can be a daunting task, but it is possible. The first step is to identify the different components of the file and separate them into the appropriate Model, View, and Controller files. The Model should contain the code that interacts with the database, the View should contain the code that displays the data, and the Controller should contain the code that handles the logic and connects the Model and View.

Once you have identified the components, you can start to move them into the appropriate files. It is important to keep in mind that the Model, View, and Controller should be completely separate and not contain any code from the other components.

Finally, you will need to create the necessary routes and functions to connect the Model, View, and Controller. This will allow the application to properly display the data and handle the logic.


// Route to connect the Model, View, and Controller
Route::get('/', 'Controller@index');

// Function to connect the Model, View, and Controller
public function index()
{
// Get data from the Model
$data = Model::getData();

// Pass the data to the View
return view('index', ['data' => $data]);
}


I hope this helps!

Was this helpful?

Copy linkTweet thisAlerts:
@lexytaylormbauthorMay 08.2023 — This does help slightly, here is my file:

I am attempting to use jsfiddle for the first time here...
https://jsfiddle.net/lexytaylormb/w1ybnc3s/1/#&togetherjs=rgE6cuK94e
Copy linkTweet thisAlerts:
@Aliya7788May 27.2023 — Are you a beginner looking to enhance your PHP development skills by implementing the Model-View-Controller (MVC) architecture? This article provides a concise guide to help you transform a single PHP file into a structured MVC application. By understanding the separation of concerns and organizing your code into models, views, and controllers, you can improve code maintainability and scalability. Take the first step towards building efficient and structured PHP applications with the MVC architecture. https://ffstylishname.com
@Aliya7788May 27.2023(updated)
×

Success!

Help @lexytaylormb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...