Jump to content


Photo

Comparing strings and counting matches.


  • Please log in to reply
5 replies to this topic

#1 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 18 March 2006 - 06:35 PM

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:


//Transferring question_ids from source page

if(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 page

if (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 loop

echo $score;
mysql_close();


Any ideas why this might be happening?

#2 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 19 March 2006 - 08:27 AM


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?

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 19 March 2006 - 04:42 PM

Try this:
<?php
if(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;
}
?>

Without access to your database it's hard to test.

Ken

#4 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 19 March 2006 - 07:34 PM

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.

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 March 2006 - 01:15 AM

Found the problem...

You have
<?php $guess = $description[$counter]; ?>
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:
<?php $guess = $description[$ints[$counter]]; ?>
The "[!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]$ints[$counter][!--colorc--][/span][!--/colorc--]" holds the id of the question asked.

Ken

#6 gerrydewar

gerrydewar
  • Members
  • PipPipPip
  • Advanced Member
  • 36 posts

Posted 20 March 2006 - 09:03 AM

Genius. That has solved the problem.

Thanks Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users