简体   繁体   中英

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:

"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. Its inserting correctly in the participants table but for the answers table is not working:

 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. But this values are not returned correctly from the array with " $request['answer'][$key] " and " $request['answer'][$key][$nameKey] ".

Do you know how to properly get the question id and answer from the array?

In this above array example, the question id of the "answer1" is 1 and the id of the "answer2" is 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 ). On second run you have $key == 4 , but not have this in $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.

Please tell me how $request['answer'] works? What is $request['answer'][1] and what $request['answer'][1][1] ?

Secondly $request['answer'][$key][$nameKey] in this case is array of two elements. If Answer::create knows about it - it's no problem ;)

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

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