会话数据问题编号的值没有增加并在 codeigniter 项目中传递

Posted

技术标签:

【中文标题】会话数据问题编号的值没有增加并在 codeigniter 项目中传递【英文标题】:value for session data question number not getting incremented and passed in codeigniter project 【发布时间】:2020-09-05 14:44:36 【问题描述】:

请建议我作为会话数据 question_number 的值没有增加并在 Codeigniter 项目中传递 1 的旧值 question_number 始终作为问题编号传递 代码最初应该将question_number设置为1,然后当用户第一次提交表单时,表单数据由同一个文档处理,问题编号应该加一并传递给下面的同一个表单 但不幸的是,这个新的问题编号并没有传递给隐藏的输入类型 看下面的代码

$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbName);

                                                if (!$this->session->userdata("user_id")) 
                                                    header("location:" . base_url() . "login");
                                                
                                                $_SESSION['error'] = '';
                                                $_SESSION['success'] = '';

                                                // template methods start here ==========================================

                                                if (isset($_POST["LaunchTest"])) 

                                                        $test_id = addslashes($_POST['id']);
                                                        echo("test_id: " . $test_id . "<br />");
                                                        $session_id = addslashes($_POST['session_id']);
                                                        echo("session_id: " . $session_id . "<br />");
                                                        $counter = addslashes($_POST['counter']);
                                                        echo("counter: " . $counter . "<br />");
                                                        $score = addslashes($_POST['score']);
                                                        echo("score: " . $score . "<br />");
                                                        $question_number = addslashes($_POST['question_number']);
                                                        echo("question_number: " . $question_number . "<br />");
                                                        $answer = addslashes($_POST['hidden_answer']);
                                                        echo("answer: " . $answer . "<br />");
                                                        $user_choice = addslashes($_POST['radio_choice']);
                                                        echo("user_choice: " . $user_choice . "<br />");
                                                        $start_id = addslashes($_POST['start_id']);
                                                        echo("start_id: " . $start_id . "<br />");
                                                        $end_id = addslashes($_POST['end_id']);
                                                        echo("end_id: " . $end_id . "<br />");
                                                        $current_question= $question_number;
                                                        if ($user_choice==$answer) 
                                                            $score++;
                                                        
                                                        $preventRepeat = [];
                                                        $question_number = $question_number+1;
                                                        echo("new Question number: " . $question_number . "<br />");
                                                        $new_question = $question_number;
                                                        $_SESSION['question_number'] = $question_number;

                                                        $organisation = $_SESSION['organisation'];
                                                        date_default_timezone_set('Africa/Lagos');
                                                        $today = date('m/d/Y h:i:s a', time());

                                                        $sql_query = "UPDATE cbt_attempts_tbl SET "
                                                        . "currentQuestion ='"
                                                        . $current_question . "',"
                                                        . "score ='"
                                                        . $score . "',"
                                                        . "created_date ='"
                                                        . $today . "'"
                                                        . " WHERE session_id = '"
                                                        . $session_id .
                                                        "'";
                                                        ;   //$displayedRowCount = $rowCount;
                                                        echo $sql_query . "<br />";
                                                        $result = mysqli_query($con, $sql_query) or die("No data returned from DB Query");
                                                        //$candidate_quest_count++;
                                                        echo "database updated succesfully. <br />";

                                                        $current_question++;


                                                        if ($question_number > $_SESSION['total_questions']) 
                                                            //calculate score
                                                            $score_output = calculateScore($score, $_SESSION['total_questions']);
                                                            //end test
                                                            header("location:" . base_url() . "score?id=" . $test_id .
                                                        "&test_session=" . $session_id. "score=". $score_output);
                                                         else 
                                                            //            myRandomNum = generateSecondRandomNumber();
                                                            $myRandomNum = generateRandomNumber($start_id, $end_id);
                                                            echo("myRandomNum: " . $myRandomNum . "<br />");

                                                            //$myRandomNum = preventRepeatingQuestions($myRandomNum);
                                                            $quesionAtRand_str = getQuestionAtRandom($myRandomNum, $con);

                                                             $QuestData_arr = explode("~", $quesionAtRand_str);
                                                            //echo("QuestData_arr is: " . $QuestData_arr . "<br />");

                                                            $Question = $QuestData_arr[1];
                                                            //echo("Question is: " . $Question . "<br />");

                                                            $OptA = $QuestData_arr[2];
                                                            //echo("OptA is: " . $OptA . "<br />");

                                                            $OptB = $QuestData_arr[3];
                                                            //echo("OptB is: " . $OptB . "<br />");

                                                            $OptC = $QuestData_arr[4];
                                                            //echo("OptC is: " . $OptC . "<br />");

                                                            $OptD = $QuestData_arr[5];
                                                            //echo("OptD is: " . $OptD . "<br />");

                                                            $Answer = $QuestData_arr[6];
                                                            //echo("Answer is: " . $Answer . "<br />");
                                                        

                                                

                                                ?>


                                                <?php include('admin_block.php'); ?>
                                                <br/>

                                                <?php
                                                if (!isset($_GET["id"])) 
                                                    $_SESSION['error'] = '<label class="text-danger">nothing selected.</label><br />';
                                                    header("location:" . base_url() . "launchtest");
                                                    exit();
                                                
                                                $test_id = addslashes($_GET['id']);
                                                //display deleted succesfully
                                                //header return to update screen
                                                $organisation = $_SESSION['organisation'];
                                                $user_name = $_SESSION['user_name'];
                                                // main start
                                                //-----FIRST TIME RUN STARTS HERE---

                                                if (!isset($test_session_id)) 
                                                            $arr_usr_choice = [];
                                                            $arr_correct_answer = [];
                                                            $arr_usr_choice[0] = "nothing";
                                                            $arr_correct_answer[0] = "nothing";
                                                            $candidate_quest_count = 1;
                                                            $question_number = 1;
                                                            $preventRepeat = [];
                                                            $counter = 1;
                                                            $current_question = 1;
                                                            $score = 0;
                                                            $_SESSION["score"] = 0;
                                                            $test_session_id = generateTestSessionId();
                                                            //echo("test_session_id is: " . $test_session_id . "<br />");

                                                            //gettestdetails from summary table
                                                            $testDetails = getTestDetailsFromSummary($con, $test_id);
                                                            //echo("testDetails: " . $testDetails . "<br />");

                                                            $dataFromSmryTble_arr = explode("~", $testDetails);
                                                            $test_name= $dataFromSmryTble_arr[2];
                                                            //echo("test_name: " . $test_name . "<br />");

                                                            $type = $dataFromSmryTble_arr[6];
                                                            //echo("type: " . $type . "<br />");

                                                            $subject = $dataFromSmryTble_arr[5];
                                                            //echo("subject: " . $subject . "<br />");

                                                            $total_questions = $dataFromSmryTble_arr[3];
                                                            //echo("total_questions: " . $total_questions . "<br />");

                                                            $start_id = $dataFromSmryTble_arr[0];
                                                            //echo("start_id: " . $start_id . "<br />");

                                                            $end_id = $dataFromSmryTble_arr[1];
                                                            //echo("end_id: " . $end_id . "<br />");
                                                            $_SESSION['total_questions'] = $total_questions;
                                                            loadDetailsToAttemptsTbl(
                                                                $con,
                                                                $test_id,
                                                                $test_session_id,
                                                                $test_name,
                                                                $type,
                                                                $subject,
                                                                $total_questions,
                                                                $start_id,
                                                                $end_id
                                                            );


                                                        //1. get test takers first name and lastname

                                                        if (!isset($_SESSION['firstname']) || !isset($_SESSION['lastname'])) 
                                                            header("location:" . base_url() . "enter_student_details?id=" . $test_id .
                                                                "&test_session=" . $test_session_id);
                                                        
                                                        $firstname = $_SESSION['firstname'];
                                                        $lastname = $_SESSION['lastname'];

                                                        $output = displayFirstQuestion($con, $test_session_id); // duisplays one Question
                                                            //echo("output is: " . $output . "<br />");

                                                            $QuestData_arr = explode("~", $output);
                                                            //echo("output is: " . $output . "<br />");

                                                            $Question = $QuestData_arr[1];
                                                            //echo("Question is: " . $Question . "<br />");

                                                            $OptA = $QuestData_arr[2];
                                                            //echo("OptA is: " . $OptA . "<br />");

                                                            $OptB = $QuestData_arr[3];
                                                            //echo("OptB is: " . $OptB . "<br />");

                                                            $OptC = $QuestData_arr[4];
                                                            //echo("OptC is: " . $OptC . "<br />");

                                                            $OptD = $QuestData_arr[5];
                                                            //echo("OptD is: " . $OptD . "<br />");

                                                            $Answer = $QuestData_arr[6];
                                                            //echo("Answer is: " . $Answer . "<br />");
                                                            $_SESSION['question_number'] = $question_number;

                                                
                                                //-----FIRST TIME RUN ENDS HERE---
                                                echo '<form method="post" action="'. $_SERVER['PHP_SELF'].'?id=' . $test_id .
                                                        '&test_session=' . $test_session_id .'"';
                                                echo '<h2 class="section-heading animated">'. $test_name .'</h2>';


                                                echo '<TABLE class="my_style" border="0"><TR><TD>' . $firstname . " ". $lastname . '</TD></TR></TABLE>';


                                                echo '<font color="#fff"><table class="my_style" border="0" cellpadding="2">';
                                                echo '<tr >';
                                                echo '<td valign="top" >' . $_SESSION["question_number"]  . '</td>';
                                                echo '<td valign="top" colspan="3" scope="col"><b>' . $Question . '</b></td>';
                                                echo '</tr >';
                                                echo '<tr >';
                                                echo '<td></td><TD WIDTH="2%">A.</TD><td WIDTH="2%"><input name="radio_choice" type="radio" value="A" required /></td><td valign="top" >' . $OptA . '</td>';
                                                 echo '</tr >';
                                                 echo '<tr >';
                                                 echo '<td></td><TD WIDTH="2%">B.</TD><td WIDTH="2%"><input name="radio_choice" type="radio" value="B" /></td><td valign="top" >' . $OptB . '</td>';
                                                 echo '</tr >';
                                                 echo '<tr >';
                                                 echo '<td></td><TD WIDTH="2%">C.</TD><td WIDTH="2%"><input name="radio_choice" type="radio" value="C" /><td valign="top"  >' . $OptC . '</td>';
                                                 echo '</tr >';
                                                 echo '<tr >';
                                                 echo '<td></td><TD WIDTH="2%">D.</TD><td WIDTH="2%"><input name="radio_choice" type="radio" value="D" /><td valign="top" >' . $OptD. '</td>  <input type="hidden"  value="'.$Answer.'" name="hidden_answer" /> ';
                                                echo '</tr>';
                                                 echo '</tr >';
                                                echo '<td colspan="4" ><input type="submit" style="background-color: #008CBA;" value="Next Question" name ="LaunchTest" onClick="checkRadios()"/>';
                                                  echo '<input  type="hidden" name="id" value="'. $test_id.'"/>';
                                                echo '<input  type="hidden" name="question_number" value="'. $_SESSION["question_number"] .'"/>';
                                                echo '<input  type="hidden" name="session_id" value="'.$test_session_id.'"/>';
                                                echo '<input  type="hidden" name="counter" value="'.$counter.'"/>';
                                                echo '<input  type="hidden" name="score" value="'.$_SESSION["score"] .'"/>';
                                                echo '<input  type="hidden" name="start_id" value="'.$start_id.'"/>';
                                                echo '<input  type="hidden" name="end_id" value="'.$end_id.'"/>';
                                                echo '</td>';
                                                echo '</tr >';

                                                echo '</table></font>';
                                                echo '</form>';
                                                // main end
                                                ?> 

【问题讨论】:

1.) 这看起来不像 codeigniter - 它只是用 PHP 编写的代码; 2.)您的格式有点“混乱”,而且您的问题很不清楚恕我直言 - 请澄清问题。 请将视图和控制器代码分开。这就是使用框架背后的全部想法。 粗略一看,我可能漏掉了一些东西:你的代码中的$test_session_id 是什么?如果未设置,$question_number 将始终重置为 1。另外,请格式化问题,这很难理解。 我很尴尬。非常感谢大家的 cmets,@Ynhockey 是对的。 test_session_id 导致了问题。我还将视图与控制器分开 【参考方案1】:

@Ynhockey 是对的。 test_session_id 是导致问题的原因

【讨论】:

以上是关于会话数据问题编号的值没有增加并在 codeigniter 项目中传递的主要内容,如果未能解决你的问题,请参考以下文章

PHP 会话数据未保存

Cookies vs 会话 vs 数据库 [关闭]

控制台命令上的PHP会话丢失

如何存储用户的会话数据

UDP编号的段?

如何将数据持久化(转储)到本地存储并在以后的会话中加载?