Question
1) If you have not already, review Exercise 4-1 in Chapter 4. Look at the solution files for the exercise. 2) Update your ToDo List
1) If you have not already, review Exercise 4-1 in Chapter 4. Look at the solution files for the exercise.
2) Update your ToDo List to include Categories like Exercise 4-1 does. Your sample data might include categories like "Yard Work", "Homework", and "Household Chores". This will require you to create a 2nd table in your database called "categories". It should have a "categoryID" field that is primary and set to auto increment (A.I. checkbox in myPhpAdmin). The second and last field should be "categoryName". You will also need to add a categoryID column to your "todoitems" table. Update your add item process to include adding the categoryID by selecting the category name from a drop menu. This is similar to Exercise 4-1 which has sample code in the textbook.
Note: Best practice here is to make the categoryID column you add to the todoitems table a foreign key that is linked to the categoryID column in the categories table. I am not requiring this, but it is a best practice. Without the foreign key, you can create "orphaned" todo items that will not have a linked category. That said, the directions tell you how to handle that in your code. If you do add a foreign key, do some extra testing to insure you avoid errors when trying to add an item before you have categories to choose from and handle the error that arises from attempting to delete a category when todo items are assigned to it. And considering you might not want to delete all items assigned to that category, you might want to add an update category feature to each item in the todo list. Again, I'm not requiring this, but a foreign key is best practice and definitely adds some extra work to this simple todo list. :)
3) At this point your ToDo List should be caught up to the solution of Exercise 4-1 where you can add categories and delete categories. If you are struggling with this, you should contact me here before attempting to refactor your code with the MVC pattern. Even if you are not struggling, this is a good place to take a break before moving forward. :)
Part Two: Refactoring Your Code to use the MVC Pattern.
4) Work on the textbook's example code first. Read through pages 159 to 179. We will not go any deeper than what is shown within these pages. However, our project is slightly different so you will not just be reproducing exactly what is in the book. You will need to think a little!
5) Make a copy of your existing project so you can refer to or revert to an unaltered copy if needed.
6) Right now, you probably have individual files named: add_item.php, delete_item.php, add_category.php, and delete_category.php. We will stop using these files and replace them with functions. Page 171 shows a product_db.php file example. Ours will be item_db.php. Create a folder named model and create item_db.php inside that folder. You will need all 4 functions shown on page 171 but for items in your todo list instead of products. Once you have completed that, look at page 169. Create a file named "category_db.php in your model folder. The text shows 2 functions for categories. You will need them both. However, you also need to create a "delete_category" function and an "add_category" function.
7) You do not need to change your "database.php" file, but you do need to move it to the "model" folder.
8) Create a "view" folder and create the "header.php" and "footer.php" files you see on page 175. However, remember to put in your ToDo List information. You can also create a "css" folder inside your "view" folder and put your css file (I named mine "main.css") inside that folder. When you do this, you'll need to make sure you link to the css file correctly.
9) Create an error.php file in the view folder that can display an error_message if it exists. The text has provided an example or two of error files starting in Chapter 4.
10) We have now broken out the "model" and the "view". Now we need to refactor our existing "index.php" into a controller. We will also refactor the "add_item_form.php" file and create two new files: "item_list.php" and "category_list.php" which all belong in the "view" folder.
11) Look at page 173. This is the example index.php file for the book project. It looks complicated at first but it is not as bad as it seems. It is just a BIG if / else if statement. Tackle one if condition at a time. You can see it is looking for $action values which you will now have your forms submit with hidden inputs.
======
12) Get one if condition to work at a time until they are all working for you. Note on the first one - our "list_items" - they assign a default $category_id = 1. Instead of assigning a default value, let the NULL or FALSE value pass through. Allow your "get_items_by_category" function to be called with a null or false value in the $category_id parameter. Inside the function, add an if statement checking for a NULL or FALSE value there instead. If the $category_id is null or false, set your $query to return all todo items. Otherwise, have it return the items from the specific category. In your table display, once again use this if statement to look for null or false values in the categoryName, and if true, display "None" in the category field.
Note: #12 has caused some confusion. This is related to the foreign key discussion above. Checking if the todo items return a null value for categoryName and displaying "None" instead is how we are handling the "orphaned" items mentioned above. This should only happen if you do not enforce a foreign key for the categoryID field. Again, a foreign key is not required for this assignment, but in database design terms, it is best practice.
======
13) Aside from adapting a little to your project, the index.php on page 173 is close to what you should have. PHP is good about giving useful errors and telling you what file and line number to look at if you have errors as you go. It happens to us all :)
14) What's missing? The book does not have an "else if" in their index.php example for deleting a category or for adding a category. See if you can figure these out on your own. They are very similar to deleting an item and adding an item.
15) Are we finished? Almost! The book adds an "aside" element and lists the categories to the side of the project. Instead, see if you can add a select menu above the list display that lets you choose which category of items to display. Don't forget to add a selection to view all categories. You will need a submit button beside the select menu if you are using php strictly. Javascript would allow you to submit the form with the
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started