Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In addition to the ID/class disparity others have mentioned, there is no element with ID or class of "question", though there is a <code>&lt;p class="q"&gt;</code>.</p> <p>The PHP script is executed before the JS, so your loop to fill the JS arrays repeatedly fills them with the first values from the PHP arrays. The script that the browser executes is:</p> <pre><code> var Q = []; var op1 = []; var op2 = []; var op3 = []; var op4 = []; var ans = []; for(i=1;i&lt;=10;i++) { Q[i] = "[question 1, or possibly question 2]"; op1[i] = "[option 1-1 ...]"; op2[i] = "[option 1-2...]"; op3[i] = "[option 1-3...]"; op4[i] = "[option 1-4...]"; ans[i] = "[answer 1 ...]"; } </code></pre> <p>Instead, generate the array entirely in PHP:</p> <pre><code>var Q = [ &lt;?php echo implode(', ', array_map('json_encode', $Q)); ?&gt; ]; </code></pre> <p>Rather than a separate array for each question component, a single array of question objects makes it easier to keep questions and choices together:</p> <pre><code>&lt;?php // Fetching DB rows should be handled by a class or function that maps // DB rows to PHP objects, which is part of a data access layer, // rather than where you generate HTML output, which belongs to the // presentation layer. // For this example, we use example data rather than the result of an SQL query. $questionQry = array( array('Question 1?', 'Choice 1 A', 'Choice 1 B', 'Choice 1 C', 'Choice 1 D'), array('Question 2?', 'Choice 2 A', 'Choice 2 B', 'Choice 2 C', 'Choice 2 D'), array('Question 3?', 'Choice 3 A', 'Choice 3 B', 'Choice 3 C', 'Choice 3 D'), array('Question 4?', 'Choice 4 A', 'Choice 4 B', 'Choice 4 C', 'Choice 4 D') ); // encode data so it's suitable to be output as JS. // If you use PDO, you don't need to change this loop. foreach ($questionQry as $row) { $questions[] = json_encode(array( 'query' =&gt; $row[0], 'choices' =&gt; array_slice($row, 1) )); } ?&gt; var questions = [ &lt;?php echo implode(', ', $questions) ?&gt; ]; </code></pre> <p>Note this shows another advantage PDO has over the old mysql extension: query results can be looped over with <a href="http://php.net/foreach" rel="nofollow"><code>foreach</code></a>, just like arrays, since <a href="http://php.net/PDOStatement" rel="nofollow"><code>PDOStatement</code></a> implements <a href="http://php.net/Traversable" rel="nofollow"><code>Traversable</code></a>. Thus you can switch between arrays for examples and SQL results for production code with a minimum of code rewriting.</p> <p>The way your <a href="http://brainstormsandraves.com/articles/semantics/structure/" rel="nofollow">HTML is structured</a>, once you fix the other problems the user will see all the questions, then all the first possible answers, then the second, &amp;c. Instead, you could create an element that you can <a href="http://api.jquery.com/clone/" rel="nofollow">clone</a>, then fill with content. Think structurally; an Question is a Query and some Choices, which are a list (an ordered one, at that). The HTML should reflect this.</p> <pre><code>&lt;p id="QuestionTemplate" class="question" style="display: none"&gt; &lt;div class="query"&gt;&lt;/div&gt; &lt;ol class="choices"&gt; &lt;li&gt;&lt;input type="radio" value="0" /&gt;&lt;label&gt;&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="1" /&gt;&lt;label&gt;&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="2" /&gt;&lt;label&gt;&lt;/label&gt;&lt;/li&gt; &lt;li&gt;&lt;input type="radio" value="3" /&gt;&lt;label&gt;&lt;/label&gt;&lt;/li&gt; &lt;/ol&gt; &lt;/p&gt; </code></pre> <p>NB: slap a <code>.choices { list-style-type: upper-alpha; }</code> in your style sheet and answer choices are named A through D automatically.</p> <p>To fill out the question template:</p> <pre><code>function renderQuestion(qstn, qid) { var $qElt = $('#QuestionTemplate').clone(); $qElt.find('.query').attr('id', qid) .text(qstn.query); $qElt.find('.choices').children().each(function (i, item) { item.find('input').attr({name: qid, id: qid+'_'+i}); item.find('label').text(qstn.choices[i]) .attr('for', qid+'_'+i); } } $(function() { for (var i=0; i &lt; Q.length; ++i) { renderQuestion(Q[i], 'q'+i); } }); </code></pre> <p>Or create the questions from scratch:</p> <pre><code>function renderQuestion(qstn, qid) { var qElt = document.createElement('p'); qElt.id = 'Question_'+qid; var elt= document.createElement('div'); elt.appendChild(document.createTextNode(qstn.query)); elt.className="query"; qElt.appendChild(elt); elt = document.createElement('ol'); elt.className="choices"; for (var i=0; i &lt; qstn.choices.length; ++i) { var choice = document.createElement('li'); choice.appendChild(document.createElement('input')); choice.lastChild.type='radio'; choice.lastChild.name = qid; choice.lastChild.id = qid+'_'+i; choice.lastChild.value = i; choice.appendChild(document.createElement('label')); choice.lastChild.for = qid+'_'+i; choice.appendChild(document.createTextNode(qstn.choices[i])); elt.appendChild(choice); } qElt.appendChild(elt); document.appendChild(qElt); } </code></pre> <p>In either case, the correct answers shouldn't be output when generating the JS.</p> <p>Since you don't need JS to generate the questions, even better would be to generate the questions in PHP, which means you won't need to worry about what to do when JS isn't available.</p> <pre><code>&lt;?php // Fetching DB rows should be handled by a class or function that maps // DB rows to PHP objects, which is part of the data access layer, // rather than where you generate HTML output. // For this example, we just use example data. $questions = array( array('query' =&gt; 'Question 1?', 'A' =&gt; 'Choice 1 A', 'B' =&gt; 'Choice 1 B', 'C' =&gt; 'Choice 1 C', 'D' =&gt; 'Choice 1 D'), array('query' =&gt; 'Question 2?', 'A' =&gt; 'Choice 2 A', 'B' =&gt; 'Choice 2 B', 'C' =&gt; 'Choice 2 C', 'D' =&gt; 'Choice 2 D'), array('query' =&gt; 'Question 3?', 'A' =&gt; 'Choice 3 A', 'B' =&gt; 'Choice 3 B', 'C' =&gt; 'Choice 3 C', 'D' =&gt; 'Choice 3 D'), array('query' =&gt; 'Question 4?', 'A' =&gt; 'Choice 4 A', 'B' =&gt; 'Choice 4 B', 'C' =&gt; 'Choice 4 C', 'D' =&gt; 'Choice 4 D') ); ?&gt; &lt;?php foreach ($query as $i =&gt; $q): ?&gt; &lt;p id="Question_&lt;?php echo $i; ?&gt;" class="question"&gt; &lt;div class="query"&gt;&lt;?php echo array_shift($q); &lt;/div&gt; &lt;ol class="choices"&gt; &lt;?php foreach ($q as $j =&gt; $choice): ?&gt; &lt;li&gt; &lt;input type="radio" name="&lt;?php echo "q$i"; ?&gt;" id="&lt;?php echo "q${i}_${j}"; ?&gt;" value="&lt;?php echo $j; ?&gt;" /&gt; &lt;label for="&lt;?php echo "q${i}_${j}; ?&gt;"&gt;&lt;?php echo $choice; &lt;/label&gt; &lt;/li&gt; &lt;?php endforeach; ?&gt; &lt;/ol&gt; &lt;/p&gt; &lt;?php endforeach; ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload