Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating Databases for Web Applications State capitals quiz: demonstrates parallel structures AND multi-purpose php files Classwork: design a new quiz.

Similar presentations


Presentation on theme: "Creating Databases for Web Applications State capitals quiz: demonstrates parallel structures AND multi-purpose php files Classwork: design a new quiz."— Presentation transcript:

1 Creating Databases for Web Applications State capitals quiz: demonstrates parallel structures AND multi-purpose php files 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.

2 multi-purpose php files Used to both –present a form and –check the input in a form. Replacement for html and php pair. Advantages: –don’t have to coordinate two files –less files to handle Disadvantages –need to include coding to make determination

3 Use of @ sign The @ sign is used to suppress error messages. We will use it in the code to test if something, namely a form input, exists. –if it does, then control goes to the code for handling the form –otherwise, control goes to the code for displaying the form Also use it to suppress an error when one input is missing

4 rand Built-in php function. Takes two arguments. rand(a, b), a and b integers, returns random value from a, … b. Check out shuffle, mt_rand. Extra credit posting opportunity to explain.

5 State capital quiz http://socialsoftware.purchase.edu/jeanine. meyer/statecapquizask.phphttp://socialsoftware.purchase.edu/jeanine. meyer/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

6 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

7 State capitals storyboard statecapquiz.php statecapquizcheck.php Include file: statecapitals.php

8 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 very simple cases. Repeat: multi-purpose, self-referencing of php scripts is a common practice –benefits: reduces number of files. Information in one place –negatives: the one file is more complex

9 statecapitals.php <?php $states = Array(); $capitals = Array(); $states[]="Alabama"; //adds to array $capitals[]="Montgomery"; $states[]="Alaska"; $capitals[]="Juneau"; …

10 statecapquizask.php State capitals quiz: check State Capital Quiz <?php $saywhich=@$_GET['saywhich']; if ($saywhich){ include("statecapitals.php"); $which=$_GET['which']; $choice=rand(0, sizeOf($states)-1);

11 if ($which=='state') { $state = $states[$choice]; print("What is the capital of $state? "); print(" \n"); print(" "); print(" \n"); }

12 else { $capital = $capitals[$choice]; print("$capital is the capital of which state? "); print(" \n"); print(" "); print(" \n"); }

13 else { print("Choose form of question: do you want to be given the state or the capital? "); print(" \n"); print("Ask State"); print(" Capital\n"); print(" \n"); print(" "); print(" "); } ?>

14 statecapquizcheck.php State capitals quiz: check <?php include('statecapitals.php'); $choice=$_GET['choice']; $state=@$_GET['state']; $capital=@$_GET['capital']; $which=$_GET['which']; $correctstate=$states[$choice]; $correctcapital=$capitals[$choice];

15 if ($which=='state') { if ($capital == $correctcapital) { print("Correct! $correctcapital is the capital of $correctstate!"); print(" Play again "); }

16 else { print("WRONG! \n"); print(" New turn \n"); print("OR try again: What is the capital of $correctstate? "); print(" \n"); print(" "); print(" \n"); } }

17 else { if ($state == $correctstate) { print("Correct! The capital of $correctstate is $correctcapital!"); $saywhich='false'; print(" Play again "); }

18 else { print("WRONG! \n"); print(" New turn \n"); print("OR try again: $correctcapital is the capital of what state? "); print(" \n"); print(" "); print(" \n"); } } ?>

19 else { $capital = $capitals[$choice]; print("$capital is the capital of which state? "); print(" \n"); print(" "); print(" \n"); } }

20 else { print("Choose form of question: do you want to be given the state or the capital? "); print(" \n"); print("Ask State"); print(" Capital\n"); print(" \n"); print(" "); print(" "); } ?>

21 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 –prevent browser from showing previous inputs –see next slide…

22 Note Answer must be exact! Sometimes this is not appropriate. Consider NOT requiring exact case. –Extra credit opportunity Consider something else, such as inexact spelling. What is close enough? How to measure? Use of regular expression is a way to present a pattern of answers as opposed to a set answer. Later: we will cover the use of LIKE as an operator in MySql statements

23 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!

24 Homework Get your quiz working. Post link in posting to the General Discussion Forum. Research and post unique source on Regular expressions on moodle. We use regular expressions –to validate input and extract information from input. –potential for quizzes.


Download ppt "Creating Databases for Web Applications State capitals quiz: demonstrates parallel structures AND multi-purpose php files Classwork: design a new quiz."

Similar presentations


Ads by Google