简体   繁体   中英

How to restructure the array so is possible to store each answer and question_id in the answers table?

If the user is doing a registration in a conference and selects quantity “1” for registration type general and “1” for registration type plus and click “Next” and goes to the registration form.

In the form, the user needs to introduce the name and surname for each participant this two fields are always mandatory for each participant being registered. Then, the registration type general has 3 custom questions associated with it, so for the registration type general the user also need to answer to that 3 custom questions. The questions and the answers answered by the user in the registration form were this:

Question                                  Answer
input text custom question                text answer
long text custom question:                long answer
checkbox custom question:                 check1answer

With the registration form below, and with the answers above, the participant array in the $request->all() shows like below. The name, surname, rtypes is stored properly but the answers and question_id are not be stored very properly in the array:

  "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" => "check1answer"
      ]
      5 => array:1 [▼
        "question_id" => "3"
      ]
      "rtypes" => "1"
    ]
    2 => array:3 [▼
      "name" => "Jake"
      "surname" => "K"
      "rtypes" => "4"
    ]
  ]

With the data stored like that using this foreach to insert the participants and the answers in the database shows "Undefined index: answer" in $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['rtypes']
    ]);

    Answer::create([
        'participant_id' => $participant_result->id,
        'answer' => $participant['answer'],
        'question_id' => $participant['question_id']
    ]);
}

Do you know to strucutre the participant array so solve the issue? So is possible to store correctly the answers of each participant and the question_id in the answers table?

The $participant[0]['answer'] shows "text answer".
The $participant[1]['answer'] shows "undefined index answer".
The $participant[2]['answer'] shows "long answer".
The $participant[3]['answer'] shows "undefined index answer".

Im a beginner but maybe a structure like this allow to use the foreach to store in the answers table. Each index of the array stores all info associated with each participant (name, surname, registration type where the participant is being registered and if there are custom questions associated with the registration type is also necessary to store the answers:

 "participant" => array:2 [▼
  1 => array:15 [▼
    "name" => "John"
    "surname" => "W"
    "answer" => [
      0  => "text answer"
      1  => "long answer"
      2  => "check1"
    ]
    "question_id" => [
      0 => "1"
      1 => "2"
      2 => "3"
    ]
    "rtypes" => "1"
  ]
  2 => array:3 [▼
    "name" => "Jake"
    "surname" => "K"
    "rtypes" => "4"
  ]
]

The participant in index 2 is being registered in the registration type "plus" that has id 4 and this registration type dont have any custom question associated. So is only necessary to collect the name, surname and the registration type id. So this is ok for the participant 2 because dont have custom questions.

Registration Form:

<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][rtypes]" 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][rtypes]" value="4"/>

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

You need to manipulate your original code in such a way that you will get the desired format via that code only.

But if you don't have control on that code, then try to manipulate your current array structure to make it in desired format using below code:-

$final  = array();
  foreach($array['participant'] as $key=>$val){

    $final['participant'][$key]['name'] = $val['name'];
    $final['participant'][$key]['surname'] = $val['surname'];
    $answer_array = array_column($val,'answer');
    $question_id_array = array_column($val,'question_id');
    if(is_array($answer_array) && count($answer_array) >0){
        $final['participant'][$key]['answer'] = $answer_array;
    }
    if(is_array($question_id_array) && count($question_id_array) >0){
        $final['participant'][$key]['question_id'] = $question_id_array;
    }
    $final['participant'][$key]['rtypes'] = $val['rtypes'];

  }

  print_r($final);

Output:- https://eval.in/1048496

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM