其输出在php / html中的详细信息不正确 [英] Its outputs details incorrectly in php/html

查看:103
本文介绍了其输出在php / html中的详细信息不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户从问题下拉菜单中选择全部并在下面输出时,我想从下拉菜单中显示所有问题。问题在于它没有这样做,并且使情况变得更糟,它给了我未定义的抵消错误,指出:

 注意:Undefined抵消:......在第605行

第605行是:

  echo'< p>< strong>问题:< / strong> '.htmlspecialchars($ arrQuestionNo [$ key])。 ':'.htmlspecialchars($ arrQuestionContent [$ key])。 '< / p>'。 PHP_EOL; 

我的问题是如何解决错误并显示所有问题,如果用户选择所有选项?



我有一个演示可以通过:

按照以下步骤操作:



CODE:



问题下拉菜单:

 < select name =questionid = questionsDrop > 
< option value =0>全部< / option>
< option value = 23> 1< / option>
< option value = 32>> 1< / option>
< / select>

下面是根据从问题下拉菜单中选择哪些选项确定显示的代码。

  function StudentAnswers()
{

$ selectedstudentanswerqry =
SELECT $ b $ (学生姓名,学生姓名,学生姓名,q.SessionId,
问题编号,QuestionContent,o.OptionType,q.NoofAnswers,
GROUP_CONCAT(DISTINCT回答ORDER BY Answer SEPARATOR',')AS答案, r.ReplyType,QuestionMarks,
GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR',')AS StudentAnswer,ResponseTime,MouseClick,StudentMark
FROM Student st
INNER JOIN Student_Answer sa ON(st.StudentId (sa.StudentId = sr.StudentId)
INNER JOIN问题q ON(sa.QuestionId = q.QuestionId)
INNER JOIN回答一个ON q.QuestionId = an.QuestionId
LEFT JOIN回复r ON q.ReplyId = r.ReplyId
LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
;

//最初为空
$ where = array('q.SessionId =?');
$ parameters = array($ _ POST [session]);
$ parameterTypes ='i';


//检查是否选择了特定问题
$ p_question = empty($ _ POST [question])?'':$ _ POST [question];

switch($ p_question){
case 0:
// dont'add where filter
break;
default:
$ where [] ='q.QuestionId =?';
$ parameters []。= $ _POST [question];
$ parameterTypes。='i';
}

//如果我们在任何条件中添加了$,那么我们需要
中的WHERE子句//我们的查询
if(!empty( $ where)){
$ selectedstudentanswerqry。='WHERE'。 implode('AND',$ where);
global $ mysqli;
$ selectedstudentanswerstmt = $ mysqli-> prepare($ selectedstudentanswerqry);
//你只需要调用bind_param一次

if(count($ where)== 1){
$ selectedstudentanswerstmt-> bind_param($ parameterTypes,$ parameters [ 0]);
}
else if(count($ where)== 2){
$ selectedstudentanswerstmt-> bind_param($ parameterTypes,$ parameters [0],$ parameters [1]);
}


}

$ selectedstudentanswerqry。=
GROUP BY sa.StudentId,q.QuestionId
ORDER BY StudentAlias,q.SessionId,QuestionNo
;

//获得结果并赋值变量(前缀为db)
$ selectedstudentanswerstmt-> execute();
$ selectedstudentanswerstmt-> bind_result($ detailsS​​tudentId,$ detailsS​​tudentAlias,$ detailsS​​tudentForename,$ detailsS​​tudentSurname,$ detailsS​​essionId,$ detailsQuestionNo,
$ detailsQuestionContent,$ detailsOptionType,$ detailsNoofAnswers,$ detailsAnswer,$ detailsReplyType,$ detailsQuestionMarks ,$ detailsS​​tudentAnswer,$ detailsResponseTime,
$ detailsMouseClick,$ detailsS​​tudentMark);

$ selectedstudentanswerstmt-> store_result();
$ selectedstudentanswernum = $ selectedstudentanswerstmt-> num_rows();


$ question = array();

while($ selectedstudentanswerstmt-> fetch()){

$ arrQuestionNo = array();
$ arrQuestionContent = array();



$ arrQuestionNo [$ detailsS​​tudentId] = $ detailsQuestionNo;
$ arrQuestionContent [$ detailsS​​tudentId] = $ detailsQuestionContent;



$ questions [] = $ arrQuestionNo;
$ questions [] = $ arrQuestionContent;

}

$ selectedstudentanswerstmt-> close();

?>

.......................................... .................................................

< h2>学生的回答< / h2>

<?php

foreach($ questions as $ key => $ question){

echo'< p><强>问题:其中/强> '.htmlspecialchars($ arrQuestionNo [$ key])。 ':'.htmlspecialchars($ arrQuestionContent [$ key])。 '< / p>'。 PHP_EOL;

}
}
?>

更新:

学生表结构:

pre $ CREATE TABLE`Student`(
`StudentId` int(10)NOT NULL AUTO_INCREMENT,
`StudentForename` varchar(25)NOT NULL,
`StudentSurname` varchar(25)NOT NULL,
`StudentAlias` varchar(15)NOT NULL,$ b $``StudentEmail varchar(50)NOT NULL,
`StudentUsername` varchar(20)NOT NULL,
`StudentPassword` varchar(50)NOT NULL,
`StudentDOB` date NOT NULL,
`Year` int(2)NOT NULL,
`CourseId` int(6)NOT NULL,
`Active` tinyint(1)NOT NULL DEFAULT'1',
PRIMARY KEY `StudentId`),
KEY`FK_Course`(`CourseId`)
)ENGINE = InnoDB AUTO_INCREMENT = 41 DEFAULT CHARSET = utf8

