kwdelre Posted January 11, 2011 Share Posted January 11, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/ Share on other sites More sharing options...
Pikachu2000 Posted January 11, 2011 Share Posted January 11, 2011 I don't see where you ever initialize the $i variable, so it may not contain the value you think it does. Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158030 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 $i++; won't work for initializing it? Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158047 Share on other sites More sharing options...
BlueSkyIS Posted January 11, 2011 Share Posted January 11, 2011 $i++ is incrementing, not initializing. you shouldn't increment anything that isn't initialized. Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158053 Share on other sites More sharing options...
kwdelre Posted January 11, 2011 Author Share Posted January 11, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158103 Share on other sites More sharing options...
BlueSkyIS Posted January 11, 2011 Share Posted January 11, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158110 Share on other sites More sharing options...
kwdelre Posted January 12, 2011 Author Share Posted January 12, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158191 Share on other sites More sharing options...
jcbones Posted January 12, 2011 Share Posted January 12, 2011 The reason was that you where trying to use a variable inside of a single quoted string. PHP doesn't parse variables inside of a single quoted string, you must use a double quoted string. Quote Link to comment https://forums.phpfreaks.com/topic/224113-stuck/#findComment-1158194 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.