2015-02-11 123 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