Jump to content

Comparing strings and counting matches.


gerrydewar

Recommended Posts

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 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();

[/code]

Any ideas why this might be happening?
Link to comment
Share on other sites


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?
Link to comment
Share on other sites

Try this:
[code]<?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;
}
?>[/code]

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

Ken
Link to comment
Share on other sites

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
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.