I'm working on a project and I need to recover Id in php or js
I have 2 row in the same table (mySql) row 1 = id_answer and row 2 = text
I exploit the first row text with a foreach in my code.
I search to associate in the same time in a div, my text and in value the Id who doing references to this answer.
<ul id="listQuestionResponseFaq" class="col-sm-12 pad-0">
<?php
foreach ($questionResponseArrayFaq as $questionResponseFaq ) {
?>
<li class="liSearchFaq col-sm-8 col-sm-offset-2 pad-0">
<div class='panel padding txtLeft' value="<?php $answerSearch ?> ">
<?php echo $questionResponseFaq; ?>
</div>
</li>
<?php
}
?>
</ul>
I dont know how to recover the id_answer and insert in my answer. Its not the same id for all answer. Every answer got a different ID
Thx to help me
$queryFaq = $pdoRepository->executeQuery("
SELECT DISTINCT fa.* FROM form_answer AS fa
LEFT JOIN form_option_filter AS fof
ON fof.id_option = fa.id_option
LEFT JOIN form_option AS fo
ON fo.id_option = fa.id_option
WHERE fof.statut = 1
AND fo.search = 1");
if ($queryFaq) {
$questionResponseArrayFaq = array();
while ($row = $queryFaq->fetch(PDO::FETCH_ASSOC)) {
$questionsSearch = $row['text'];
$answerSearch = $row['id_answer'];
array_push($questionResponseArrayFaq, $questionsSearch);
}
}
I think I understand what you are looking for and if so I am going to recomend you take a different approach to take advantage of PHP's associate array looping.
$questions = array();
while ($row = $queryFaq->fetch(PDO::FETCH_ASSOC))
{
$questions[$row['text']] = $row['id_answer'];
}
foreach($questions as $question => $answer)
{
echo 'Question: ' . sanitize($question);
echo 'Answer:' . sanitize($answer);
}
If you really want 2 seperate arrays you can itterate through both using the reset
and next
functions. I found this on another stack overflow example a hwile ago but can't find it right now. If someone does feel free to reference and give credit.
$questions = array();
$answers = array();
while ($row = $queryFaq->fetch(PDO::FETCH_ASSOC))
{
$questions[] = $row['text']
$answers[] = $row['id_answer'];
}
$answer = reset($answers);
foreach($questions as $question)
{
echo 'Question: ' . sanitize($question);
echo 'Answer:' . sanitize($answer);
$answer = next($answers);
}