gazzo1967 Posted June 13, 2017 Share Posted June 13, 2017 (edited) 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 June 13, 2017 by gazzo1967 Quote Link to comment Share on other sites More sharing options...
gazzo1967 Posted June 13, 2017 Author Share Posted June 13, 2017 apologies i spotted something on here but even tho i put it in still didn't work lol before the // show info below once it should have $nextuser = $currentuser; Sorry Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 13, 2017 Share Posted June 13, 2017 Huh? What comes up blank - the whole page? Try turning on php error checking. And be sure that you have data to show before going into the loop. Quote Link to comment Share on other sites More sharing options...
gazzo1967 Posted June 13, 2017 Author Share Posted June 13, 2017 (edited) 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 June 13, 2017 by gazzo1967 Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 13, 2017 Share Posted June 13, 2017 Is 'pg_fetch_array' a valid function? What extension is it part of? Quote Link to comment Share on other sites More sharing options...
benanamen Posted June 13, 2017 Share Posted June 13, 2017 (edited) Is 'pg_fetch_array' a valid function? What extension is it part of? Awe cmon ginerjm, I know you are more experienced than to make a post like that. RTFM or use Google. http://php.net/manual/en/function.pg-fetch-array.php Edited June 13, 2017 by benanamen Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 13, 2017 Share Posted June 13, 2017 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? Quote Link to comment Share on other sites More sharing options...
gazzo1967 Posted June 13, 2017 Author Share Posted June 13, 2017 Changing to assoc returns the same results. i was led to believe that to output how i want to do it was done by definining how i echo my query result Our server is running postgres Quote Link to comment Share on other sites More sharing options...
ginerjm Posted June 13, 2017 Share Posted June 13, 2017 You previously said if I change to this: (meaning fetch_assoc) it worked. Quote Link to comment Share on other sites More sharing options...
gazzo1967 Posted June 13, 2017 Author Share Posted June 13, 2017 apologies i have tried both array and assoc and it returns the same resulys Quote Link to comment Share on other sites More sharing options...
gazzo1967 Posted June 19, 2017 Author Share Posted June 19, 2017 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 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted June 19, 2017 Share Posted June 19, 2017 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.