2015-02-07 147 views
2

我在沒有正確答案的情況下進行測驗,相反,我們將比較和計算個性以創建匹配算法。到目前爲止,我有:PHP:從無線電盒獲取價值

enter image description here

我有3個表,涉及的測驗:問題,question_answers,question_choices:

questions: 
+-------------+---------------+ 
| question_id | question  | 
+-------------+---------------+ 
| 1   | Is your... | 
| 2   | Do you..  | 
| 3   | Have you... | 
|_____________|_______________| 

question_choices: 
+-------------+-------------+-------------+ 
| choice_id | question_id | choice_text | 
+-------------+-------------+-------------+ 
| 1   | 1   | Somewhere.. | 
| 2   | 1   | Somewhere.. | 
| 3   | 1   | Somewhere.. | 
| 4   | 2   | Yes   | 
| 5   | 2   | No   | 
| 6   | 3   | Yes   | 
| 7   | 3   | No   | 
|_____________|_____________|_____________| 

question_answers: 
+------------+---------+------------+------------+ 
| answer_id | user_id |question_id | choice_id | 
+------------+---------+------------+------------+ 
| 1   | 125  | 1   | 2   | 
| 2   | 3  | 1   | 3   | --> 125 & 3 = No Match 
| 3   | 125  | 2   | 1   | 
| 4   | 3  | 2   | 1   | --> 125 & 3 = Match 
|____________|_________|____________|____________| 

我現在努力嘗試獲得所有相關信息張貼到空的question_answers表。但是,我很難提取所選選項的值,並且在單擊提交時,只有針對最後一個問題(問題3)單擊的選項出現在所有問題中。

我該如何爲每個單獨的問題量身定做,以及如何使用UPDATE將這三個結果發佈到數據庫?

<?php 
$choices = (choices()); 
$questions = (questions()); 
$questions_id = (questions_id()); 
$questions_length = count(questions()); 
for ($i = 0; $i < $questions_length; $i++){ 
    $each_q = $questions[$i]; 
    $each_q_id = $questions_id[$i]; 
?> 


<div class="poll"> 
    <div class="poll_question"> 
     <?php echo $each_q_id . ". " . $each_q; ?> 
    </div> 
    <form action="" method="post"> 
     <div class="poll_options"> 
      <div class="poll_option"> 


       <?php 
        $each_c = $choices[$i]; 
        $choices_length = count($each_c); 
        for ($k = 0; $k < $choices_length; $k++){ 

        $each_c = explode('<br />', $each_c); 
       ?> 

       <input type="radio" name="$each_c" value="1" id="c1"> 
       <label for="c1"><?php echo $each_c[0]; ?></label> 
       <input type="radio" name="$each_c" value="2" id="c2"> 
       <label for="c2"><?php echo $each_c[1]; ?></label> 
       <?php if (!empty($each_c[2]) === true){ ?> 
       <input type="radio" name="$each_c" value="3" id="c3"> 
       <label for="c2"><?php echo $each_c[2]; ?></label> 

       <?php  
       }} 

        $data = $_POST; 
        $each_cc = count($each_c); 
        print_r($session_user_id); 
        $option = 0; 
        if(isset($data['$each_c'])){ 
        $option = $data['$each_c']; 
        echo "--".$each_q_id."--".$each_cc."--". $option. "<br>"; 
       } 

       ?>    
     </div> 
    </form> 
<?php 

} 
?> 

功能:

function choices(){ 
    $query = mysqli_query($_POST['x'], "SELECT `question`, GROUP_CONCAT(`choice_text` ORDER BY `choice_text` DESC SEPARATOR '<br />') as `choice_texts` FROM `questions` JOIN `question_choices` ON questions.question_id = question_choices.question_id GROUP BY `question`"); 
    while($row = mysqli_fetch_assoc($query)){ 
     $rows[] = $row;} 
    if (count($rows)>0){ 
     foreach ($rows as $key => $value) { 
      $c[] = $value['choice_texts']; 
     } 
     return $c;}else return false; 
} 

function questions_id(){ 
    $query = mysqli_query($_POST['x'], "SELECT `question_id` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`"); 
    while($row = mysqli_fetch_assoc($query)){ 
     $rows[] = $row;} 
    if (count($rows)>0){ 
     foreach ($rows as $key => $value) { 
      $q_id = $value['question_id']; 
      $array_q[] = ($q_id); 
     } 
     return $array_q;}else return false; 
} 

function questions(){ 
    $query = mysqli_query($_POST['x'], "SELECT `question` FROM `questions` WHERE DATE(NOW()) BETWEEN `start` AND `end`"); 
    while($row = mysqli_fetch_assoc($query)){ 
     $rows[] = $row;} 
    if (count($rows)>0){ 
     foreach ($rows as $key => $value) { 
      $q = $value['question']; 
      $array_q[] = $q; 
     } 
     return $array_q;}else return false; 
} 
+0

