在屏幕上显示PHP搜索结果

时间:2017-04-28 00:11:46

标签: php postgresql

我已经编写了一个php搜索页面,但是我无法解决我想要的页面显示问题。

我的数据返回查询是正确的,我相信我的问题是我如何在屏幕上显示信息。

示例表

userid    Questionnaire ID      QID     Question        Answer 
  1        38                   846     Question 1      Answer 1
  1        38                   849     Question 2      Answer 2
  1        38                   850     Question 3      Answer 3
  1        38                   869     Question 4      Answer 4
  2        38                   846     Question 1      Answer 1
  2        38                   849     Question 2      Answer 2
  2        38                   850     Question 3      Answer 3
  2        38                   869     Question 4      Answer 4

我想展示什么

userid
1
Questionaire ID
38
Question 1
Answer 1
Question 2
Answer 2
Question 3
Answer 3
Question 4
Answer 4
PAGE BREAK (if possible)
userid
2
Questionaire ID
38
Question 1
Answer 1
Question 2
Answer 2
Question 3
Answer 3
Question 4
Answer 4

我得到的是这个

userid
1
Questionaire ID
38
Question 1
Answer 1
userid
1
Questionaire ID
38
Question 2
Answer 2
userid
1
Questionaire ID
38
Question 3 etc


userid
2
Questionaire ID
38
Question 1
Answer 1
userid
2
Questionaire ID
38
Question 2
Answer 2
userid
2
Questionaire ID
38
Question 3 etc

我相信我必须在互联网上使用foreach数组进行搜索,但我是PHP和自学的新手,因此很难理解它的工作方式。 这就是我回应结果的方式

while ($row = pg_fetch_array($result)) {


echo '<div style="border:1px solid #000000; padding:15px; margin-bottom: 10px;">';
echo '<p><strong>User ID:</strong><br> ' . $row['userid'] . '</p>';
echo '<p><strong>Questionaire ID:</strong><br> ' . $row['Questionaire ID'] . '</p>';
echo '<p><strong>Question:</strong><br> ' . $row['Question'] . '</p>';
echo '<p><strong>Answer:</strong><br> ' . $row['Answer'] . '</p>';
echo '</div>';

抱歉,我更新了我的帖子,以便更清楚

2 个答案:

答案 0 :(得分:0)

将结果存储在一个数组中,然后将它们打印到屏幕上。

var http = require('http');

var myServer = http.createServer(function(req,res){
    response.writeHead(200, {"Content-Type" : "text/plain"});
    response.write('Hello World');
    response.end();
});

myServer.listen(3000);

答案 1 :(得分:0)

试试吧

<table>
  <tr>
    <th>userid</th>
    <th>Questionnaire ID</th>
    <th>QID</th>
    <th>Question</th>
    <th>Answer</th>
  </tr>
  <?php
    while ($row = pg_fetch_array($result)) {
     echo '<tr>';
     echo '<th>'.$row['userid'].'</th>';
     echo '<th>'.$row['Questionaire ID'].'</th>';
     echo '<th>'.$row['QID'].'</th>';
     echo '<th>'.$row['Question'].'</th>';
     echo '<th>'.$row['Answer'].'</th>';
     echo '</tr>'
    ?>
</table>