I am a newbie PHP devloper and i want to implement an online examination system. I want to implement a system which asks one question at a time i.e after answering 1st question click newxt then 2nd question will be displayed. how to implement this? do i have to implement through sessions? I am using loop but loop displays 20 questions one after another plzz help.
You could use sessions, yes. Using sessions would allow you to write all of your questions and everything in a single page, and then you could use a switch/case block to pump out the right question within the page. Use the post method on the form, and at the top of your page, check if the post is set, push the data into the correct mysql statement if so, and display the next question question (based on a session variable).
A very easy way to do this would be to implement a html structure that is the same for each questions. In php, at the beginning of the page, you just check for the post data, if there's none, then you load the first question ( in database, or hardcodded, as you wish ). If there's post data, you check if the question id is there, save the answer for the user, and load the next question.
It would look a bit like that:
<?php
function loadQuestion($id){
//your logic to load the question
return $question;
}
function saveAnswer($id,$answer){
//your logic to save the answer
}
if($_POST){
saveAnswer($_POST['id_question'],$_POST['answer'])
$data = loadQuestion($_POST['id_question']+1);
}
else $data = loadQuestion(1); //first question
?>
<form method="POST">
<input type="hidden" name="id_question" value="<?php echo $data["id_question"]; ?>" />
<div class="questionDiv">
<?php echo $data["question"]; ?>
</div>
answer:
<input type="text" value="" name="answer" />
<input type="submit" value="send" />
</form>