02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Remember that a querystring takes the form<br />

Chapter 14: Quizzes<br />

varName1=varValue1&varName2=varValue2&varName3=varValue3&varName4=<br />

varValue4&...<br />

Each variable name and value are linked using the ampersand character. To turn the string in<strong>to</strong> variable<br />

data you can use the ‘Request’ command. The first section of the script turns the querystring<br />

in<strong>to</strong> the variables ‘category’, ‘difficulty’, ‘question’, and ‘A, B, C, D’.<br />

An ASP page is cached by the browser so it is best <strong>to</strong> include the command<br />

‘Response.Expires=0;’ at the beginning of the file <strong>to</strong> ensure that this caching does not take place,<br />

otherwise the page can only be used once.<br />

Having parsed the string in<strong>to</strong> usable variables the ‘dbconnect.asp’ file is included in the file.<br />

The effect of an include is as though the code in the file is actually part of this page. So if<br />

everything has worked at this stage in the file you will have the variables <strong>to</strong> define a record in<br />

the database and an open connection. Because the same ASP page is used for both updating<br />

and adding we must first find out whether this question appears in the database. To do this<br />

we will need another object, an ‘ADODB.recordset’, which is created in the same way as the<br />

connection. Having created a recordset we use the ‘Open’ method passing a string and the open<br />

connection. This time the string is an SQL query. To discover whether the record exists we<br />

simply ask for a list of records containing this question using the SQL query ‘SELECT * FROM<br />

quiz WHERE question=’ the current question. Here the table is ‘quiz’ and the only test is for<br />

the single parameter ‘question’. The database engine then looks through all the records in the<br />

‘quiz’ table for a match for the current question. Each time it finds a match it is added as a new<br />

record <strong>to</strong> the object ‘rs’. We can move between records in the ‘rs’ object until we reach the end<br />

when rs.EOF (End Of File) evaluates <strong>to</strong> true. If rs.EOF is false after the SQL query then the<br />

question was not found. To allow us <strong>to</strong> use this information later in the code the variable ‘found’<br />

is set <strong>to</strong> true or false based on the return value of the query: true if the question exists and false<br />

if not.<br />

Part of the information s<strong>to</strong>red in the database is the date the record was added. To do this we<br />

need <strong>to</strong> get the current date. Creating a variable using ‘new Date( )’ initializes the variable <strong>to</strong> a<br />

‘Date’ object containing the current date. To use this with an SQL query we must convert the<br />

format in<strong>to</strong> a string. The string should be of the form<br />

Month/Date/Year<br />

where ‘Month’ is a number between 1 and 12, ‘Date’ is a number between 1 and 31 (depending<br />

on the month) and the year is in the four-number format. The ‘Date’ object has methods for<br />

returning this information, although the month is returned as a number between 0 and 11, so 1<br />

must be added <strong>to</strong> achieve the SQL formatting.<br />

We are nearly ready <strong>to</strong> update or add <strong>to</strong> the database. First we must format a query string<br />

using the information we have discovered. If we are updating then we use the SQL command<br />

‘UPDATE’. The format is <strong>to</strong> follow ‘UPDATE’ with the table name, and then use ‘SET’ and a<br />

list of values <strong>to</strong> set separated by commas. So that SQL knows which record <strong>to</strong> update, the query<br />

is completed by adding ‘WHERE’ with the column name ‘question’ being equal <strong>to</strong> the current<br />

213

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!