Jump to content

Trouble Displaying Data From A Mysql Database Into Flash


dinita

Recommended Posts

Just wondered if anyone could offer any advice on parsing information from a mySQL database into a Flash movie clip with 5 dynamic text fields.

 

 

the flash movie clip loops eight times creating an id on ever loop, it will display questions for a quiz game. it has 5 text fields to hold the questions and its possible answers, I would like to draw the questions and answers from the database using php.

 

The tables in my database are structured like this:

 

Table structure for table `answers`

--

 

CREATE TABLE `answers` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`ANSWER` varchar(80) NOT NULL,

`CORRECT` tinyint(4) NOT NULL DEFAULT '0',

`question_ID` int(11) NOT NULL,

PRIMARY KEY (`ID`),

UNIQUE KEY `ID` (`ID`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=33 ;

 

-- --------------------------------------------------------

 

--

-- Table structure for table `questions`

--

 

CREATE TABLE `questions` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`text` varchar(80) NOT NULL,

`quiz_ID` int(11) NOT NULL,

PRIMARY KEY (`ID`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;

 

-- --------------------------------------------------------

--

-- Table structure for table `quizName`

--

 

CREATE TABLE `quizName` (

`ID` int(11) NOT NULL AUTO_INCREMENT,

`Name` varchar(45) NOT NULL,

`Description` varchar(45) NOT NULL,

PRIMARY KEY (`ID`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;

 

I planned to first start a connection to a database, then create a function that select's the data from the database using an ISSET to know what number of the loop we are on.

 

so i guess my questions are:

1) is it possible to use the id number created every cycle of the loop in the flash actionscript in my php?

2) could I use this id to select the questions using my Question ID?

 

hope someone can help.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

My code is below, but in short I created arrays to represent each of the dynamic text fields in my Flash project. Its probably not the smartest way to do this but I'm just a beginner.

 

<?php
error_reporting(-1);
session_name("name");
session_start();
//functions
function redirect_page($url)
   {
    if(!headers_sent())
    {
	    header("Location: ".$url);
	    exit;
    }
    else				   
    {
	    echo '<script type="text/javascript">';
	    echo 'window.location.href="'.$url.'";';
	    echo '</script>';
	    echo '<noscript>';
	    echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
	    echo '</noscript>';
	    exit;
    }
   }
function getquestions($id)
{
   $sql    =mysql_query("select text FROM questions WHERE quiz_ID =$id ");
   $questions   = array();

   while($row   = mysql_fetch_row($sql))
   {
    $questions[] = $row[0];
   }
   return $questions;
}
function getquesid($id)
{
   $sql    =mysql_query("select ID FROM questions WHERE quiz_ID =$id ");
   $questions   = array();

   while($row   = mysql_fetch_row($sql))
   {
    $questions[] = $row[0];
   }
   return $questions;
}
function getanswers()
{
   foreach ($quesid as $id)
   {
    $sql    = mysql_query("select answer FROM answers WHERE question_ID= $id ");
   }
   $answers = array();

   while($row    =mysql_fetch_row($sql))
   {
    $answers[] = $row[0];
   }
   return $answers;
}
function getcorrect($id)
{
   $sql    = mysql_query("SELECT correct FROM answers WHERE question_ID= $id ");
   $correct   =array();

   while($row    =mysql_fetch_assoc($sql))
   {
    $correct[]    =$row["correct"];
   }
   return $correct;
}
//Connect to Database
$con = mysql_connect();
if(!$con)
{
   die('Could not connect: '.mysql_error());
}
else
{
   // SELECT DATABASE
   mysql_select_db("quizCreation", $con);
}

   //GET NAME
   if(isset($_SESSION['quizid']))
   {
    $id   =$_SESSION['quizid'];
   }
   else
   {
    $id    = 6;
   }

   //get quiz name
   $sql = mysql_query("SELECT Name FROM quizName WHERE ID= '$id'");
   while ($row = mysql_fetch_array($sql))

   {
    $name = $row['Name'];
   }

   //use quiz id to output array of question ID's and text

   $question    = implode ("/",getquestions($id));
   $quesid    =getquesid($id);
   $answers = array();

// use question ID's to output 4 arrays

   foreach ($quesid as $id)
   {

    $sql    = mysql_query("select answer FROM answers WHERE question_ID= $id ");

    while($row    =mysql_fetch_row($sql))
    {
	    $answers[] = $row[0];
    }
   }


   $one = array( $answers[0],$answers[4],$answers[8],$answers[12],$answers[16],$answers[20],$answers[24],$answers[28]);
   $two = array( $answers[1],$answers[5],$answers[9],$answers[13],$answers[17],$answers[21],$answers[25],$answers[29]);
   $three  =array( $answers[2],$answers[6],$answers[10],$answers[14],$answers[18],$answers[22],$answers[26],$answers[30]);
   $four  =array( $answers[3],$answers[7],$answers[11],$answers[15],$answers[19],$answers[23],$answers[27],$answers[31]);


   $answerone = implode ("/",$one);
   $answertwo = implode ("/", $two);
   $answerthree = implode ("/", $three);
   $answerfour =implode ("/", $four);


// use question ID's to output array of correct answers
$cor = array();
   foreach ($quesid as $id)
   {

    $sql    = mysql_query("select correct FROM answers WHERE question_ID= $id ");

    while($row    =mysql_fetch_row($sql))
    {
	    $cor[] = $row[0];
    }
   }

$cor1    =array
($cor[0],$cor[4],$cor[8],$cor[12],$cor[16],$cor[20],$cor[24],$cor[28]);
$cor2    =array
($cor[1],$cor[5],$cor[9],$cor[13],$cor[17],$cor[21],$cor[25],$cor[29]);
$cor3    =array
($cor[2],$cor[6],$cor[10],$cor[14],$cor[18],$cor[22],$cor[26],$cor[30]);
$cor4    =array
($cor[3],$cor[7],$cor[11],$cor[15],$cor[19],$cor[23],$cor[27],$cor[31]);

   $correctone = implode ("/", $cor1);
   $correcttwo = implode ("/", $cor2);
   $correctthree = implode("/", $cor3);
   $correctfour = implode("/", $cor4);

//echo this information
   echo "name=". urlencode($name);
   echo "&questions=" . urlencode($question);
   echo "&answerone=". urlencode($answerone);
   echo "&answertwo=". urlencode($answertwo);
   echo "&answerthree=". urlencode($answerthree);
   echo "&answerfour=". urlencode($answerfour);
   echo "&correctone=".urlencode($correctone);
   echo "&correcttwo=".urlencode($correcttwo);
   echo "&correctthree=".urlencode($correctthree);
   echo "&correctfour=".urlencode($correctfour);

   mysql_close($con);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.