0

I am currently in the process of making a quiz, with the table format:

| id |     question     | option1 | option2 | option3 | answer |
--------------------------------------------------------
| 1  | What is my name? |  Dave   |   Bob   | Charles | Linda  |

I am trying to verify user's answers and I'm not quite sure how to go about it. I am a bit stumped using the foreach loop to assign array values to other arrays. The following code within matresult.php incorrect but it should show what I am trying to achieve:

Within matquiz.php I have

//MySql query to select all data from matquiz table
$query="SELECT * FROM MatQuiz";                        
//Assign the query to a result variable
$result = mysql_query($query, $connect);

while($row = mysql_fetch_assoc($result)){
CODE
}

Within matresult.php I have

$array3 = array();
$array4 = array();
$array5 = array();
$array6 = array();
$array7 = array();
$array8 = array();

$finalresults = array($row['ID'], $row['question'], $row['option1'], $row['option2'], $row['option3'], $row['answer']);

foreach($finalresults as $value){                                

     array_push($array3, $value['ID']);      //ID
     array_push($array4, $value['question']);  //question
     array_push($array5, $value['option1']); //op1
     array_push($array6, $value['option2']); //op2
     array_push($array7, $value['option3']); //op3
     array_push($array8, $value['answer']);  //answer
}

What I am trying to achieve is to push each $finalresults array value into 6 seperate arrays, and do this for 5 questions/rows.

So for example, I want array3 to hold all 5 question IDs. [1,2,3,4,5] and I would like array4 to hold all 5 questions etc.

I am aware that the syntax within the foreach loop is wrong but this is my question to you, how would I go about achieving this?

Kieran
  • 69
  • 7
  • From looking at what you have done, it looks correct, albeit a bit verbose. What issues/errors are you getting? – Phillip Hartin Feb 08 '17 at 00:43
  • Are you calling `mysql_fetch_assoc()` just once? If the query is expected to return several rows, you will need to call it in a `while` loop. Be advised also that the `mysql_*()` functions were deprecated long ago and no longer available by default in recent PHP versions. mysqli or PDO are suitable replacements. – Michael Berkowski Feb 08 '17 at 00:43
  • It is called within a while loop, as per the **EDIT**. – Kieran Feb 08 '17 at 01:00

1 Answers1

0

First of all, you should probably be using mysqli_query and mysqli_fetch_array, as those are more common nowadays. See here for reasons why.

I also think you don't need the foreach at all, just use mysqli_fetch_array in a while loop, no need for another loop in there.

As for the array_push() calls, you could write those like this:

$array3[] = $row['ID'];

And they have the same effect, and I think that is more common. See here in the PHP manual. When you don't put an specific index in an array assignment, PHP just assigns to the next available numeric index.

I'm not sure what you are using $finalresults for, but it isn't needed either as far as I can tell, since you were just using it as a temporary holding area for data you were going to assign to your array3-array8 anyway. I've removed that from my example.

This example has everything in the same file, split it up if you need to:

<?php

$array3 = array();
$array4 = array();
$array5 = array();
$array6 = array();
$array7 = array();
$array8 = array();

$query = "SELECT * FROM MatQuiz";                        
$result = mysqli_query($connect, $query);

while ($row = mysqli_fetch_array($result)) {

  $array3[] = $row['ID'];
  $array4[] = $row['question'];
  $array5[] = $row['option1'];
  $array6[] = $row['option2'];
  $array7[] = $row['option3'];
  $array8[] = $row['answer'];

}
Community
  • 1
  • 1
djs
  • 3,947
  • 3
  • 14
  • 28
  • Ideal! Thank you very much, seems I was vastly over complicating things. Still very new to using php. I will update the code to use mysqli also. Appreciate the help, thank you. – Kieran Feb 08 '17 at 01:08
  • No problem, glad I could help! – djs Feb 08 '17 at 01:09