Trial-and-Error Approach


We will use the rand command to determine which question to send via XML to the client. We find the minimum and maximum QuID s and use those two values as the boundaries for our rand command. But there may be invalid values in this range because some QuID s were skipped in data entry or by later deletion of questions. So we could write code that follows the logic of Figure 13.2 and looks like this:

Figure 13.2. Guess and Check to Find Valid QuID s

graphics/13fig02.gif

PHP
 <? class Question {  var $idkey;  var $Q;  var $A= array(); 

The database is queried during this object constructor:

PHP
 function Question( ) {   MySQL_connect('sql.useractive.com', 'bigfun', '[REDACTED]');   MySQL_select_db('bigfun');   $min = MySQL_query ("select Min(QuID) from Questions" );   $max = MySQL_query ("select Max(QuID) from Questions" );   do {     $WhichQuID = rand(MySQL_result($min,0,0),MySQL_result($max,0,0));     $res = MySQL_query("select * from Questions where QuID = $WhichQuID");      }      while(!MySQL_num_rows($res)) ;   MySQL_free_result($max);   MySQL_free_result($min);    $sqlquestion = MySQL_fetch_array ($res );    $sqlQuID     = $sqlquestion["QuID"];    $this->Q     = $sqlquestion["Q"];    $this->idkey = $sqlquestion["QuID"];    MySQL_free_result($res);    $res = MySQL_query ("select * from Answers where WhichQuestion =           $sqlQuID");    $numrows = MySQL_num_rows($res);    for($i=0;$i<$numrows;$i++) {       $temp = MySQL_fetch_array($res);       array_push ($this->A , $temp["AnswerChoice"]);       }    MySQL_free_result($res);    return $this; } 

There are several new functions in this code. The Min() and Max() functions return the minimum and maximum values in a column. These functions operate on a group , meaning that if the optional group by command is used, an individual sum (and so on) is computed for each. If there is no group by , then the entire result is treated as being in the same group. Other group functions in MySQL are Count() , Sum() , and Avg() . These functions compute the number of values, the sum of the values, and the average of the values, respectively.

PHP
 function Question( ) {   MySQL_connect('sql.useractive.com', 'bigfun', '[REDACTED]');   MySQL_select_db('bigfun') 

Connect to our server. Select our database.

PHP
 $min = MySQL_query ("select Min(QuID) from Questions" );   $max = MySQL_query ("select Max(QuID) from Questions" ); 

Find out the maximum and minimum values for QuID s in the entire Questions table. Store them in appropriate variables .

PHP
 do{    $WhichQuID = rand(MySQL_result($min,0,0),MySQL_result($max,0,0));     res = MySQL_query("select * from Questions where QuID = $WhichQuID");     }   while(!MySQL_num_rows($res)); 

Using the bounds we already understand, we randomly select a QuID. We then make the question object equivalent to the data stored in the record with that QuID.

We are not sure if the QuID we selected is real. We know QuIDs are unique, and we know the min and max ”but we don't know if there are holes in the list. It is valid if there are. So we count the number of rows of output. If there are no rows of output, then the QuID is invalid ”it is not associated with a question.

In this case we select and test another QuID ”$res will have one row when we exit this while loop.

PHP
 MySQL_free_result($max); MySQL_free_result($min); 

Do not forget to release unused memory.

PHP
 $sqlquestion = MySQL_fetch_array ($res ); $sqlQuID = $sqlquestion["QuID"]; $this->Q = $sqlquestion["Q"]; $this->idkey= $sqlquestion["QuID"]; 

We take the data from our result. Then we store it in a couple of variables. $sqlQuID will be used later on. The last two declarations begin the actual construction of the object.

PHP
 MySQL_free_result($res); 

We free up some space.

PHP
 $res = MySQL_query ("select * from Answers where WhichQuestion =        $sqlQUIN"); 

We find the answers that are associated with the question we already have.

PHP
 $numrows = MySQL_num_rows($res); 

This line determines the number of times the following for loop will execute.

PHP
 for($i=0;$i<$numrows;$i++) {   $temp = MySQL_fetch_array($res);   array_push ($this->A , $temp["AnswerChoice"]);   } 

This for loop moves through the result we received from the Answers database and adds the value in the AnswerChoice column to the question object's answer array.

PHP
 return $this; 

This code returns the Question we have just created for use by the preexisting PHP code.



Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net