2015-02-11 67 views
0

我使用此代碼將問題與選項一起分組。將數組插入數據庫

<?php 

$entries = preg_split('/(?=[a-z\d]+\.(?!\d))/', $str, -1, PREG_SPLIT_NO_EMPTY); 

    $questions = array(); 
    $currentQuestion = null; 
    $id = 0; 

    foreach($entries as $entry) { 
    if(is_numeric(substr($entry, 0, 1)) === true) { 
     $currentQuestion = $entry; 
     $questions[$entry] = array(); 
     $id++; 
     // echo "INSERT INTO question (id, q_name) VALUES ($id, $currentQuestion)"."<br>"; 
     // mysqli_query($con, "INSERT INTO question (id, q_name) VALUES (NULL, '$currentQuestion')"); 
     continue; 

    } 

    // mysqli_query($con, "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 0);"); 
    // echo "INSERT INTO answers (id, choices, question, correct) VALUES (NULL, 'choices', $id , 'stuff')"."<br>"; 
    $questions[$currentQuestion][] = $entry; 
    } 

這是數組的結果。

Array 
(
    [1. What is love?] => Array 
     (
      [0] => a. Haddaway 

      [1] => b. Haxxaway 

      [2] => c. Hassaway 

      [3] => d. Hannaway 

     ) 

    [2. What is love? ] => Array 
     (
      [0] => a. Haddaway 

      [1] => b. Haxxaway 

      [2] => c. Hassaway 

      [3] => d. Hannaway 

     ) 

    [3. What is love 1.1? ] => Array 
     (
      [0] => a. Haddaway 

      [1] => b. Haxxaway 

      [2] => c. Hassaway 

      [3] => d. Hannaway 

     ) 

    [4. What is love? ] => Array 
     (
      [0] => a. Haddaway 

      [1] => b. Haxxaway 

      [2] => c. Hassaway 

      [3] => d. Hannaway 
     ) 

) 

,這是我的數據庫結構:表answersquestion列是從questions表的主鍵,這將決定哪些問題選擇屬於...

questions 
+-------+--------------------------+ 
| id | q_name     | 
+-------+--------------------------+ 
| 1 | 1.) What is foo?  | 
| 2 | 2.) What is foo?  | 
+-------+--------------------------+ 

answers 
+-------+-------------+-----------------------+ 
| id | choices  | question | correct | 
+-------+-------------+-----------------------+ 
| 1 | a. foo1 | 1   | 0  | 
| 2 | b. foo2 | 1   | 0  | 
| 3 | c. foo3 | 1   | 1  | 
| 4 | a. foo3 | 2   | 0  | 
| 5 | b. foo2 | 2   | 1  | 
| 6 | c. foo1 | 2   | 0  | 
+-------+-------------+-----------------------+ 

我設法插入問題db,但我有麻煩插入選擇,因爲我很困惑我應該怎麼做$questions爲了得到選擇...

任何建議(S)會做!

+0

** 1)**儘量不要在循環中運行查詢,而是在循環內部構建它並在循環之後執行它。 ** 2)**請粘貼數據庫表的結構; '答案'和'問題'('SHOW CREATE TABLE answers;')? – 2015-02-11 14:19:20

+0

哦,真抱歉...好吧,我會編輯它,非常感謝! – yowza 2015-02-11 14:20:39

+0

@ʰᵈˑ我更新了我的問題,我希望這已經夠透徹了。 – yowza 2015-02-11 14:40:23

回答

1

我會不是使用json_encode()因爲你正在建立一個規範化的數據結構。

下面是你需要做什麼的細目;

  • 插入問題納入questions
  • 抓住last_insert_id並將其存儲在一個變量
  • 使用last_insert_id

現在,請將答案爲answers

  • 鏈接的問題的答案到代碼上。

    收集數據

    $arrAnswers = array(); 
    $arrQuestions = array(); 
    $id = 0; //Assuming your table is empty 
    
    foreach($entries as $entry) { //Loop through the grabbed records 
        if(is_numeric(substr($entry, 0, 1)) === true) { //Is it a question? 
        $id++;  
        $arrAnswers[$id] = array(); 
        $arrQuestions[$id] = '(\''. $entry .'\')'; 
        } else { //Ok, it's a possible answer to a question 
        $arrAnswers[] = '(\''. $entry .'\', '. $id .', 0)'; 
        } 
    } 
    

    插入問題

    現在我們有一個數組保存所有的答案的一個問題。數組鍵是數據庫中的問題ID。我們現在可以通過干預來插入問題;

    $strDbQuery = "INSERT INTO `questions` (`q_name`) VALUES ". implode(", ", $arrQuestions); 
    // Execute the query. 
    

    插入

    現在你已經插入你的問題的答案,你現在可以將你的答案。

    $strDbQuery = "INSERT INTO `answers` (`choices`, `question`, `correct`) VALUES ". implode(", ", $arrAnswers); 
    // Execute the query. 
    

    因爲你的數組(不是你的問題)沒有保存一個值來表明答案是否正確,你必須手動完成。

  • +0

    是的,我會做些什麼來確定答案是正確的。我真的很感謝你幫助我的時間!獎勵!我學習新事物! – yowza 2015-02-11 16:20:26

    +0

    不客氣:)很高興我能幫到 – 2015-02-11 16:20:50

    +0

    再次問你,我在插入db的答案時出現錯誤,它說Array to String Conversion,我認爲這是發生的,因爲它是一個多維數組? 這是它返回...... INSERT INTO'answers'('choices','question','correct')值的數組 – yowza 2015-02-13 09:56:43

    1

    要存儲數組,您需要將其作爲字符串寫入數據庫。有兩個(2)功能浮現在腦海:

    serialize()serialize
    json_encode()json_encode

    要麼偉大工程; serialize()將數組轉換爲一個字符串,然後您可以保存;稍後檢索您的數組,將字符串傳遞給unserialize()函數。
    json_encode()也具有伴隨的json_decode()功能。要以數組形式讀取數據[而不是此場景中的對象],您必須這樣做:
    $questions = json_decode($string_from_database, true);

    我希望這有助於。

    +0

    我會看看這個!我想表達我的謝意! – yowza 2015-02-11 14:35:57

    +0

    在我看來,我猜json_encode()對我很有吸引力,我可能需要第二個意見! – yowza 2015-02-11 14:38:49

    +0

    @yowza你是對的。它的大小通常比'serialize()'小 – 2015-02-11 14:39:41