Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Flash, PHP & MySQL

Status
Not open for further replies.

triquad

Programmer
Feb 5, 2009
16
EG
I have been developing a flash trivia using some tutorial. Everything went fine & I have added some new features to the trivia. The tutorial provides you with a url link to their database in order for you to test your trivia. They used ASP.NET as a way to load & display questions from their DB. Now I should create my own trivia questions & answers and store them in my own DB. The tutorial teaches you how to develop the flash trivia but they didn't mention how to make the ASP.NET file to link the DB to the flash trivia. As I never used ASP.NET before, I thought I would use PHP & MySQL instead. I created my own DB, inserted some random questions & answers and wrote the php code. When I try to test the flash trivia using my DB & the php file I have wrote, it doesn't seem to work. The flash trivia file is working normally but the questions & answers are not loaded from the MySQL DB.

I have included below the ActionScript I use (Working Fine) Plus PHP file (doesn't seem to be working) & screen shots of the MySQL DB.

ActionScript -Frame 1-
Code:
//Stop the timeline
stop();

//Determine the number of questions in the database
var numQCount:Number;
//Create trivia data receiver objects
var varTriviaCount:LoadVars = new LoadVars();
//Load the data from the external web application
varTriviaCount.load("[URL unfurl="true"]http://localhost/FlashDB/gettriviaquestioncount.php");[/URL]

//Once the data loads from remote server, get the count
varTriviaCount.onLoad = function(blnSuccess:Boolean):Void {
//See if data was loaded
if(blnSuccess){
//Parse the data returned
numQCount = varTriviaCount.qCount;
} else {
//Display an error
trace("Error occurred on LoadVars");
}
};

//When the Faceoff button is clicked, go to the trivia game
btnLaunch.onRelease = function():Void {
gotoAndStop(10);
};

ActionScript -Frame 10-
Code:
//Initialize trivia variables
var numCurrentQ:Number = 1;
var numCurrentScore:Number = 0;
var numTimerVal:Number = 10;
var numRandomQ:Number;
var strCurrentQ:String;
var arrQuestionList:Array = new Array();
var strAnswerList:String;
var strCorrectList:String;
var strAnswerText:String;
var strCorrectAnswer:String;
var arrAnswerText:Array = new Array();
var arrCorrectAnswer:Array = new Array();
var numCorrectAnswer:Number;

//Start the trivia quiz
getNextQuestion();

function getNextQuestion(){
//Initialize grades
txtGradeC1._visible = false;
txtGradeC2._visible = false;
txtGradeC3._visible = false;
txtGradeC4._visible = false;
txtGradeW1._visible = false;
txtGradeW2._visible = false;
txtGradeW3._visible = false;
txtGradeW4._visible = false;
//Get a random number to determine current question
numRandomQ = Math.floor(Math.random()*numQCount-1) + 1;
//Determine if this question has been asked
var blnNewQuestion:Boolean = true;
//See if this is the first question
if(numCurrentQ!=1){
//This is not the first question
for(z=0;z<arrQuestionList.length;z++){
if(numRandomQ==arrQuestionList[z]){
//This question has already been asked
blnNewQuestion = false;
}
}
}
//See if this question has been asked
if(blnNewQuestion){
//Add the current question number to the list of asked questions
arrQuestionList.push(numRandomQ);
//Create trivia data sender & receiver objects
var varTriviaSend:LoadVars = new LoadVars();
var varTriviaReceive:LoadVars = new LoadVars();
//Load the data from the external web application
varTriviaSend.questionNum = numRandomQ;
//varTriviaSend.send("[URL unfurl="true"]http://localhost/FlashDB/gettriviaquestion.php","_blank","GET");[/URL]
varTriviaSend.sendAndLoad("[URL unfurl="true"]http://localhost/FlashDB/gettriviaquestion.php",varTriviaReceive,"GET");[/URL]

//Once the data loads from remote server, build the question/answer display
varTriviaReceive.onLoad = function(blnSuccess:Boolean):Void {
//See if data was loaded
if(blnSuccess){
//Parse the data returned
//Set the question and answer text
strCurrentQ = varTriviaReceive.currentQ;
txtQuestion.text = strCurrentQ;
//Set the answers
strAnswerList = varTriviaReceive.answerList;
arrAnswerText = strAnswerList.split("|");
txtAnswer1.text = arrAnswerText[0];
txtAnswer2.text = arrAnswerText[1];
txtAnswer3.text = arrAnswerText[2];
txtAnswer4.text = arrAnswerText[3];
//Unhide the buttons
btnAnswer1._visible = true;
btnAnswer2._visible = true;
btnAnswer3._visible = true;
btnAnswer4._visible = true;
//Unhide the incorrect answers
txtAnswer1._visible = true;
txtAnswer2._visible = true;
txtAnswer3._visible = true;
txtAnswer4._visible = true;
//Determine the correct answer
strCorrectList = varTriviaReceive.correctList;
arrCorrectAnswer = strCorrectList.split("|");
for(y=0;y<arrCorrectAnswer.length;y++){
if(arrCorrectAnswer[y]==1){ numCorrectAnswer = y+1; }
}
} else {
//Display an error
trace("Error occurred on LoadVars");
}
}
//Set the question number/score display
txtQNum.text = numCurrentQ;
txtScore.text = numCurrentScore;
txtTimer.text = numTimerVal;
btnNext._visible = false;
//Run the countdown timer
countDown = function () {
numTimerVal--;
txtTimer.text = numTimerVal;
if (numTimerVal == 0) {
clearInterval(timer);
noAnswer();
btnNext._visible = true;
}
};
timer = setInterval(countDown, 1000);
} else {
getNextQuestion();
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer1.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 1){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC1._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer2._visible = false;
txtAnswer3._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW1._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer2.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 2){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC2._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer3._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW2._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer3.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 3){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC3._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer2._visible = false;
txtAnswer4._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW3._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
//Set the grades
txtGradeC4._visible = true;
}
}
}

//When the user clicks an answer button, determine if it is correct
btnAnswer4.onRelease = function():Void {
//stop the timer
clearInterval(timer);
//Unhide the 'Next' button
btnNext._visible = true;
//See if answer is correct
if(numCorrectAnswer == 4){
//Answer is correct
numCurrentScore = numCurrentScore + (numTimerVal * 100);
txtScore.text = numCurrentScore
//Set the grades
txtGradeC4._visible = true;
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
txtAnswer1._visible = false;
txtAnswer2._visible = false;
txtAnswer3._visible = false;
} else {
//Answer is not correct
//Hide the buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Set the grades
txtGradeW4._visible = true;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
//Set the grades
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
//Set the grades
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
//Set the grades
txtGradeC3._visible = true;
}
}
}

//Set the display when no answer is selected
function noAnswer(){
//Hide all the answer buttons
btnAnswer1._visible = false;
btnAnswer2._visible = false;
btnAnswer3._visible = false;
btnAnswer4._visible = false;
//Hide the incorrect answers
if(numCorrectAnswer != 1){
txtAnswer1._visible = false;
} else {
txtGradeC1._visible = true;
}
if(numCorrectAnswer != 2){
txtAnswer2._visible = false;
} else {
txtGradeC2._visible = true;
}
if(numCorrectAnswer != 3){
txtAnswer3._visible = false;
} else {
txtGradeC3._visible = true;
}
if(numCorrectAnswer != 4){
txtAnswer4._visible = false;
} else {
txtGradeC4._visible = true;
}
}

//When the user clicks the 'Next' button, go to the next question
btnNext.onRelease = function():Void {
//See if this is the last question
if(numCurrentQ < 10){
//Not the last question, pull next question
numCurrentQ++;
numTimerVal = 10;
getNextQuestion();
//Erase the grades
txtGradeC1._visible = false;
txtGradeC2._visible = false;
txtGradeC3._visible = false;
txtGradeC4._visible = false;
txtGradeW1._visible = false;
txtGradeW2._visible = false;
txtGradeW3._visible = false;
txtGradeW4._visible = false;
} else {
//This is the last question, jump to frame 20
gotoAndStop(20);
}
}



PHP Files
1. DB.php
Code:
<?php 
$host = 'localhost'; 
$user = 'username';
$pass = 'password';
$database = 'databasename';
?>

2.
Code:
<?php 


include("DB.php"); 

$table = 'tblTriviaAnswer';

mysql_connect ($host, $user, $pass); 
mysql_select_db ($database);

$result = mysql_query ( "SELECT * FROM `tbltriviaanswer`"); 
$newresult = mysql_query ("SELECT * FROM `tbltriviaquestion`");	



$i = 1; 

	while ( $i <= 10){

		$row = mysql_fetch_array($result); 
		 
		extract($row); 
		echo "$answerText";
		echo "$correctAnswer";
		$i = $i + 1; 
	}

$j = 1; 

	while ( $j <= 10){

		$row = mysql_fetch_array($newresult); 
		 
		extract($row);
		echo "$questionNum";
		$j = $j + 1;

	}
?>

MySQL DB ScreenShots
tbltriviaquestions.jpg


tbltriviaanswer.jpg



One Last thing is the DB ScreenShot that was included with the tutorial:
tdb.jpg



Do you have any ideas how can I edit the PHP file in order for the trivia to load the questions & answers ?
 
The loadVars object requires responses to be formatted like a GET request to a PHP script.

myscript.php?firstName=bob&lastName=smith

So you need to use PHP to format the response sent to loadVars.

Here are some extended examples.

myVar1=Some%20URL%20Encoded%20value%20for%20flash&
myVar2=Some%20other%20URL%20Encoded%20value%20for%20flash&

objLoadVars.myVar1 would contain 'Some Encoded value for flash' w/o quotes in Flash when the onLoad event fires.

objLoadVars.myVar2 would contain 'Some other Encoded value for flash' w/o quotes in Flash when the onLoad event fires.

Always add an ampersand to the end of the value.

If you want to send an empty var:

myVar=&

objLoadVars.myVar would be defined but empty in Flash when the onLoad event fires.

Always URLEncode/escape values sent to Flash. If you have an ampersand in your value for example:

myVar=This%26that&

objLoadVars.myVar would contain 'This & that' w/o quotes in Flash when the onLoad event fires.

Hope this helps.


-a6m1n0

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top