2013-01-17 30 views
4

當用戶從問題下拉菜單中選擇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 
+5

你不是已經問這個問題的一個小時前?並且...嘗試複製更少的代碼和更重要的代碼片段,沒有人想要閱讀幾頁代碼來找到(例如)錯字。 –

+0

@ VladPreda它是類似的代碼,但不同的問題。我被告知要分開我的問題,所以我在這個問題的一個問題和這個問題上問了一個問題。我將減少代碼,我只是想讓你們都知道我的代碼的結構 – user1914374

+3

在Helios計算和工程學院,他們需要教你*如何調試和理解你編寫的代碼*。你正在解釋問題,就好像你是一個網站用戶(「我選擇」,「它顯示」),但不是編寫代碼的程序員。程序員必須能夠遵循程序邏輯並將實際結果與預期結果進行比較。就變量值和控制流而言,不是像「它不顯示」這樣的文學術語。 –

回答

0

foreach循環遍歷questions陣列,這是科幻隨着:

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

這意味着它是一個索引數組,而不是一個關聯數組;關鍵字是0和1.

但是,您隨後回顯$arrQuestionNo[$key]$arrQuestionContent[$key]。這些是關聯數組,其關鍵是學生ID,而不是從0開始的索引(除非您碰巧讓學生使用這些ID號)。

此外,您每次通過提取循環初始化$arrQuestionNo$arrQuestionContent爲空數組。所以當你最後迴應結果時,這些只包含最後一行提取的問題。

你應該使用一個多維數組:

while ($selectedstudentanswerstmt->fetch()) { 
    $questions[$detailsStudentId][$detailsQuestionNo] = $arrQuestionContent; 
} 

然後您的打印循環應該是:

foreach ($questions as $studentId => $studentQuestions) { 
    echo '<h2>Student '.htmlspecialchars($studentId).' Answers</h2>'. PHP_EOL; 
    foreach ($studentQuestion as $questionNo => $content) { 
    echo '<p><strong>Question:</strong> ' .htmlspecialchars($questionNo). ': ' .htmlspecialchars($content). '</p>' . PHP_EOL; 
    } 
} 
+0

'PHP中的一個數組實際上是一個有序的映射。「從技術上講,PHP中的任何數組都是關聯的,不是嗎? – Leri

+0

是的,它們在技術上是相同的,但是它們'_receptually_ different。 – Barmar

+0

@Barmar I假設我需要將'$ arrQuestionNo [$ detailsS​​tudentId] = $ arrQuestionNo'更改爲'$ arrQuestionNo [] = $ detailsQuestionNo;'。 '$ arrQuestionContent'也一樣? – user1914374

2

試試這個.......

$question = array(); 

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

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

EDITED

或者,如果你可以試試這個您的分組由問題:

$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 
    } 
} 

或者,如果您的分組通過用戶ID ...

$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'] 
    } 
} 
相關問題