用php/js/mysql恢复id或值

hrysbysz  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(207)

我在做一个项目,我需要恢复php或js的id
我在同一个表中有两行(mysql)行1=id\u answer,行2=text
我在代码中使用foreach攻击第一行文本。
我在一个div中同时搜索与我的文本和值相关联的id,id引用了这个答案。

<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>

我不知道如何恢复id\u答案并插入我的答案。不是所有答案的id都一样。每个答案都有不同的id
谢谢你帮我
mysqlbase公司

$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);        

    }
}
im9ewurl

im9ewurl1#

我想我了解你在寻找什么,如果是这样,我建议你采取不同的方法来利用php的关联数组循环。

$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);
}

如果你真的想要两个独立的数组,你可以使用 reset 以及 next 功能。一个小时前我在另一个堆栈溢出示例中发现了这个,但现在找不到。如果有人愿意提供参考和信用。

$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);
}

相关问题