【问题标题】:How to get each question_id and answer from the array?如何从数组中获取每个 question_id 和答案?
【发布时间】:2018-08-20 18:51:55
【问题描述】:

如果用户在会议中进行注册,并且正在注册 1 名 id 为“1”的注册类型的参与者 (Jake K),以及 1 名 id 为“4”的注册类型的参与者 (John W),带有表单数据的请求有这 3 个数组:

"name" => array:2 [▼
  1 => array:1 [▼
    1 => "Jake"
  ]
  4 => array:1 [▼
    1 => "John"
  ]
]
"surname" => array:2 [▼
  1 => array:1 [▼
    1 => "K"
  ]
  4 => array:1 [▼
    1 => "W"
  ]
]
"answer" => array:1 [▼
  1 => array:1 [▼   
    1 => array:2 [▼   // answers answered for the registration_type_id 1
      1 => "answer1"
      2 => "answer2"
    ]
  ]
]

必须使用该数组的信息插入participantsanswers 表中。它在participants 表中正确插入,但对于answers 表不起作用:

 foreach ($request->all()['name'] as $key => $nameArray) {
        foreach ($nameArray as $nameKey => $name) {
            // this is working
            $participant_result = Participant::create([
                'name'                 => $name,
                'surname'              => $request['surname'][$key][$nameKey],
                'registration_id' => $registration->id,
                'registration_type_id' => $key
            ]);


            // save answer to Database if exist
            // this is not working
            $answer = Answer::create([
                'question_id' => $request['answer'][$key], // the issue is here
                'participant_id' => $participant_result->id,
                'answer' => $request['answer'][$key][$nameKey], // and here
            ]);
        }
    }

问题是因为需要在answers 表的question_idanswer 列中插入。但是这个值没有从带有“$request['answer'][$key]”和“$request['answer'][$key][$nameKey]”的数组中正确返回。

你知道如何正确地从数组中获取问题 id 和答案吗?

在上述数组示例中,“answer1”的问题 id 为 1,“answer2”的 id 为 2。

  "answer" => array:1 [▼
    1 => array:1 [▼
      1 => array:2 [▼
        1 => "answer1"
        2 => "answer2"
      ]
    ]

表格:

<form method="post"
      action="https://proj.test/conference/1/conf-test/registration/storeRegistration">
    <h6> Participant - 1 - general</h6>

    <div class="form-group">
      <label for="namegeneral_1">Name</label>
      <input type="text" required  id="namegeneral_1" name="name[1][1]" class="form-control" value="">
    </div>

    <div class="form-group">
      <label for="surnamegeneral_1">Surname</label>
      <input type="text"  id="surnamegeneral_1" class="form-control" name=" surname[1][1]" value="">
    </div>

    <div class="form-group">
      <label for="participant_question">Question 1</label>
      <input type='text' name='answer[1][1][1]' class='form-control' required>
      <input type="hidden" name="question_required[1][1]" value="1">
    </div>

    <div class="form-group">
      <label for="participant_question">Question 2</label>
      <textarea name='answer[1][1][2]' class='form-control' rows='3' required></textarea>
      <input type="hidden" name="question_required[1][2]" value="1">
    </div>

    <h6> Participant - 1 - plus</h6>

    <div class="form-group">
      <label for="nameplus_1">Name</label>
      <input type="text" required  id="rnameplus_1" name="rname[4][1]" class="form-control" value="">
    </div>

    <div class="form-group">
      <label for="surnameplus_1">Surname</label>
      <input type="text"  id="rsurnameplus_1" class="form-control" name=" rsurname[4][1]" value="">
    </div>

    <input type="submit" class="btn btn-primary" value="Register"/>
</form>

【问题讨论】:

    标签: php


    【解决方案1】:

    首先:在循环中你有多个名字,但答案只有一个键(1)。在第二次运行时,您有 $key == 4,但在 $request['answer'] 中没有这个。也许在第一个循环内你应该有两个循环?一个用于注册参与者,一个用于注册答案?如果两者都注册了答案,则使用$participantResult 构建数组,并为每个答案注册count($participantResult) 答案:O 如果没有,请仅记住第一个注册参与者的 ID 并保存答案。

    请告诉我$request['answer'] 是如何工作的?什么是$request['answer'][1],什么是$request['answer'][1][1]

    其次,$request['answer'][$key][$nameKey] 在这种情况下是两个元素的数组。如果 Answer::create 知道 - 没问题 ;)

    编辑

    这是正确的解决方案吗?

    <?php
    $aPID = [];
    
    foreach ($request->all()['name'] as $key => $nameArray) {
        foreach ($nameArray as $nameKey => $name) {
            $aPID[$key] = Participant::create([
                'name'                 => $name,
                'surname'              => $request['surname'][$key][$nameKey],
                'registration_id' => $registration->id,
                'registration_type_id' => $key
            ]);
        }
    }
    
    foreach($request['answer'] as $pid => $answersArray) {
        foreach($answersArray as $questionId => $answer) {
            $answer = Answer::create([
                'question_id' => $questionId,
                'participant_id' => $aPID[$pid],
                'answer' => $answer,
            ]);
        }
    }
    

    【讨论】:

    • id 为 4 的注册类型没有答案数组,因为没有与 id 为 4 的注册类型相关联的自定义问题,因此在注册表中不会出现在注册的参与者的任何自定义问题id 为 4 的注册类型,因此 id 为 4 的注册类型没有 answers 数组。
    • 伙计,我知道$request['answer'][1] 的结果是什么,但不知道[1] 是什么意思?第二个来自$request['answer'][1][1]?我认为第一个索引是参考['name'][1],但第二个?你能告诉我表单是如何构建的吗?
    • 我更新了这个问题,$request['answer'] [1] 是 id 为 1 的注册类型的答案。
    • 我认为如果你有这样的表格会更舒服:name="participant[1][name]"name="participant[1][answer][1] ...如果可能的话;)
    • 就像它显示“数组到字符串的转换(SQL:插入answers (question_id, participant_id, answer) 值(1, {"name":"Jake" ,"surname":"K","re​​gistration_id":207,"registration_type_id":1,"id":83}, answer1)) "。
    猜你喜欢
    • 2012-07-10
    • 2013-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 1970-01-01
    • 2012-05-07
    • 2023-03-20
    相关资源
    最近更新 更多