0

I have a quiz DB which has particular arrangement as shown below:

"question" : "What are the two binary numbers?",
"answers" : {
    "ch1" : "1 and 0",
    "ch2" : "1 and 2",
    "ch3" : "1 to 9"
},
"rightanswer" : "ch1" }                                                                            

There are n number of such entries in the DB quiz. Now I will have loop through the entire DB and print each value.

how can this can done in for loop; I'm just looking like question[i], answer.ch1[i], answer.ch2[i] .... how to retrieve?

var-dump Results:

array(4) { 
    ["_id"]=> object(MongoId)#7 (1) { ["$id"]=> string(24)"56bb13aef9f36fe751eecfe4" } 
    ["question"]=> string(32) What are the two binary numbers?" 
    ["answers"]=> array(3) { 
        ["ch1"]=> string(7) "1 and 0" 
        ["ch2"]=> string(7) "1 and 2" 
        ["ch3"]=> string(6) "1 to 9" 
    } 
    ["rightanswer"]=> string(3) "ch1" 
}
array(4) { 
    ["_id"]=> object(MongoId)#8 (1) { ["$id"]=> string(24) "56bb1714f9f36fe751eecfe5" } 
    ["question"]=> string(51) "It is a standard specifying a power saving feature?" 
    ["answers"]=> array(3) { .....
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
Puneeth
  • 419
  • 1
  • 6
  • 18
  • This is what I tried but its not working for (;;){links=array('$obj["question"]

         $obj["answer.ch1"]
         $obj["answer.ch2"]
         $obj["answer.ch3"]'); }
    – Puneeth Feb 10 '16 at 13:39
  • Please read http://stackoverflow.com/help/how-to-ask and try to expose in your question: which library you use to retrieve data from the database, what you expect from your code, and what is the actual result. – Alex Blex Feb 10 '16 at 14:28
  • @Alex thanks for your reply. The above code in a for loop lists all the questions using $obj["question"] and the multiple choices in a radio button $obj["answers.ch1"], $obj["answers.ch2"], and $obj["answers.ch3"]. Whats wrong with the code, why it is not working? when I print $obj["question"] alone it works fine. – Puneeth Feb 11 '16 at 06:28
  • there are several mongo-db php drivers, and even more libraries on top of them. All have slightly different syntax. In your question you provided document structure in the database only, which make it hard to guess how you retrieve data before you try to iterate over it. Please read again how to ask question, and add essential code **to the question** not in the comments. – Alex Blex Feb 11 '16 at 09:08
  • @Alex my another Post has proper code snippet, please answer there http://stackoverflow.com/questions/35334785/getting-random-records-and-display-in-particular-format-using-php-and-mongodb – Puneeth Feb 11 '16 at 09:32
  • The snippet is okay, but you should really add it to this question, rather than create a new one. There is a grey `edit` link below the question, right under the tagline. – Alex Blex Feb 11 '16 at 09:39

2 Answers2

0

I assume you will connect to collection, and in code collectionName is the name of table.

$connection = new MongoClient();
$collection = $connection->database->collectionName;

$cursor = $collection->find();
foreach ( $cursor as $id => $value )
{
    echo "$id: ";
    var_dump( $value );
//echo $value->question_id;
//echo $value->answers->ch1;

}

You can replace the code and print field name inside the loop the way you want.

Thanks Amit

Amit Shah
  • 1,380
  • 1
  • 10
  • 19
  • @Puneeth can you please tell me what do you get in var_dump or are you not able to make the connection to database. – Amit Shah Feb 10 '16 at 15:45
  • Var_dump is not serving my purpose, as i said above whats wrong with the code.? – Puneeth Feb 11 '16 at 06:34
  • @Puneeth do print_r or var_dump, and send me the data, i can see whats wrong there, actually after print_r/var_dumo you will come to know how you should print data. – Amit Shah Feb 11 '16 at 07:03
  • var-ump Results: array(4) { ["_id"]=> object(MongoId)#7 (1) { ["$id"]=> string(24) "56bb13aef9f36fe751eecfe4" } ["question"]=> string(32) What are the two binary numbers?" ["answers"]=> array(3) { ["ch1"]=> tring(7) "1 and 0" ["ch2"]=> string(7) "1 and 2" ["ch3"]=> string(6) "1 to 9" } "rightanswer"]=> string(3) "ch1" } : array(4) { ["_id"]=> object(MongoId)#8 (1) { ["$id"]=> string(24) "56bb1714f9f36fe751eecfe5" } ["question"]=> string(51) "It is a standard specifying a power saving feature?" ["answers"]=> array(3) { – Puneeth Feb 11 '16 at 07:14
0

Since you managed to print $obj["question"] with expected result, iteration over answers must be:

foreach($obj["answers"] as $key=>$answer) {
    echo "$key: $answer"; //or whatever format you need
}
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • Assume I don't have nested records. Now when I use the following code nothing is getting printed. Can you please let me know whats wrong with this particular code? foreach(...) {links=array('$obj["question"]

         $obj["ch1"]
         $obj["ch2"]
         $obj["ch3"]'); }
    – Puneeth Feb 11 '16 at 09:12
  • Everything. First of all you print nothing, just add 1 long string to `nks` array, secondly, learn difference between `'` and `"` http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php finally, read the answer you commented under - there is no `$obj["answer.ch3"]` but `$obj["answer"]["ch3"]` – Alex Blex Feb 11 '16 at 09:18