Jump to content

Stuck


kwdelre

Recommended Posts

I've been stuck too long for my beginner brain. Here is the majority of the code:

 

mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


$quizquery = mysql_query("SELECT Id, Total_Questions, Maximum_Score, Passing_Score, Correct_Answer FROM Module_1_Quiz WHERE  Id = '1'");
$quizqueryresult = mysql_fetch_array($quizquery);
$totalq = $quizqueryresult['Total_Questions'];
$maxscore = $quizqueryresult['Maximum_Score'];
$passingscore = $quizqueryresult['Passing_Score'];



$qvalue = $maxscore / $totalq; //scoring values
$Score=0;


$ansquery = mysql_query("SELECT Correct_Answer FROM Module_1_Quiz");



$QChoice_1 = $_POST['QChoice_1'];
$QChoice_2 = $_POST['QChoice_2'];





while ($answers = mysql_fetch_array($ansquery)) {

   $i++;
	if ($_POST['QChoice_$i'] == $answers['Correct_Answer']) {
  
$Score = round(($Score + $qvalue), 0);

}
else { echo "wrong";}
}


 

The problem i am running into is trying to score this quiz with the following:

 

while ($answers = mysql_fetch_array($ansquery)) {

   $i++;
	if ($_POST['QChoice_$i'] == $answers['Correct_Answer']) {
  
$Score = round(($Score + $qvalue), 0);

}
else { echo "wrong";}
}

 

I can't get the "if" statement to satisfy.....and am about to rip my eyes out...

 

thanks!

Link to comment
Share on other sites

Then why would this be working fine? i didn't "initialize" $i. When I did initialize $i it wouldn't work.

 

//Connect to database
mysql_connect($host, $dbusername, $password);
mysql_select_db($TxQuizdb);


//Retrieve Quiz Questions
$qquery = mysql_query("SELECT * FROM Module_1_Quiz");


$quiz = "<table width=600 border=1>";
while ($m1questions = mysql_fetch_array($qquery)) { 
    

$question = $m1questions["Question"];
$answer1 = $m1questions["Choice1"];
$answer2 = $m1questions["Choice2"];
$answer3 = $m1questions["Choice3"];

$i++;


$quiz .= "<tr><td> $question";
$quiz .= "</td></tr><br><tr><td>";
$quiz .= "<input type='radio' name='QChoice_$i'  value='$answer1' />";
$quiz .= "$answer1";
$quiz .= "<input type='radio' name='QChoice_$i'  value='$answer2' />";
$quiz .= "$answer2";
$quiz .= "<input type='radio' name='QChoice_$i'  value='$answer3' />";
$quiz .= "$answer3";
$quiz .= "</td></tr>";


}

$quiz .= "</table>";



echo $quiz;

 

 

Even when i do initialize it, it still doesn't work...

 

$QChoice_1 = $_POST['QChoice_1'];
$QChoice_2 = $_POST['QChoice_2'];

$i=1;



while ($answers = mysql_fetch_array($ansquery)) {


	if ($_POST['QChoice_$i'] == $answers['Correct_Answer']) {
  
$Score = round(($Score + $qvalue), 0);

}
else { echo "wrong";}
$i++;
}

 

thanks

Link to comment
Share on other sites

no one said that initializing $i would fix anything. it is possible to ignore good coding practices and have "working" code, especially if your error_reporting is turned down or off. otherwise, you would get notices about un-initialized variables.

 

I don't see where you ever initialize the $i variable, so it may not contain the value you think it does.

Link to comment
Share on other sites

Bluesky - I know you are trying to help so thank you. i'm learning a lot in a small time. I finally got it to work with this:

 

$ansquery = mysql_query("SELECT * FROM Module_1_Quiz");
//$answers = mysql_fetch_array($ansquery);


$QChoices = array($_POST['QChoice_1'], $_POST['QChoice_2']);



$i = 0;
while ($answers = mysql_fetch_array($ansquery)) {


  if($QChoices[$i] == $answers['Correct_Answer']) {
  
$Score = round(($Score + $qvalue), 0);

   
  }
  $i++;
}

 

Thanks again for lending your eyes.

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.