gerrydewar Posted March 18, 2006 Share Posted March 18, 2006 Hello everyone,I'm trying to compare 2 strings and increment a counter everytime i have a match. Basically the answer given by a user matched with the correct answer from the database. Sounds easy but for some reason my score starts at 10 and works its way down instead of starting at zero and going up. For example, if i click on my submit button with no answers radio buttons selected my score is 10. If i select one correct answer and nothing else i get 9, 2 correct gives me 8 and so on. However, it gets down to 4 and sticks for a while. In the end 10 correct answers gives me a score of 3, sometimes 4. The code is below:[code]//Transferring question_ids from source pageif(isset($_POST['submit'])){$ints = unserialize(stripslashes($_POST['ints']));echo "<pre>\n";print_r($ints);echo "</pre>\n";}//Transferring radio button values selected by user on source pageif (array_key_exists('submit',$_POST)){$description = $_POST['choice'];echo "<pre>\n";print_r($description);echo "</pre>\n";}//Comparing users answer with correct answer and adding one to total score each time$score = 0;//Connect to db.require_once ('../mysql_connect.php');for($counter = 0; $counter < 10; $counter++){$query = "SELECT answer FROM questions WHERE question_id = '$ints[$counter]'";//Run query$result = @mysql_query ($query) or die('Problem with query:' . $query . '<br/>' . mysql_error());while($row=mysql_fetch_array($result)){//echo $row[0];}$answer = $row[0];$guess = $description[$counter];if($answer==$guess){$score++;}$row[0] = "";}// end loopecho $score;mysql_close();[/code]Any ideas why this might be happening? Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/ Share on other sites More sharing options...
gerrydewar Posted March 19, 2006 Author Share Posted March 19, 2006 I've tried resetting $row[0] to be "" everytime the loop gets to the end so that it is empty but i still get a score of 10 as my initial score when no answers are selected. There is a problem with my for loop somewhere i think but can't see it. Do i need an else in the if statement? Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/#findComment-18759 Share on other sites More sharing options...
kenrbnsn Posted March 19, 2006 Share Posted March 19, 2006 Try this:[code]<?phpif(isset($_POST['submit'])){ $ints = unserialize(stripslashes($_POST['ints'])); $description = $_POST['choice']; echo '<pre> $ints:'.print_r($ints,true)."</pre>\n"; echo '<pre> $description: ' . print_r($desription,true) . '</pre>';//Comparing users answer with correct answer and adding one to total score each time $score = 0;//Connect to db. require_once ('../mysql_connect.php'); for($counter = 0; $counter < count($ints); $counter++){ $query = "SELECT answer FROM questions WHERE question_id = '$ints[$counter]'"; //Run query $result = @mysql_query ($query) or die('Problem with query:' . $query . '<br/>' . mysql_error()); $row = mysql_fetch_asscoc($result); $answer = $row['answer']; $guess = $description[$counter]; if($answer == $guess) $score++; } echo $score;}?>[/code]Without access to your database it's hard to test.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/#findComment-18819 Share on other sites More sharing options...
gerrydewar Posted March 19, 2006 Author Share Posted March 19, 2006 Tried those changes and the count starts at zero now which is great. It still doesn't increment properly though when a user gets a question right. I can send the database files to you. Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/#findComment-18848 Share on other sites More sharing options...
kenrbnsn Posted March 20, 2006 Share Posted March 20, 2006 Found the problem...You have [code]<?php $guess = $description[$counter]; ?>[/code]The problem is that the index into the $description array is not a sequential number, but the id of the question asked. The line you need here is: [code]<?php $guess = $description[$ints[$counter]]; ?>[/code]The "[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$ints[$counter][!--colorc--][/span][!--/colorc--]" holds the id of the question asked.Ken Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/#findComment-18935 Share on other sites More sharing options...
gerrydewar Posted March 20, 2006 Author Share Posted March 20, 2006 Genius. That has solved the problem.Thanks Ken Quote Link to comment https://forums.phpfreaks.com/topic/5240-comparing-strings-and-counting-matches/#findComment-18991 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.