AbraCadaver wrote:So what do those answers have to do with true or false?
The number of answers determine how many iterations the foreach loop will do.
For the line truefalse = '$num$j' I want this to be variable $num1 the first time round, $num2 the second, $num3 the third and so on.
So if the $_POST holds
Array ( [id] => 114 [answer] => Array ( [0] => nine [1] => two [2] => eight [3] => seven ) [quest] => Array ( [0] => 4 ) )
Then $num_true will equal 1 and the for loop will only run once, which will set $num4 = 'T'
The foreach loop will run 4 times and each times, each time I want the $sql variable to hold
INSERT INTO answers SET answer ='nine', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='two', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='eight', questionid = '114', truefalse ='F'
INSERT INTO answers SET answer ='seven, questionid = '114', truefalse ='T'
Code: Select all
$num_ans = count($_POST['answer']);
$num_true = count($_POST['quest']);
$num1 = 'F';
$num2 ='F';
$num3 = 'F';
$num4 = 'F';
for ($i=0;$i<$num_true;$i++)
{
if($_POST['quest']{$i} == 1)
{
$num1 = 'T';
}
elseif($_POST['quest']{$i} == 2)
{
$num2 = 'T';
}
elseif($_POST['quest']{$i} == 3)
{
$num3 = 'T';
}
elseif($_POST['quest']{$i} == 4)
{
$num4 = 'T';
}
}
$j = 1;
foreach ($_POST['answer'] as $value)
{
//$x = "\$num".$j;
$sql = "INSERT INTO answers set
answer = '$value',
questionid = '$_POST[id]',
truefalse = '$num$j'"; //I want this to be variable $num1 the first time round, $num2 the second, $num3 the third and so on.
$j++;
if (!mysqli_query($link, $sql))
{
$error = 'Error inserting answer into answers '
.mysqli_error($link);
include 'error.html.php';
exit();
}
}
I hope this makes things a little clearer
Thanks for the help