Page 1 of 1

Session issues

Posted: Tue Aug 02, 2005 8:56 am
by nawhaley
Hey I'm working on setting up a session to print out contents from a database then wait for user reponse and print out the next question. So far the original call to my printing function works fine but when I hit the submit button it will not pull the next question from the database can anyone take a look at this and give me some clues as to where I went wrong?

Code: Select all

<?
session_start();
include 'databasetest2.php';


$_SESSION["link"] = odbc_connect("testdatabase","bob","somestuff");

/* set up query for questions */
$_SESSION["questionquery"] = "Select * from tblQuestions";
$_SESSION["questionresults"] = odbc_exec($_SESSION["link"],$_SESSION["questionquery"]);

$_SESSION["questioncounter"] = 1;
$_SESSION["counter"] = 1;


/* find out type of question Single answer or multiple answer */
$typeofquestion = odbc_result($_SESSION["questionresults"],"QuestionType");

while(odbc_fetch_row($_SESSION["questionresults"]))
{
$_SESSION["questioncounter"]++;

}




/*pull questioncode from the table tblQuestions*/
$questioncode = odbc_result($_SESSION["questionresults"],"QuestionCode");


/* get answers for the questions */
$queryanswers = "Select * from tblAnswers";
$answerresult = odbc_exec($_SESSION["link"],$queryanswers);

/*pull questioncode from tblAnswers*/
$ansquestioncode = odbc_result($answerresult,"QuestionCode");

writequestion();



?>
thats the first file here is the datbasetest2.php file

Code: Select all

<?
session_start();

writequestion();

function writequestion()
{

if($_SESSION["counter"] < $_SESSION["questioncounter"]):
 
  

/*gets the type of questions and question itself from database results,prints question*/  
   print('<FORM METHOD = "POST" ACTION = "databasetest2.php">');
   $cq = odbc_fetch_row($_SESSION["questionresults"],$_SESSION["counter"]);
  
   $question = odbc_result($_SESSION["questionresults"],"Question");
   
   $questioncode = odbc_result($_SESSION["questionresults"],"QuestionCode");
   $typeofquestion = odbc_result($_SESSION["questionresults"],"QuestionType");
   $queryanswers = "Select Answer from tblAnswers where QuestionCode ='$questioncode'";
   $answerresult = odbc_exec($_SESSION["link"],$queryanswers);
   print("$typeofquestion"."<BR>");
   print("$questioncode"."<BR>");
//print("$question"."<BR>");
   

/* if the question is a single answer S then print as radio button selection else use checkboxes*/  
    if($typeofquestion =="S"):
             
        $answer = odbc_result($answerresult,"Answer");
        $correct = odbc_result($answerresult,"Correct");
        print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");    


 
      while(odbc_fetch_row($answerresult))
        {
           
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "radio" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }
      
    else:
     
       while(odbc_fetch_row($answerresult))
        {
          
          $answer = odbc_result($answerresult,"Answer");
          $correct = odbc_result($answerresult,"Correct");
          print('<INPUT TYPE = "checkbox" NAME = "answer" VALUE= "$correct">'."$answer"."<BR>");
        }

   endif;
     print('<INPUT TYPE ="submit" VALUE = "submit">');
     print('</FORM>');
     $_SESSION["counter"]++; 
endif;
   }
?>
Sorry about the large amount of code but I couldnt think of a easy way to have this make sense without letting you view the full source code.

Posted: Tue Aug 02, 2005 10:17 am
by nawhaley
question and a bump for this, would it be easier to print the question out from the inital page the first time and have it call the second page. Then once the second page is called go inside my writequestion() function and have it do a PHP_SELF call under the form action?