22
Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database. Get a simple quiz working like the state capital quiz. Post unique source on REGULAR expressions.

Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Embed Size (px)

Citation preview

Page 1: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Creating Databases for Web Applications

State capitals quizClasswork: design a new quiz

Homework: Make sure you can upload files and have MySQL database. Get a simple quiz working like the

state capital quiz.Post unique source on REGULAR expressions.

Page 2: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

News

• Added a new General Discussion forum.

• Use this for questions, comments, news.– the News forum is intended to be just from the

teacher!

• Use the General Discussion forum for 1 of the 2 homework assignments for today!

Page 3: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

State capital quiz

• http://newmedia.purchase.edu/~Jeanine/db/statecapquizask.php

• Choose what to be asked– Note: how can this be worded better????

• Ask• Check the answer

– Give chance to try again or– Go back to get new question

Page 4: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

State capitals quiz

• Implementation: three php files. Two of the php files call themselves and include the other file.– statecapitals.php

• This file is include[d] in the others. It sets up 2 parallel arrays holding the names of the states and the capital cities.

– statecapquizask.php• This is a multi-purpose file.

– statecapquizcheck.php• This is also a multi-purpose file. Checks answer. Allows for

another guess or goes back to statecapquizask.php

Page 5: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

State capitals storyboard

statecapquiz.php

statecapquizcheck.php

Include file: statecapitals.php

Page 6: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Programming practices

• Parallel structures, in this case the array of states corresponding to the array of capitals, is a common programming device. Think of it as an alternative to a database or xml for simple cases.

• Self-referencing of php scripts is a common practice (mentioned last class)– benefits: reduces number of files. Information in one

place– negatives: the one file is more complex

Page 7: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Materials

• Go to [our] moodle site: http://moodle.purchase.edu– sign in and go to the class.– go to Code Examples– click on StateCapitalQuiz

• NOTE: using View source on the browser does not show the whole php file!

Page 8: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

statecapitals.php

<?php$states = Array();$capitals = Array();$states[]="Alabama";$capitals[]="Montgomery";$states[]="Alaska";$capitals[]="Juneau";…

Page 9: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

statecapquizask.php<html> <head> <title>State capitals quiz: check</title></head> <body>

<h1>State Capital Quiz </h1><p><?php$saywhich=@$_GET['saywhich'];if ($saywhich){ include("statecapitals.php"); $which=$_GET['which']; $choice=rand(0, sizeOf($states)-1);

Page 10: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

if ($which=='state') { $state = $states[$choice]; print("What is the capital of $state?<br>"); print("<form action='statecapquizcheck.php'

method='get'>\n"); print("<input type='text' name='capital'><br>\n"); print("<input type='hidden' name='which'

value=$which>\n"); print("<input type='hidden' name='choice'

value=$choice>\n"); print("<input type='submit' value='Submit

Answer'>"); print("</form>\n"); }

Page 11: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else { $capital = $capitals[$choice]; print("$capital is the capital of which state?<br>");

print("<form action='statecapquizcheck.php' method='get'>\n");

print("<input type='text' name='state'><br>\n"); print("<input type='hidden' name='which' value=$which>\n");

print("<input type='hidden' name='choice' value=$choice>\n");

print("<input type='submit' value='Submit Answer'>");

print("</form>\n"); } }

Page 12: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else { print("Choose form of question: do you want to be given the

state or the capital?<br>"); print("<form action='statecapquizask.php' method='get'>\

n"); print("Ask <input type='radio' name='which'

value='state'>State"); print(" <input type='radio' name='which'

value='capital'>Capital\n"); print("<input type='hidden' name='saywhich' value='true'>\

n"); print("<input type='submit' value='Submit choice'>"); print("</form>"); } ?> </body> </html>

Page 13: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

statecapquizcheck.php<html> <head> <title>State capitals quiz:

check</title> </head> <body><?phpinclude('statecapitals.php');$choice=$_GET['choice'];$state=$_GET['state'];$capital=$_GET['capital'];$which=$_GET['which'];$correctstate=$states[$choice];$correctcapital=$capitals[$choice];

Page 14: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

if ($which=='state') {

if ($capital == $correctcapital) {

print("Correct! $correctcapital is the capital of $correctstate!");

print("<p><a href='statecapquizask.php'>Play again </a>");

}

Page 15: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else {

print("WRONG!<p>\n"); print("<a href='statecapquizask.php'>New turn </a><p>\

n"); print("OR try again: What is the capital of $correctstate?

<br>"); print("<form action='statecapquizcheck.php' method='get'>\

n"); print("<input type='text' name='capital'><br>\n"); print("<input type='hidden' name='state' value=$state>\n"); print("<input type='hidden' name='which' value=$which>\

n"); print("<input type='hidden' name='choice' value=$choice>\

n"); print("<input type='submit' value='Submit Answer'>"); print("</form>\n"); } }

Page 16: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else {

if ($state == $correctstate) {

print("Correct! The capital of $correctstate is $correctcapital!");

$saywhich='false';

print("<p><a href='statecapquizask.php'>Play again </a>");

}

Page 17: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else {print("WRONG!<p>\n"); print("<a href='statecapquizask.php'>New turn </a><p>\

n"); print("OR try again: $correctcapital is the capital of what

state?<br>"); print("<form action='statecapquizcheck.php' method='get'>\

n");print("<input type='text' name='state'><br>\n"); print("<input type='hidden' name='capital' value=$capital>\

n"); print("<input type='hidden' name='which' value=$which>\

n"); print("<input type='hidden' name='choice' value=$choice>\

n"); print("<input type='submit' value='Submit Answer'>"); print("</form>\n"); } }?>

Page 18: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else { $capital = $capitals[$choice]; print("$capital is the capital of which state?<br>"); print("<form action='statecapquizcheck.php'

method='get'>\n"); print("<input type='text' name='state'><br>\n"); print("<input type='hidden' name='which'

value=$which>\n"); print("<input type='hidden' name='choice'

value=$choice>\n"); print("<input type='submit' value='Submit

Answer'>"); print("</form>\n"); } }

Page 19: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

else {print("Choose form of question: do you want to be given

the state or the capital?<br>");print("<form action='statecapquizask.php' method='get'>\

n");print("Ask <input type='radio' name='which'

value='state'>State");print(" <input type='radio' name='which'

value='capital'>Capital\n");print("<input type='hidden' name='saywhich' value='true'>\

n");print("<input type='submit' value='Submit choice'>");print("</form>"); }?></body></html>

Page 20: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Reprise

• if statements within if statements• php produces the appropriate html,

including the form element• The php is printing (producing) html. It is

not operating the quiz!• There are ways to enhance this program

– perhaps limit the number of re-tries

• THIS WILL BE A HOMEWORK ASSIGNMENT!

Page 21: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Classwork (teams)

Design simple quiz • like the state capital quiz, using parallel arrays

for questions and answers – Ideas?

OR• make significant enhancement to my quiz, like

adding scoring, limiting re-tries

OR• some other quiz!

Page 22: Creating Databases for Web Applications State capitals quiz Classwork: design a new quiz Homework: Make sure you can upload files and have MySQL database

Homework

• Get your quiz working. Post link in posting to the General Discussion Forum.

• Research and post unique source on Regular expressions on the Links and sources forum. We use regular expressions to validate input and extract information from input.