Reputation: 249
In a table i have sums ie 1+1, im trying to make the code work the answer out and check it against the answer given by the end user.
Code
$stmt = $db->prepare("select * from exams where username = :username");
$stmt->execute(array(':username' => "$username"));
$row = $stmt->fetch();
$stmt->execute();
$question1 = $row['q1'];
foreach( $stmt as $row )
{
echo "q1<td>" . $row['q1'] . "</td>";
echo "<td>" . $row['q1a'] . "</td>";
echo "<td>Correct = " . $question1 . "</td>";
}
Table information on the row q1
type = text
the varibles
$row['q1'] = 1+1
$row['q1a'] = there answer
$question1 = the right answer
i have tried $question1 = $row['q1'];
and the output is always the sum in the table.
is there a way to echo the sum (in my table) out and find the value?
Upvotes: 1
Views: 60
Reputation: 632
PHP will not automatically work out the answer to the given question as this is currently stored as a string, you would first need to split the string into each individual part of the sum. You may wish to read more into the explode function.
Once the string is exploded into its parts (see example below) you can then add the two individual parts together to get the answer you were originally seeking:
$questionParts = explode('+',$row['q1']);
$answer = $questionParts[0] + $questionParts[1];
Upvotes: 1