Jump to content

Archived

This topic is now archived and is closed to further replies.

gerrydewar

Comparing strings and counting matches.

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?

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

×

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.