简体   繁体   English

如何从数组中获取每个 question_id 和 answer?

[英]How to get each question_id and answer from the array?

If the user is doing a registration in a conference, and is registering 1 participants in the registration type with id "1" (Jake K), and 1 participant in the regitration type with id "4" (John W), the request with the form data has this 3 arrays:如果用户在会议中进行注册,并且正在注册 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"
    ]
  ]
]

Is necessary to use the info of this array to insert in the participants and answers table.有必要使用此数组的信息插入participantsanswers表中。 Its inserting correctly in the participants table but for the answers table is not working:它在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
            ]);
        }
    }

The issue is because is necessary to insert in the question_id and answer columns of the answers table.问题是因为有必要插入到answers表的question_idanswer列中。 But this values are not returned correctly from the array with " $request['answer'][$key] " and " $request['answer'][$key][$nameKey] ".但是这个值没有从带有“ $request['answer'][$key] ”和“ $request['answer'][$key][$nameKey] ”的数组中正确返回。

Do you know how to properly get the question id and answer from the array?您知道如何从数组中正确获取问题 ID 和答案吗?

In this above array example, the question id of the "answer1" is 1 and the id of the "answer2" is 2.在上面的数组示例中,“answer1”的问题 id 为 1,“answer2”的 id 为 2。

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

Form:形式:

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

Firstly: in loop you have more than one name, but answer has one key ( 1 ).第一:在循环中,您有多个名字,但 answer 有一个键 ( 1 )。 On second run you have $key == 4 , but not have this in $request['answer'] .在第二次运行时,您有$key == 4 ,但在$request['answer'] Maybe inside first loop you shoud have two loops?也许在第一个循环中你应该有两个循环? One for registering participants and one for registering answers?一种用于注册参与者,一种用于注册答案? If answers are registered for both, build array with $participantResult and for every answer register count($participantResult) answers :O If not, remember ID only for first registered participant and save answer.如果两个答案都注册了,用$participantResult构建数组,并为每个答案注册count($participantResult)答案:O 如果没有,只记住第一个注册参与者的 ID 并保存答案。

Please tell me how $request['answer'] works?请告诉我$request['answer']是如何工作的? What is $request['answer'][1] and what $request['answer'][1][1] ?什么是$request['answer'][1]什么是$request['answer'][1][1]

Secondly $request['answer'][$key][$nameKey] in this case is array of two elements.其次$request['answer'][$key][$nameKey]在这种情况下是两个元素的数组。 If Answer::create knows about it - it's no problem ;)如果 Answer::create 知道它 - 没问题;)

EDIT编辑

Is this right solution?这是正确的解决方案吗?

<?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,
        ]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何重组数组,以便将每个答案和Question_id存储在答案表中? - How to restructure the array so is possible to store each answer and question_id in the answers table? 为什么只从隐藏字段中获取最后一个 question_id 值……? - Why geting in request only last question_id value from hidden field…? 无法生成和插入quiz_id和question_id - failing to generate and insert quiz_id and question_id 从其他表中获取 id 然后插入到表中。 我想点击按钮然后回答当前问题 - Get id from other table then insert into table. I want to click the button then answer current question 当question_id相同而user_id不同时的sql查询 - sql query to count when question_id is same and user_id is different 我应该如何在 php 中为每个问题做一个答案的测验? - How should I make quiz with one answer for each question in php? 在我的测验ID上循环并获取每个并插入答案表 - loop on my quiz id and get each one and inserted in the answer table 从iPhone上的HTTP请求获取数组作为答案 - Get an array as answer from HTTP request on iPhone 如何将每个答案存储在答案表中? (如何正确存储在数组中) - How to store each answer in the answers table? (how to properly store in the array) 如何从parseJSON数组获取每个数据 - how to get each data from a parseJSON array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM