简体   繁体   中英

Storing online exam results in sessions in PHP

I'm working on online free examination website. Using PHP, jQuery & session I'm displaying questions with choices on front end. If user clicks any choice among displayed choices (radio buttons with same name for all choices as obvious) I'm storing selected question number & choice (as answer1/answer2 up to answer2) in session variable, adding current quesno & choice to previous one.

The problem is if I want to change the answer which I'vE already answered, it's just appending as new result instead of updating.

I've tried to loop through that session variable but stuck. The following is the data format I'm storing into session variable.

if( $_POST['qno'] && $_POST['choice'] ){
    $ques_no = $_POST['qno'];
    $selected_choice = $_POST['choice'];// choices in radio buttons with name choice


    if( isset($_SESSION['result']) ){

        //here i'm trying to loop though $_SESSION['result'] to check the below format
 >> ','.$ques_no.',' << in session so that it can be changed, i stuck here.


        $_SESSION['result']=$_SESSION['result'].','.$ques_no.','.$selected_choice.'|';
    }else{
        $_SESSION['result'] = ','.$ques_no.','.$selected_choice.'|';
    }
}

If this procedure is OK to follow please tell me the process of loop through and check whether that pattern existed in session or not; if it is wrong procedure please guide me.

The easiest way to accomplish this is to make $_SESSION['result'] an array rather than a string. Then you don't have to worry about if statements at all, except to make sure $_SESSION['result'] exists, you just assign the value in a single step and you're done:

if (!isset($_SESSION['result'])) $_SESSION['result'] = array();
$_SESSION['result'][$ques_no] = $selected_choice;

Just make sure you edit your other code to interpret $_SESSION['result'] as an array, or else you'll get some unexpected results.

I prefer you buy available online scripts in market as starting point for it. This will save you time, cost and testing efforts.

Below is one of the fine scripts that I worked it and it worked like charm. Using this as base I developed a online testing portal of over 1000 users using computer adaptive test.

http://codecanyon.net/item/online-skills-assessment/9379895

It is a good starting point for people looking to develop Online Exam System.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM