當用戶從問題下拉菜單中選擇All
並在下面輸出時,我想從下拉菜單中顯示所有問題。問題是,它是不是這樣做的,使情況變得更糟,它給我不確定的偏移誤差說明:其在php/html中的輸出詳細信息不正確
Notice: Undefined offset: ... in .... on line 605
605線是:
echo '<p><strong>Question:</strong> ' .htmlspecialchars($arrQuestionNo[$key]). ': ' .htmlspecialchars($arrQuestionContent[$key]). '</p>' . PHP_EOL;
我的問題是如何解決的錯誤和如果用戶選擇All
選項,則顯示所有問題?
我有一個demo你可以通過:DEMO
按照以下步驟進行:
- 在模塊下拉菜單,選擇系統Stratergy並提交
- 當評估下拉菜單出現時,選擇POKUB1並提交
- 你會看到學生和問題下拉菜單。你可以看到,如果你打開有3個學生和2個問題的下拉菜單。請選擇一個學生和
All
問題和ubmit。當你真的想在這裏顯示所有問題的詳細信息時,這是你會看到錯誤的地方。
CODE:
問題下拉菜單:
<select name="question" id="questionsDrop">
<option value="0">All</option>
<option value=23">1</option>
<option value=32">1</option>
</select>
下面是顯示根據哪個選項從所選的問題下拉菜單,它確定碼。
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:
學生表結構:
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
問表結構:
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
你不是已經問這個問題的一個小時前?並且...嘗試複製更少的代碼和更重要的代碼片段,沒有人想要閱讀幾頁代碼來找到(例如)錯字。 –
@ VladPreda它是類似的代碼,但不同的問題。我被告知要分開我的問題,所以我在這個問題的一個問題和這個問題上問了一個問題。我將減少代碼,我只是想讓你們都知道我的代碼的結構 – user1914374
在Helios計算和工程學院,他們需要教你*如何調試和理解你編寫的代碼*。你正在解釋問題,就好像你是一個網站用戶(「我選擇」,「它顯示」),但不是編寫代碼的程序員。程序員必須能夠遵循程序邏輯並將實際結果與預期結果進行比較。就變量值和控制流而言,不是像「它不顯示」這樣的文學術語。 –