Jump to content

How to display some sql query data in a loop or array?


gazzo1967

Recommended Posts

Hi All

I wonder if some one can help as im a 'newbie' at php?

I have tried to do this but i just can seem to get my head around it.

 

What i am trying to do is to echo out fields once 

 

"Ref Number",
"Course Name",
"First Name",
"Last Name",
"Employee Number",
"Email Address",
 
But then echo out the following fields repeatedly for the user entered data
"Question",
"Answer",
 
i have researched but every array/loop i look at does not use a query as the output..
My database is postgres.
 
Here is my code below but it just comes up blank when i try to run it :(
if i remove the if statement it works but gives out all data
 
thanks for the help :)
Regards Gaz
 
$i = 0;
$currentuser = 'init';
$nextuser = 'init'; // added init as sometimes php doesnt like comparing null strings

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

$currentuser = $row['Email Address'];
if ($currentuser != $nextuser){
	
//show info below once
echo '<div style="border:1px solid #000000; padding:15px; margin-bottom: 10px;">';
echo '<p><strong>Job Reference Number:</strong><br> ' . $row['Ref Number'] . '</p>';
echo '<p><strong>Application:</strong><br> ' . $row['Course Name'] . '</p>';
echo '<p><strong>First Name:</strong><br> ' . $row['First Name'] . '</p>';
echo '<p><strong>Last Name:</strong><br> ' . $row['Last Name'] . '</p>';
echo '<p><strong>Employee Number:</strong><br> ' . $row['Employee Number'] . '</p>';
echo '<p><strong>Work Email Address:</strong><br> ' . $row['Email Address'] . '</p>';

//loop these entrys for the user then return to the begining for the next user
echo nl2br('<p><strong>Question:</strong><br> ' . $row['Question'] . '</p>');
echo nl2br('<p><strong>Answer:</strong><br> ' . $row['Answer'] . '</p>');
echo '</div>';
}

$rows = pg_num_rows($result);

if ($rows == 0)
{
echo 'Sorry, but we can not find an entry to match your query<br><br>';
}
Edited by gazzo1967
Link to comment
Share on other sites

Hi thank you for responding

i do have error reporting on and didn't post the whole code as the query, connection etc work fine :0)

 

If i change my output  to this it returns my query

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

echo '<div style="border:1px solid #000000; padding:15px; margin-bottom: 10px;">';
echo '<p><strong>Job Reference Number:</strong><br> ' . $row['Ref Number'] . '</p>';
echo '<p><strong>Application:</strong><br> ' . $row['Course Name'] . '</p>';
echo '<p><strong>First Name:</strong><br> ' . $row['First Name'] . '</p>';
echo '<p><strong>Last Name:</strong><br> ' . $row['Last Name'] . '</p>';
echo '<p><strong>Employee Number:</strong><br> ' . $row['Employee Number'] . '</p>';
echo '<p><strong>Work Email Address:</strong><br> ' . $row['Email Address'] . '</p>';

echo nl2br('<p><strong>Question:</strong><br> ' . $row['Question'] . '</p>');
echo nl2br('<p><strong>Answer:</strong><br> ' . $row['Answer'] . '</p>');
echo '</div>';
}

it might help to show the data structure in my table

 

this would be for only one user

 

"Ref Number", "Course Name", "First Name","Last Name","Employee Number","Email Address","Question","Answer"  (question 1)
"Ref Number", "Course Name", "First Name","Last Name","Employee Number","Email Address","Question","Answer"  (question 2)
"Ref Number", "Course Name", "First Name","Last Name","Employee Number","Email Address","Question","Answer"  (question 3)
"Ref Number", "Course Name", "First Name","Last Name","Employee Number","Email Address","Question","Answer"  (question 4)
"Ref Number", "Course Name", "First Name","Last Name","Employee Number","Email Address","Question","Answer"  (question 5)
etc upto 20 questions
 
then next user would be the same and so on
what i am trying to achieve is
User 1
 
"Ref Number",
"Course Name",
"First Name",
"Last Name",
"Employee Number",
"Email Address",
 
"Question", ( 1)
"Answer", (1)
 
"Question", ( 2)
"Answer", (2)
"Question", ( 3)
"Answer", (3)
"Question", ( 4)
"Answer", (4)
etc till the end of the user 1 entries
 
then loop to the next
user 2
 
i hope that makes sense
 
Gary
Edited by gazzo1967
Link to comment
Share on other sites

Just never heard of it and thought it was some oddball one.  My bad....:(

 

Re: the real problem tho,

 

If the _array call is not working gotta wonder why sicne it shouldn't matter.  OP should do a var dump of the $result and see exactly how his query results are being returned.  Of course, using the _assoc function is a better choice since he doesn't need the numeric indices.

 

Could it have anything to do with the index names in use, with the embedded spaces?

Link to comment
Share on other sites

No takers?

I am a new beginner to php and i'm the type of person that learns by seeing an example. Unfortunately all the examples i see online do not use a query but pre-populated data and not having the level of experience (yet) i find it difficult to apply what i need to do.

The reason why there are different code examples is my bad as i have tried different ways of returning the query result.

I would appreciate anyone who could help

Link to comment
Share on other sites

Several things:

  • If you want help with code, we need the whole code or at least a very carefully shortened version of it. What you've shown above isn't even syntactically valid, so now we have no idea if that's the whole problem or just the result of sloppy editing.
  • Turning up the error reporting isn't enough. You also need to either display or log the messages -- and read them, of course. A blank page is a strong indicator that you not have proper error handling. Also note that the pg extension requires manual error checks after every single function call. You don't have that.
  • You'll make your life a lot easier if you turn the query result into a more convenient array structure instead of trying to build the HTML markup directly from the rows. For example, create an array with one row per user and collect the questions and answers within the user records.
  • You should consider replacing the pg_* functions with PDO. This is a universal database interface which is much more convenient than the low-level interface you're currently using. For example, you can make PDO automatically throw an exception whenever something goes wrong, so there's no need for manual error checks.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.