问题表结构:

  CREATE TABLE`问题`(
`QuestionId` int(10)NOT NULL AUTO_INCREMENT,
`SessionId` int(10)NOT NULL,
`QuestionNo` int(3)NOT NULL,
`Ques (2)NOT NULL,
`ReplyId` int(1)NOT NULL,
`QuestionMarks` int(4)NOT NULL(1)NOT NULL,
`NotofAnswers` int ,
`OptionId` int(2)NOT NULL,
PRIMARY KEY(`QuestionId`)
)ENGINE = InnoDB AUTO_INCREMENT = 357 DEFAULT CHARSET = utf8


  $ question = array(); 

while($ selectedstudentanswerstmt-> fetch()){
//假设您不需要StudentId
$ questions [] = array('no'=> ; $ detailsQuestionNo,
'content'=> $ detailsQuestionContent);
}

  foreach($ questions as $ key => $ question){
echo'< p>< strong>问题:< / strong> '。
htmlspecialchars($ question ['no'])。
':'。
htmlspecialchars($ question ['content'])。
'< / p>'。
PHP_EOL;
}



EDITED



或者你可以尝试这个,如果你按照问题分组:

  $ question = array(); 

while($ selectedstudentanswerstmt-> fetch()){
if(true === isset($ questions [$ detailsQuestionId])){
$ questions [$ detailsQuestionId ] ['students'] [] = $ detailsS​​tudentId;
} else {
$ questions [$ detailsQuestionId] = array();
$ questions [$ detailsQuestionId] ['no'] = $ arrQuestionNo;
$ questions [$ detailsQuestionId] ['content'] = $ arrQuestionContent;
$ questions [$ detailsQuestionId] ['students'] = array();
$ questions [$ detailsQuestionId] ['students'] [] = $ detailsS​​tudentId;



foreach($ questions as $ questionId => $ question){
// $ question ['no']
/ / $ question ['content']

foreach($ question ['students'] AS $ key => $ studentId){
// $ studentId
}

$ / code>



或者,如果您按用户ID分组... ...



  $ students = array(); 
$ b $ while($ selectedstudentanswerstmt-> fetch()){
if(false === isset($ students [$ detailsS​​tudentId])){
$ students [$ detailsS​​tudentId ] = array();
}
$ students [$ detailsS​​tudentId] [$ detailsQuestionId] =
array('no'=> $ arrQuestionNo,
'content'=> $ arrQuestionContent;
}

foreach($ students AS $ studentId => $ questions){
// $ studentId
foreach($ questions AS $ questionId => $ question) {
// $ questionId
// $问题['no']
// $问题['content']
}
}


I want to display all the questions from the drop down menu when the user selects All from the question drop down menu and output it underneath. Problem is that it is not doing this and to make it worse, it is giving me undefined offset errors stating:

Notice: Undefined offset: ... in .... on line 605

Line 605 is:

echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[$key]). ': ' .htmlspecialchars($arrQuestionContent[$key]). '</p>' . PHP_EOL;

My question is how to fix the error and display all the questions if user selects All option?

I have a demo you can go through: DEMO

Follow steps below:

  • In Module drop down menu, select System Stratergy and submit
  • When Assessment drop down menu appears, select POKUB1 and submit
  • You will see students and question drop down menu. You can see that if you open up the drop down menus that there are 3 students and 2 questions. Please select a single student and All questions and ubmit. This is where you will see the errors when really I want to display all of the question's details here.

CODE:

Question Drop down menu:

<select name="question" id="questionsDrop">
<option value="0">All</option>
<option value=23">1</option>
<option value=32">1</option>
</select>

Below is code which determines display depending on which options are selected from question drop down menu.

    function StudentAnswers()
        {

        $selectedstudentanswerqry = "
            SELECT
            sa.StudentId, StudentAlias, StudentForename, StudentSurname, q.SessionId, 
            QuestionNo, QuestionContent, o.OptionType, q.NoofAnswers, 
            GROUP_CONCAT( DISTINCT Answer ORDER BY Answer SEPARATOR ',' ) AS Answer, r.ReplyType, QuestionMarks, 
            GROUP_CONCAT(DISTINCT StudentAnswer ORDER BY StudentAnswer SEPARATOR ',') AS StudentAnswer, ResponseTime, MouseClick, StudentMark
            FROM Student st
            INNER JOIN Student_Answer sa ON (st.StudentId = sa.StudentId)
            INNER JOIN Student_Response sr ON (sa.StudentId = sr.StudentId)
            INNER JOIN Question q ON (sa.QuestionId = q.QuestionId)
            INNER JOIN Answer an ON q.QuestionId = an.QuestionId
            LEFT JOIN Reply r ON q.ReplyId = r.ReplyId
            LEFT JOIN Option_Table o ON q.OptionId = o.OptionId
            ";

            // Initially empty
            $where = array('q.SessionId = ?');
            $parameters = array($_POST["session"]);
            $parameterTypes = 'i';


            // Check whether a specific question was selected
            $p_question = empty($_POST["question"])?'':$_POST["question"];

            switch($p_question){
            case 0:
                //dont' add where filters
                break;
            default:
                $where[] = 'q.QuestionId = ?';
                $parameters[] .= $_POST["question"];
                $parameterTypes .= 'i';
            }

            // If we added to $where in any of the conditionals, we need a WHERE clause in
            // our query
            if(!empty($where)) {
                $selectedstudentanswerqry .= ' WHERE ' . implode(' AND ', $where);
                global $mysqli;
                $selectedstudentanswerstmt=$mysqli->prepare($selectedstudentanswerqry);
                // You only need to call bind_param once

                if (count($where) == 1) {
                $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0]);
            }
            else if (count($where) == 2) {
                $selectedstudentanswerstmt->bind_param($parameterTypes, $parameters[0], $parameters[1]);
            }


            }

            $selectedstudentanswerqry .= "
              GROUP BY sa.StudentId, q.QuestionId
              ORDER BY StudentAlias, q.SessionId, QuestionNo
            ";

        // get result and assign variables (prefix with db)
        $selectedstudentanswerstmt->execute(); 
        $selectedstudentanswerstmt->bind_result($detailsStudentId,$detailsStudentAlias,$detailsStudentForename,$detailsStudentSurname,$detailsSessionId,$detailsQuestionNo, 
        $detailsQuestionContent,$detailsOptionType,$detailsNoofAnswers,$detailsAnswer,$detailsReplyType,$detailsQuestionMarks,$detailsStudentAnswer,$detailsResponseTime,
        $detailsMouseClick,$detailsStudentMark);    

        $selectedstudentanswerstmt->store_result();
        $selectedstudentanswernum = $selectedstudentanswerstmt->num_rows(); 


            $question = array();

            while ($selectedstudentanswerstmt->fetch()) {

            $arrQuestionNo = array();
            $arrQuestionContent = array();



            $arrQuestionNo[ $detailsStudentId ] = $detailsQuestionNo;
            $arrQuestionContent[ $detailsStudentId ] = $detailsQuestionContent;



            $questions[] = $arrQuestionNo;
            $questions[] = $arrQuestionContent;

        }

        $selectedstudentanswerstmt->close();

        ?>

...........................................................................................

    <h2>STUDENT'S ANSWERS</h2>

    <?php   

              foreach ($questions as $key=>$question) {

    echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[$key]). ': ' .htmlspecialchars($arrQuestionContent[$key]). '</p>' . PHP_EOL;

    }
    }
    ?>

UPDATE:

Student Table structure:

CREATE TABLE `Student` (
 `StudentId` int(10) NOT NULL AUTO_INCREMENT,
 `StudentForename` varchar(25) NOT NULL,
 `StudentSurname` varchar(25) NOT NULL,
 `StudentAlias` varchar(15) NOT NULL,
 `StudentEmail` varchar(50) NOT NULL,
 `StudentUsername` varchar(20) NOT NULL,
 `StudentPassword` varchar(50) NOT NULL,
 `StudentDOB` date NOT NULL,
 `Year` int(2) NOT NULL,
 `CourseId` int(6) NOT NULL,
 `Active` tinyint(1) NOT NULL DEFAULT '1',
 PRIMARY KEY (`StudentId`),
 KEY `FK_Course` (`CourseId`)
) ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8

Question Table Structure:

CREATE TABLE `Question` (
 `QuestionId` int(10) NOT NULL AUTO_INCREMENT,
 `SessionId` int(10) NOT NULL,
 `QuestionNo` int(3) NOT NULL,
 `QuestionContent` varchar(5000) NOT NULL,
 `NoofAnswers` int(2) NOT NULL,
 `ReplyId` int(1) NOT NULL,
 `QuestionMarks` int(4) NOT NULL,
 `OptionId` int(2) NOT NULL,
 PRIMARY KEY (`QuestionId`)
) ENGINE=InnoDB AUTO_INCREMENT=357 DEFAULT CHARSET=utf8

解决方案

Give this a try.......

$question = array();

while ($selectedstudentanswerstmt->fetch()) {
    // assuming you don't need the StudentId
    $questions[] = array('no' => $detailsQuestionNo,
                         'content' => $detailsQuestionContent);
}

and

foreach ($questions as $key => $question) {
    echo '<p><strong>Question:</strong> ' . 
         htmlspecialchars($question['no']) .
         ': ' . 
         htmlspecialchars($question['content']) . 
         '</p>' . 
         PHP_EOL;
}

EDITED

Or you can try this if your grouping by question:

$question = array();

while ($selectedstudentanswerstmt->fetch()) {
    if (true === isset($questions[$detailsQuestionId])) {
        $questions[$detailsQuestionId]['students'][] = $detailsStudentId;
    } else {
        $questions[$detailsQuestionId] = array();
        $questions[$detailsQuestionId]['no'] = $arrQuestionNo;
        $questions[$detailsQuestionId]['content'] = $arrQuestionContent;
        $questions[$detailsQuestionId]['students'] = array();
        $questions[$detailsQuestionId]['students'][] = $detailsStudentId;
    }
}

foreach ($questions as $questionId => $question) {
    // $question['no']
    // $question['content']

    foreach($question['students'] AS $key => $studentId) {
        // $studentId
    }
}

Or if your grouping by userID...

$students = array();

while ($selectedstudentanswerstmt->fetch()) {
    if (false === isset($students[$detailsStudentId])) {
        $students[$detailsStudentId] = array();
    }
    $students[$detailsStudentId][$detailsQuestionId] = 
                                       array('no' => $arrQuestionNo,
                                             'content' => $arrQuestionContent;
}

foreach ($students AS $studentId => $questions) {
    // $studentId
    foreach ($questions AS $questionId => $question) {
        // $questionId
        // $question['no']
        // $question['content']
    }
}

这篇关于其输出在php / html中的详细信息不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