Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

77
Views
Displaying PHP search results on screen

I have complied a php search page but i am having trouble working out a page display issue that i would like to have.

My data return query is correct and i believe my issue is how i display the information on screen.

Example table

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

what i would like to display

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

What I am getting is this

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

I believe I have to use an array with foreach from doing my searches on the internet but i am new to PHP and self teaching so find it difficult to understand the way it works. This is how i echo my results

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

Sorry i updated my post to be alittle clearer

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Store the results in an array and then print them to the screen.

//I have never seen pg_fetch_array()
while($row = mysql_fetch_assoc($result)){
  $new_arr[] = $row;
}
//print each row
foreach($new_arr as $new){
  echo $new;
}
over 4 years ago · Santiago Trujillo Report

0

Try it

<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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!