Project 1: Story Database
You will rework your dynamic story generator into a database-driven application by refactoring the program to be data-driven, creating and populating the database, and then modifying the program to use the database. By following this sequence, you will be able to verify your work at several critical points.
Step 1: Refactor your program to be data-driven
You probably "hard-coded" the story text and the names of input fields. If so, your code may look something like this:
echo "
Yesterday ";
echo $_POST['Time'];
echo ", my friend ";
echo $_POST['Person'];
echo " was walking past the grocery store ";
This code alternates between blocks of fixed text and input data. You will rewrite the program to read both the fixed text and the input values from a data structure. Making this change will simplify the transition to a database-driven application.
Declare 2 PHP arrays. One will hold the blocks of fixed text. The other will hold the prompts for the input fields. You may name these "$content" and "$prompt", respectively. At the beginning of your program, initialize the values for these arrays. You can simplify the program logic by using the first prompt for a story title, such as the following:
$prompt = array('Story title', 'Time', 'Person');
Then, on each line of the display, output one element of the $prompt array followed by a text input box. If you name that input box with the current subscript from the $prompt array, it will be easy to store the user's input into the corresponding element of the $content array.
You may want to experiment with reading these values from a text file.
Next, create the input fields. Create one input field for each element in the $prompt array; display the element values user prompts. For simplicity, use text boxes for all input fields. You will need a PHP loop to do this; if you use for loop, use the count function to tell how many elements the array holds. It will be convenient to use the array index value as the name for each input field.
Ensure that the text boxes display properly, then rewrite the story generator as a loop. It should display the text for the first input field, the first content element, the text for the second input field, the second content element, and so on. Check that the story displays correctly. Are you inserting spaces between the content and the input text?
Try modifying a few details of the story. You should only have to change the data arrays, not the program logic. Take a screenshot of the modified story display and include it in your assignment with a copy of the PHP source code.
Save a copy of your work with a different file name so that you refer to it later.
Step 2: Create and populate the database
Now that your story generator is data-driven, you will adapt it to use a database. The logic will not change, but you will fetch data from a table, rather than from the 2 arrays.
In phpMyAdmin, create a user and a database called storygen. In this database, create a table named Story. This table will replace the 2 arrays, so it needs fields for content and prompt. Both fields hold text values but they have very different characteristics. Consider this when you define their attributes.
Something is missing. How will your story generator know in what sequence to display the parts? Define an appropriate field for this purpose. Now review the fields and define a primary key for the table. Paste a copy of the CREATE TABLE command into a Word document that you will submit with this assignment. Write a short description of each field, including its purpose and why you defined it the way you did.
Add rows with the data from your story, then create and run a query that displays the data in sequence. You will use this query in the next step.
Step 3: Modify the program to use the database
Return to your story generator program. Remove the statements that declare and initialize the $content and $prompt arrays. In their place, add PHP code to connect to MySQL, and select the database, and and query the Story table.
Modify the loop that creates the text boxes so that it fetches rows from the query. Use the sequence and prompt fields. Make sure that this part of the program is working correctly.
Now modify the story generator loop to fetch rows from the query, but this time you will use the sequence and content fields. The HTTP request passes the user's entries, just as it always has done. Ensure that the whole program works as expected, and take screenshots of the data entry page and the story display. Include the final code of your PHP program.