PDA

View Full Version : Randomly selecting text


peter5
12-04-2005, 12:04 AM
Hi, I am creating "who wants to be a millionaire" style game for use in a highschool classroom. I have played around a little with writing text from flash to a textfile, but it has been over 12months since I wrote a php script that accessed a database.

I am firstly just wanting some advice about the best method (so I don't waste my time when I could be doing it much more simply). I am also not sure of the full capabilities of Actionscript (ie compare to full-blown languages such as C++ and VB).

My thoughts are as follows:
Use a mySQL database to store the quiz questions. (each quiz question is actually a row consisting of the following fields: level of difficulty (num), question, Answers A, B, C,D, Audience stats, phone friend comments)

When the flash program loads, it will want an initial question based on the current level of difficulty.
Flash will call a php script (can flash pass the level of difficulty as a variable, or will I need individual php scripts with a built-in levels of difficulty?)
The script will return a random row from the database based on the level of difficulty (consisting of all the fields in that row).
Will the php script be able to return 9 strings, or will it only a single string. In which case will I have to parse the string using actionscript?
Will php script be able to generate a random database row?

finally:
Is the best method to use?

Regards,
Cassie.

Xeef
12-04-2005, 12:44 AM
can flash pass the level of difficulty as a variable

Yes

Will the php script be able to return 9 strings

Yes

Will php script be able to generate a random database row?

Hmmmm not fully understand what you mean by "random"

it can generate a NEW one

Flash Gordon
12-04-2005, 12:51 AM
The script will return a random row from the database based on the level of difficulty (consisting of all the fields in that row).
I would do something like//quasi code
$query = "SELECT * from tablename WHERE difficulty_level='hard'";
$results = mysql_query($query);
while ($row = mysql_fetch_array($results)) {
$contents[] = $row;
$contents_question[] = $row['question'];
}
//$num = genterate random number here
$myQuestion = $contents_question[$num]

peter5
12-04-2005, 01:17 AM
Hi thanks for you quick replies.

I just dug out my old php/mySQL code from last year. I can't believe I actually knew what I was doing back then! However it still left me scratching my head.

I had just gotten to this stage when I got your post.
//Get the license_key
// $rec is of type "ADODB.Recordset"
$row_query=mysql_query("SELECT * FROM '".$table."' WHERE level=1 ORDER BY RAND() LIMIT 1");
$row=mysql_fetch_array($row_query);

so the random row can be picked out of the table using SQL, that way I only need to deal with a single row. (my understanding is that this method is fine with a small database <10000).

Do I now pass $row to my actionscript code, or do I need to break it into the 9 variables first, i.e.
//break the row into the various variables
$tQuestion=$row["question"];
$tAnswerA=$row["answerA"];
$tAnswerB=$row["answerB"];
$tAnswerC=$row["answerC"];
$tAnswerD=$row["answerD"];
...
$row=null;