Ask PHP Expert

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.

PHP , Programming

  • Category:- PHP
  • Reference No.:- M91190850

Have any Question?


Related Questions in PHP

Question continue to build on the skills of providing web

Question: Continue to build on the skills of providing Web page content and structure with HTML and Web page style and layout using CSS. Demonstrate the skill of creating a dynamic Web page that includes JavaScript clien ...

Question develop a 5-6 page word document that describes

Question: Develop a 5-6 page Word document that describes the design for a small Web site that meets the following specifications. 1. The Web site includes four or more Web pages designed according to current usability g ...

Question using the course software and week one template as

Question: Using the course software and Week One Template as a starting point, create a new HTML webpage and include the following: • Write the code to display your name in the largest-size heading element. • Write the m ...

In php write a simple addtion calculator that reads in two

In PHP write a simple addtion calculator that reads in two values and then add them together and display the output. Be sure to validate both inputs and ensure that it can add 0+0 = Sum: 0. The user should be able to inp ...

Final project assignment -requirements specifications -1

FINAL PROJECT ASSIGNMENT - REQUIREMENTS / SPECIFICATIONS - 1. For the final project, you will have to make good use of your CMS using PHP and MySQL for the content delivery. You will have to be able to deliver the conten ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As