php - 如何将每个答案存储在答案表中?(如何正确存储在数组中)
问题描述
如果用户选择数量“1”作为注册类型一般,“1”作为注册类型加,然后单击“下一步”并进入注册表。
在表格中,用户需要介绍每个参与者的姓名和姓氏。然后,注册类型general有3个与之关联的自定义问题,因此对于注册类型general,用户还需要回答这3个自定义问题。用户在注册表中回答的问题和答案是:
Question Answer
input text custom question text answer
long text custom question: long answer
checkbox custom question: check1
使用注册表,并使用上面的答案,显示中的参与者数组$request
如下所示。名称、姓氏、regtypes 已正确存储,但答案未正确存储在数组中:
"participant" => array:2 [▼
1 => array:15 [▼
"name" => "John"
"surname" => "W"
0 => array:1 [▼
"answer" => "text answer"
]
1 => array:1 [▼
"question_id" => "1"
]
2 => array:1 [▼
"answer" => "long answer"
]
3 => array:1 [▼
"question_id" => "2"
]
4 => array:1 [▼
"answer" => "check1"
]
5 => array:1 [▼
"question_id" => "3"
]
"regtypes" => "1"
]
2 => array:3 [▼
"name" => "Jake"
"surname" => "K"
"regtypes" => "4"
]
]
使用这种 foreach 插入参与者的数据存储在数据库中,并且数据库中的答案在以下位置显示“未定义索引:答案” $participant['answer']
:
foreach ($participants_list as $participant) {
$name = $participant['name'];
$surname = $participant['surname'];
$participant_result = Participant::create([
'name' => $name,
'surname' => $surname,
'registration_type_id' => $participant['regtypes']
]);
Answer::create([
'participant_id' => $participant_result->id,
'answer' => $participant['answer'],
'question_id' => $participant['question_id']
]);
}
你知道可能是什么问题吗?也许是因为答案和 question_id 是如何存储在数组中的,但我不明白如何正确纠正它。
报名表格:
<form method="post" action="https://proj.test/conf/1/conf-test/registration/store">
<h6>Participant - 1 - general</h6>
<div class="form-group">
<label for="namegeneral_1">Name</label>
<input type="text" required id="namegeneral_1" name="participant[1][name]" class="form-control" value="">
</div>
<div class="form-group">
<label for="surnamegeneral_1">Surname</label>
<input type="text" required id="surnamegeneral_1" class="form-control" name="participant[1][surname]" value="">
</div>
<div class="form-group">
<label for="participant_question">input text custom question</label>
<input type='text' name='participant[1][][answer]' class='form-control' required>
<input type="hidden" name="participant_question_required[]" value="1">
<input type="hidden" value="1" name="participant[1][][question_id]"/>
</div>
<div class="form-group">
<label for="participant_question">long text custom question</label>
<textarea name='participant[1][][answer]' class='form-control' rows='3' required></textarea>
<input type="hidden" name="participant_question_required[]" value="1">
<input type="hidden" value="2" name="participant[1][][question_id]"/>
</div>
<div class="form-group">
<label for="participant_question">checkbox custom question</label>
<div class='checkbox-group required'>
<div class='form-check'>
<input type='checkbox' name='participant[1][][answer]' value='select1' class='form-check-input' >
<label class='form-check-label' for='exampleCheck1'>check1</label>
</div>
<div class='form-check'>
<input type='checkbox' name='participant[1][][answer]' value='select2' class='form-check-input' >
<label class='form-check-label' for='exampleCheck1'>check2</label>
</div>
</div>
<input type="hidden" name="participant_question_required[]" value="1">
<input type="hidden" value="3" name="participant[1][][question_id]"/>
</div>
<input type="hidden" name="participant[1][regtypes]" value="1"/>
<h6>Participant - 2 - plus</h6>
<div class="form-group">
<label for="nameplus_2">Name</label>
<input type="text" required id="nameplus_2" name="participant[2][name]" class="form-control" value="">
</div>
<div class="form-group font-size-sm">
<label for="surnameplus_2">Surname</label>
<input type="text" required id="surnameplus_2" class="form-control" name="participant[2][surname]" value="">
</div>
<input type="hidden" name="participant[2][regtypes]" value="4"/>
<input type="submit" class="btn btn-primary" value="Register"/>
</form>
解决方案
在你的循环中,你这样做:
foreach ($participants_list as $participant) {
也是这样$participant
:
1 => array:15 [▼
"name" => "John"
"surname" => "W"
但是answer
key 在上面的子数组中,所以从上面的数组中它是下一个级别:
0 => array:1 [▼
"answer" => "text answer"
因此,根据您的数组,answer
键不在$participant
循环值数组中,完整:
"participant" => array:2 [▼
1 => array:15 [▼
"name" => "John"
"surname" => "W"
0 => array:1 [▼
"answer" => "text answer"
answer
会在$participant[0]['answer']
推荐阅读
- php - php json数组中的未定义错误
- bash - 比较两个版本号(如果不同的次要版本则为真)
- javascript - 使用javascript函数onclick div替换HTML内容?
- macos - 未找到 NSNotification.Name.frameDidChangeNotification
- spring - 如何从 Java 文件中获取值到 jsp?
- python - 在 tf.keras 中使用 CsvDataset for model.fit 来自 CSV 的特征和标签
- angular - 将 Angular 6 应用程序托管到 firebase
- html - 如何将 Godaddy 服务器中的图像链接到我的 html 电子邮件正文
- javascript - AngularJS 将事件发送到另一个控制器($broadcast、$window.localStorage 等)
- django - Django:将 url slugs 传递给表单集查询集