您好。感謝您希望將問題標記爲已解決,並提供解決方案。但是,我們不會編輯問題來做到這一點:通過用解決方案覆蓋問題,Q&A格式的連續性丟失,並且結果不可能幫助未來的訪問者。如果您想報告您的解決方案,請自我回答。我剛剛爲你做了這個。 – halfer 2015-04-09 17:49:24

回答

1

首先我想在你的代碼中的每一個問題,不同的形式創建(也就是在第一個for循環)。 對於第二個for循環,您試圖設置單選按鈕,但是您正在使用未與此循環連接的變量 。 我正在應用你的代碼修改一點,所以當提交$ _POST會看起來:$ _POS ['1'] =>選擇值,$ _POST ['2'] =>選擇值... 其中'1', '2'...是你的問題ID。

<?php 
$choices = (choices()); 
$questions = (questions()); 
$questions_id = (questions_id()); 
$questions_length = count(questions()); 

echo '<form action="#" method="post">'; 

for ($i = 0; $i < $questions_length; $i++) { 
    $each_q = $questions[$i]; 
    $each_q_id = $questions_id[$i]; 
?> 


<div class="poll"> 
    <div class="poll_question"> 
     <?php echo $each_q_id . ". " . $each_q; ?> 
    </div> 
    <div class="poll_options"> 
     <div class="poll_option"> 


      <?php 
       $each_c = $choices[$i]; 
       $question = $questions[$i]; 
       $choices_length = count($each_c); 
       for ($k = 0; $k < $choices_length; $k++){ 

       $each_c = explode('<br />', $each_c); 
      ?> 

      <input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $choices[$i][$k]; ?>" id="c<?php echo $choices[$i][$k] ?>"> 
      <label for="c<?php echo $choices[$i][$k] ?>"><?php echo $choices[$i][$k]; ?></label> 

      <?php 
       } 
       $data = $_POST; 
       $each_cc = count($choices[$i]); 
       print_r($session_user_id); 
       $option = 0; 
       if(isset($data[$each_q_id])){ 
        $option = $data[$i]; 
        echo "--" . $each_q_id . "--" . $each_cc . "--" . $option . "<br>"; 
       } 
      ?> 
    </div> 
<?php 
} 
    echo '<input type=submit value="submit" />'; 
    echo '<br /></form><br /><br />'; 

    if (!empty($_POST)) { 
     var_dump($_POST); 
    } 
?> 
+0

感謝您的回覆,您真棒,迄今爲止這麼好......我只是想要爲每個問題展示所有選擇,現在它只顯示每個問題的第一選擇。但是,這工作! (我完成後會發布更新的代碼)。再次感謝你。 – selvyn 2015-02-09 16:00:33

+0

更新以上,它的工作和發佈結果: 125--1--1 - 我們可以說話的地方 - 等等等我將更改選項變量以輸出選擇的ID。再次感謝你幫助我度過了美好時光。 – selvyn 2015-02-09 16:32:23

+1

它是偉大的,如果它的工作。 – taliezin 2015-02-09 16:39:43

0

(發佈代表OP的。)

這個問題已經解決的代碼更改爲以下:

<?php 
$choices = (choices()); 
$questions = (questions()); 
$questions_id = (questions_id()); 
$questions_length = count(questions()); 

echo '<form action="#" method="post">'; 

for ($i = 0; $i < $questions_length; $i++) { 
    $each_q = $questions[$i]; 
    $each_q_id = $questions_id[$i]; 
?> 


<div class="poll"> 
    <div class="poll_question"> 
     <?php echo $each_q_id . ". " . $each_q; ?> 
    </div> 
    <div class="poll_options"> 
     <div class="poll_option"> 


      <?php 
       $each_c = $choices[$i]; 
       $each_c = explode('<br />', $each_c); 
       $choices_length = count($each_c); 
       for ($k = 0; $k < $choices_length; $k++){ 

      ?> 


      <input type="radio" name="<?php echo $each_q_id; ?>" value="<?php echo $each_c[$k]; ?>" id="<?php echo $each_c[$k] ?>"> 
      <label for="<?php echo $each_c[$k] ?>"><?php echo $each_c[$k]; ?></label> 

      <?php 
       } 
       $data = $_POST; 
       $each_cc = count($choices[$i]); 
      // print_r($session_user_id); 
       $option = 0; 
       if(isset($data[$each_q_id])){ 
        $option = $data[$each_q_id]; 
        echo $session_user_id . "--" . $each_q_id . "--" . $each_cc . "--" . $option . "--" . "<br>"; 
       } 
      ?> 
    </div> 
<?php 
} 
    echo '<input type=submit value="submit" />'; 
    echo '<br /></form><br /><br />'; 

    if (!empty($_POST)) { 
     var_dump($_POST); 
    } 
?>