1

I have a DB with 100 questions and multiple answers for it. I wanted to display in a quiz format selecting 20 random questions at a time. I have done something like this below

$m=new MongoClient(); 
  $db=$m->mydb; 
  $c=$db->quiz; 
 $cursor = $c->find()
      $n = 20;
            foreach ($cursor as $obj) { 
             $links=array('$obj["question"]<br><br> &nbsp;&nbsp;&nbsp;&nbsp;
                                                    <input type="radio" name="q1" value="$obj["ch1"]"> $obj["ch1"]<br>&nbsp;&nbsp;&nbsp;&nbsp;
                                                    <input type="radio" name="q1" value="$obj["ch2"]"> $obj["ch2"]<br>&nbsp;&nbsp;&nbsp;&nbsp; 
                                                  <input type="radio" name="q1" value="$obj[ch3"]"> $obj["ch3"]</p>');

    $rand_keys = array_rand($links, $n);
    echo "<center>". "<br><table><tr><td>";

    echo "1.&nbsp;&nbsp;". $links[$rand_keys[0]] . "<br>";
    echo "</td></tr><tr><td>";
    echo "2.&nbsp;&nbsp;".$links[$rand_keys[1]] . "<br>";
    echo "</td></tr><tr><td>";
    echo "3.&nbsp;&nbsp;". $links[$rand_keys[2]] . "<br>";
    echo "</td></tr><tr><td>";


    }

It is not working. Is there anything wrong with th code? Please help me either finding the wrong to make it work or any method that would achieve my purpose.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
Puneeth
  • 419
  • 1
  • 6
  • 18
  • 1
    Possible duplicate of [Iteration using mongoDB and PHP](http://stackoverflow.com/questions/35314980/iteration-using-mongodb-and-php) – Alex Blex Feb 11 '16 at 09:10
  • @AlexBlex I'm not sure it's a duplicate. – Constantin Guay Feb 11 '16 at 09:13
  • @Puneeth, which version of MongoDB do you use? – Constantin Guay Feb 11 '16 at 09:13
  • @ConstantinGuay, 2.6.11. – Puneeth Feb 11 '16 at 09:17
  • @Alex, Its not duplicate, Iteration was to know how to access nested loops in PHP and MongoDB. Now how to display in particular format , where the code is not working here. – Puneeth Feb 11 '16 at 09:19
  • 1
    @Puneeth ok so you can't use MongoDB 3.2's new feature [$sample](https://docs.mongodb.org/manual/reference/operator/aggregation/sample/). On way to get random values from the database, without PHP, could be to add random entries. You add a field `'random' => rand(1,10000)` (depending on how many docs you'll have) and then, in your query, you ask for a random value too. – Constantin Guay Feb 11 '16 at 09:21
  • @ConstantinGuay, As I am naive in PHP and MongoDB I may struggle to understand. I need to display in quiz format taking the questions and multiple answers from DB. I mean for this I need PHP code, so whats your point? – Puneeth Feb 11 '16 at 09:25
  • @Puneeth yes, you need PHP to format the code, but not to get random values. That's why I do not add this as an answer, it's only a partial answer to your problem. – Constantin Guay Feb 11 '16 at 09:26

1 Answers1

2

OK, considering other non-duplicated questions, I would recommend to learn basics of php alone and try to iterate a hardcoded nested array to print expected output.

So some problems with the code in the question:

  • it does not fetch random questions
  • it prints nothing but adds 1 long string to links array. Please learn difference between ' and "
  • there is no loop to iterate answers, and it is not valid php code at all

The working code (again considering document structure from non-duplicated question) may look like:

foreach ($cursor as $obj) { 
    echo $obj["question"];
    foreach($obj["answers"] as $key=>$answer) {
        echo '<input type="radio" name="' . $key . '" value="'.$answer.'">';
    }
}
Community
  • 1
  • 1
Alex Blex
  • 34,704
  • 7
  • 48
  • 75
  • I have edited the code which prints randomly. Now can you give me some useful answers? Please remember this work without the for loop. What can be done to make it work. – Puneeth Feb 11 '16 at 09:44
  • 1
    No Puneeth. Evidently I cannot. I give up. – Alex Blex Feb 11 '16 at 10:02
  • If so then why isn't this working. echo ''; Please remember no nested records, so accessing obj[ch1]. – Puneeth Feb 11 '16 at 11:04