Database-driven application by refactoring the program

Assignment Help PHP Web Programming
Reference no: EM13660797

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 "<p>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.

Reference no: EM13660797

Questions Cloud

Feminism for young women of the time : Feminism for young women of the time?
Find the weighted average cost of capital : Based on the information below, calculate the weighted average cost of capital -Two thousand shares of preferred are outstanding, each of which pays an annual dividend of $7.50. They originally sold to yield 15% of their $50 face value. They''re no..
Discuss what that occupation does : Discuss what that occupation does, the work environment
Calculate the expected time and variance of each activity : Calculate the expected time and variance of each activity and draw a network diagram and identify critical path and project completion time?
Database-driven application by refactoring the program : 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 users
The alternative that the home team wins a majority of games : In a random sample of 99 NBA games, the home team won 57 games. Test the null hypothesis that the home team wins one-half of all games against the alternative that the home team wins a majority of games.
What is the coefficient of price elasticity of demand : If the price of a hardback novel increases from $25 to $30 and quantity demanded falls from 60,000 to 30,000 in response, what is the coefficient of price elasticity of demand using the simplest definition of price elasticity? a. 0.25 b. 1 c...
Show the consultancy report for anthonys orchard : Explain how purchase of the apple press might affect the company's revenue goals. Based on this information, explain whether Anthony's Orchard should invest in the apple press.
Educational supplies and equipment : Educational supplies and equipment.

Reviews

Write a Review

PHP Web Programming Questions & Answers

  Q1-joe and george are good friendsjoe is a builder and

q1-joe and george are good friends.joe is a builder and george is in underwear. george wants a house built so after a

  The law of contract is effective and able to protect the

the law of contract is effective and able to protect the weaker party from the stronger one when a contract is being

  Find an article no older than 2006 on a company that faced

find an article no older than 2006 on a company that faced an ethical dilemma. develop a 800-1000 word personal essay

  Implement a web application

Implement a web application called CS320Starter, which is similar to the crowd funding platform Kickstarter where people raise funds for their projects.

  Define a constructor for class game that takes the initial x

Define a constructor for class Game that takes the initial x and y positions of the player as parameters and creates and initialises the player, dot1, dot2 and dot3 fields.

  Qestion one describe the various ways in which a

question one describe the various ways in which a contractual relationship can come to an end including

  Xhtml form that collects a name

Create an XHTML form that collects a name, address, phone number, and zip code. The phone number must be input in the format of ddd-ddd-dddd and the zip code must be in the format of ddddd-dddd.

  Shopping cart program for web applications class

Shopping Cart program for web applications class. Allows user to browse while keeping track of the items in which they will purchase at the end on the order page link and this will give a final price for all items.

  Php mysql insertion form that will insert name in customer

php mysql insertion form that will insert name in customer table and have product field that will be a dropdown menu getting product names from the product table database. When inserted it will save student name and product name in relational table. ..

  Web programming – server side

Develop a web site that provides customer access to the Northwind database and Provide a way to review existing orders. That is, allow the user to log in to a new session and find orders for her that are already in the database.

  Robert keating is an accountant for a large firm that

robert keating is an accountant for a large firm that manufactures kitchen utensils. in the course of his normal

  Design a dynamic database using mangodb html and php

design a dynamic database using mangodb html and php. database should be designed to have the subsequent static

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd