gasper000 Posted March 7, 2009 Share Posted March 7, 2009 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- //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("http://localhost/FlashDB/gettriviaquestioncount.php"); //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- //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("http://localhost/FlashDB/gettriviaquestion.php","_blank","GET"); varTriviaSend.sendAndLoad("http://localhost/FlashDB/gettriviaquestion.php",varTriviaReceive,"GET"); //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 <?php $host = 'localhost'; $user = 'username'; $pass = 'password'; $database = 'databasename'; ?> 2. <?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 One Last thing is the DB ScreenShot that was included with the tutorial: Do you have any ideas how can I edit the PHP file in order for the trivia to load the questions & answers ? Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/ Share on other sites More sharing options...
RussellReal Posted March 7, 2009 Share Posted March 7, 2009 we can't give you directions as to how to echo the data, considering we do not know how flash expects to receive the data.. E.G. XML, CSV. Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/#findComment-778776 Share on other sites More sharing options...
gasper000 Posted March 7, 2009 Author Share Posted March 7, 2009 we can't give you directions as to how to echo the data, considering we do not know how flash expects to receive the data.. E.G. XML, CSV. It receives the data through the following link varTriviaSend.sendAndLoad("http://localhost/FlashDB/gettriviaquestion.php",varTriviaReceive,"GET"); The tutorial also included the following paragraph: As you can see, the data is URL encoded so that it can be properly imported into Flash. The first link will pull the number of questions found in the database, held in the qCount variable. The second link is called when the application randomly chooses the next question to retrieve. The data includes currentQ, answerList, and correctList. An errorMsg element is included on each page as well. If an error occurs loading the data, the errorMsg variable will be populated. Otherwise, just the items listed above will be displayed on the respective pages. The answerList and correctList data lists are actually pipe-delimited (‘|’) lists that allow the application to quickly split the list of items into an array, using the split method of the array object in Flash. Again, since the application will continue to make calls to the database application, it needs to use the remote server demonstrated in the code below unless you develop and host your own application to retrieve the data. If you wish to develop your own script, be sure that your application formats the data properly. For this application, I use ASP.NET to retrieve data from a multiple data tables. Is it helpful ? Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/#findComment-778781 Share on other sites More sharing options...
alphanumetrix Posted March 7, 2009 Share Posted March 7, 2009 instead of using GET to send the data to flash, why don't you use POST or loadvarsnum from a file? use a php code such as this: $results=array(); $sql="SELECT * FROM `".$tablename."` WHERE `id`=".$_REQUEST[id]." LIMIT 1"; $result = mysql_query($sql, $link) or die('Error: ' . mysql_error()); while($a_row = mysql_fetch_array($result, MYSQL_ASSOC)) array_push($results, $a_row); if(count($results)){ echo('&myVar='.$results[0][field_name1]); }else{ echo('Sorry - no result found'); } that should display: &myVar=whateveryouwant - then you can just call that data from the PHP file, using loadVarsNum in Flash. I don't remember exactly how to write it, as I haven't coded AS in a while. This, or something very similar to this, should work though: loadVarsNumb("myfile.php","1"); if (myVar.1level == 'whatever') { // do something } sorry about the sloppiness of that. I just can't remember how to write it. I'm %99 percent sure it's not supposed to have .1level, but it is supposed to call the level somehow. If I was at home, I'd just look at my old work and show you, but unfortunately I am not. In anycase, you just load it, and then call it from the level. (maybe using X or something) Anyway, hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/#findComment-778856 Share on other sites More sharing options...
gasper000 Posted March 7, 2009 Author Share Posted March 7, 2009 bliljerk101: Thank you very much for your time & effort. I just did what the tutorial has been telling. They used GET, that's why I used the same method. I used your codes after adding some few ,ines to call the DB details .. username ..etc..I didn't use the 2nd piece of codes to call the php file because I think the one that exists works fine & I don't wanna mess it up. Actually, it didn't work & when I try to open the php file in my browser, it says "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in (sourcefile) on line 12 Error:" Below is the php file you wrote after adding my variables. <?php include("DB.php"); $tablename = 'tblTriviaAnswer'; mysql_connect ($host, $user, $pass); mysql_select_db ($database); $results=array(); $sql="SELECT * FROM `".$tablename."` WHERE `id`=".$_REQUEST[id]." LIMIT 1"; $results = mysql_query($sql, $link) or die('Error: ' . mysql_error()); while($a_row = mysql_fetch_array($results, MYSQL_ASSOC)) array_push($results, $a_row); if(count($results)){ echo('strAnswerList='.$results[0][answerText]); }else{ echo('Sorry - no result found'); } ?> What do you think ? Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/#findComment-779199 Share on other sites More sharing options...
Michdd Posted March 8, 2009 Share Posted March 8, 2009 First off you should send another variable that just tells the php script if it should run at all, which might be the reason that you're getting the error when just visiting the page. For example, send a variable called parse from flash->Php then add: if(isset($_GET['parse'])){ //Do the stuff.. } Quote Link to comment https://forums.phpfreaks.com/topic/148338-flash-php-mysql/#findComment-779205 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.