Get id of two html elements?

Asked

Viewed 56 times

2

I have an array of inputs and use to record in the database

/*ANSWERS */
foreach($params['questionnaire_questions_answers'] as $key => $item){
    $student_anamineses_answers = DB::table('student_anamineses_answers')->insert(
        array(
            'student_anamnese_id' => $id,
            'questionnaire_question_id' => 2,/*VER COMO GRAVAR O questionnaire_question_id DINAMICAMENTE */
            'answer' => $item
        )
    );
}
/*ANSWERS */

The problem is questionnaire_question_id is in another input:

<input type="hidden" name="questionnaire_question_id[]" value="'+ item.id +'" />\
<textarea required name="questionnaire_questions_answers[]" cols="80" 
   placeholder="Digite a resposta aqui" rows="3" id="perguntas"></textarea>')

It is a question and answer system. I record the question table called Question and then need to record the answers in the Answers tables. I do a foreach to get the id of the questionnaire_questions_answers textarea so I can save the answer to the Answers table. Only I need to get the question id, q I pass the questionnaire_question_id input. When I do a foreach in the textareas I can take the data (Answers) and record in the bano, but I don’t know how to take the id of the question and record in the same table Answers. How to go through this input and save the information in questionnaire_question_id?

  • 2

    I did not understand well, the problem, this input it is sent along with the other data ? can explain the process that is happening, the question is a little vague.

  • Also not understand very well what you want, you want to access the questionnaire_question_id which is associated with questionnaire_questions_answers? If so when walking through one keep the index and use in the array of the other element. I think you want to do this questionnaire_questions_answers[0] questionnaire_question_id[0]

  • edited the question to see if it facilitates the understanding

1 answer

1


As the two ids are distinct variables you have to know which position you want to access.
You can use the foreach but you have to create a variable in advance to store the current position and then be able to relate to the other array. Example:

/*ANSWERS */
$acc = 0;
$outros_ids = $params['questionnaire_question_id'];
foreach($params['questionnaire_questions_answers'] as $key => $item){
    $student_anamineses_answers = DB::table('student_anamineses_answers')->insert(
        array(
            'student_anamnese_id' => $id,
            'questionnaire_question_id' => $outros_ids[$acc++],/*VER COMO GRAVAR O questionnaire_question_id DINAMICAMENTE */
            'answer' => $item
        )
    );
}
/*ANSWERS */

Another solution could be the use of the function array_shift that removes the first value of an array and returns it.

/*ANSWERS */
$outros_ids = $params['questionnaire_question_id'];
foreach($params['questionnaire_questions_answers'] as $key => $item){
    $student_anamineses_answers = DB::table('student_anamineses_answers')->insert(
        array(
            'student_anamnese_id' => $id,
            'questionnaire_question_id' => array_shift($outros_ids),/*VER COMO GRAVAR O questionnaire_question_id DINAMICAMENTE */
            'answer' => $item
        )
    );
}
/*ANSWERS */

From what I understand of your question this is what you intend to do, correct me if I’m wrong.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.