3

I am making quiz in php and have some problem. so i want to retrieve questions, answers and answer points from the database. for example i have 10 questions, when user marks all questions i want to count the points of marked radio buttons. I want do it dynamicaly depending on database values. i do not want to retrieve values of 10 radio buttons staticaly and sum it.

<div class="test-data">

<form method="POST" action="index.php">

<?php foreach($test_data as $id_question => $item): ?>

<div class="question" data-id="<?=$id_question?>" id="question-<?=$id_question?>">

<?php foreach($item as $point => $answer): ?>

<?php if(!$point): ?>
    <p class="q"><?=$answer?></p>
        <?php else: ?>
            <p class="a">
                <input type="radio" id="answer-<?=$id_answer?>" name="question-<?=$id_question?>" value="<?=$point?>" /><?=$answer?>
            </p>
<?php endif; ?>

<?php endforeach; ?>

</div>

<?php endforeach; ?>

            <input type="submit" name="finish_test" id="finish_test" value="Finish" />

</form>

</div>

<?php endif; ?>

this is my function which retrieves questions, answers and points

function get_test_data(){
$query = "SELECT q.question, q.quizz_id, a.id, a.answer, a.question_id, a.point
                    FROM questions q
                    LEFT JOIN answers a
                        ON q.id = a.question_id";
$res = mysql_query($query);
$data = null;
while($row = mysql_fetch_assoc($res)){
    if(!$row['question_id']) return false;
    $data[$row['question_id']]['0'] = $row['question'];
    $data[$row['question_id']][$row['point']] = $row['answer'];
}
return $data;                      

}

  • Please add details about what you want. Maybe a precise `use case` ? – Random May 19 '15 at 12:12
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 19 '15 at 12:12
  • I don't understand. What's the actual problem? – Strawberry May 19 '15 at 12:14
  • if i have 10 questions i have to create 10 variables and save values of this radio buttons. $question1 = $_POST['question1']; .. and so on. but i want to do it dynamicaly. make some array, push this 10 values into this array, and then count sum of this array. – Gio Nebieridze May 19 '15 at 12:25
  • დემე თუ მიხვდი ჩემს პრობლემას შეგიძლია სკაიპი ან ფბ მომცე და დამეხმარო? – Gio Nebieridze May 19 '15 at 12:26

0 Answers0