943,929 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 997
  • PHP RSS
May 6th, 2008
0

php query headers

Expand Post »
Hey All,

I have a simple query form that displays from a mysql database, a list of names and phone numbers. What i'd like is a column heading at the top of the page but I can't seem to figure out how to put it in there. Here is my code so far:
PHP Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. // set database server access variables:
  4. $host = "localhost";
  5. $user = "user";
  6. $pass = "password";
  7. $db = "phonebook";
  8.  
  9. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  10.  
  11. mysql_select_db($db) or die ("Unable to select database!");
  12.  
  13. $query = "SELECT * FROM people ORDER BY fname ASC";
  14.  
  15. // execute query
  16. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  17.  
  18. // see if any rows were returned
  19. if (mysql_num_rows($result) > 0) {
  20. // yes
  21. // print them one after another
  22. echo "<table cellpadding=10 border=0>";
  23. while($row = mysql_fetch_row($result)) {
  24. echo "<tr>";
  25. echo "<td>".$row[1]."</td>";
  26. echo "<td>".$row[2]."</td>";
  27. echo "<td>".$row[3]."</td>";
  28. echo "<td>".$row[4]."</td>";
  29. echo "<td>".$row[5]."</td>";
  30. echo "<td>".$row[6]."</td>";
  31. echo "<td>".$row[7]."</td>";
  32. echo "</tr>";
  33. }
  34. echo "</table>";
  35. }
  36. else {
  37. // no
  38. // print status message
  39. echo "No rows found!";
  40. }
  41.  
  42. // free result set memory
  43. mysql_free_result($result);
  44.  
  45. // close connection
  46. mysql_close($connection);
  47.  
  48. ?>
I don't want to use the actual table names because they are abbreviated, ie fname = First Name. But I do want to let the viewer know what each of the columns are. How can I do this.

Thanks.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
May 6th, 2008
0

Re: php query headers

guessing this is wat you mean. fill in missing names.

PHP Syntax (Toggle Plain Text)
  1. echo "<table cellpadding=10 border=0>
  2. <tr><td>first name</td><td>last name</td><td>a</td><td>b</td><td>c</td><td></td><td>d</td>";
  3. while($row = mysql_fetch_row($result)) {
  4. echo "<tr>
  5. <td>".$row[fname]."</td>
  6. <td>".$row[lname]."</td>
  7. <td>".$row[3]."</td>
  8. <td>".$row[4]."</td>
  9. <td>".$row[5]."</td>
  10. <td>".$row[6]."</td>
  11. <td>".$row[7]."</td>
  12. </tr>";
  13. }
  14. echo "</table>";
Reputation Points: 11
Solved Threads: 7
Junior Poster in Training
amigura is offline Offline
71 posts
since Jan 2008
May 6th, 2008
0

Re: php query headers

Ok, i think I answered my own question, someone please tell me if this was the wrong way to do this, but it works.
PHP Syntax (Toggle Plain Text)
  1. // open connection
  2. $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
  3.  
  4. // select database
  5. mysql_select_db($db) or die ("Unable to select database!");
  6.  
  7. // create query
  8. $query = "SELECT * FROM people ORDER BY fname ASC";
  9.  
  10. // execute query
  11. $result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
  12.  
  13. // see if any rows were returned
  14. if (mysql_num_rows($result) > 0) {
  15. // yes
  16. // print them one after another
  17. echo "<table cellpadding=10 border=0>";
  18. //I ADDED THE ECHO'S BELOW TO CREATE MY TABLE HEADINGS
  19. //Before the "While" statement displays the headings just once at the top of the page.
  20. echo "<tr>";
  21. echo "<td><strong>First Name</strong></td>";
  22. echo "<td><strong>Last Name</strong></td>";
  23. echo "<td><strong>Phone Number</strong></td>";
  24. echo "<td><strong>Extension</strong></td>";
  25. echo "<td><strong>Title</strong></td>";
  26. echo "<td><strong>Department</strong></td>";
  27. echo "<td><strong>Fax Number</strong></td>";
  28. echo "</td>";
  29. while($row = mysql_fetch_row($result)) {
  30. echo "<tr>";
  31. echo "<td>".$row[1]."</td>";
  32. echo "<td>".$row[2]."</td>";
  33. echo "<td>".$row[3]."</td>";
  34. echo "<td>".$row[4]."</td>";
  35. echo "<td>".$row[5]."</td>";
  36. echo "<td>".$row[6]."</td>";
  37. echo "<td>".$row[7]."</td>";
  38. echo "</tr>";
  39. }
  40. echo "</table>";
  41. }
  42. else {
  43. // no
  44. // print status message
  45. echo "No rows found!";
  46. }
  47.  
  48. // free result set memory
  49. mysql_free_result($result);
  50.  
  51. // close connection
  52. mysql_close($connection);
  53.  
  54. ?>
I added my heading tags between my open table tag and my while statement. This displays the headings only once at the top of the page. This solved my problem, but is it the correct way to do this. Pointers would be appreciated.

Thanks,
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008
May 6th, 2008
0

Re: php query headers

Thanks Amigura, yes this is what i was looking for. It didn't occur to me at first that I had to put them in above the while statement. Thanks for the quick reply.
Reputation Points: 10
Solved Threads: 1
Junior Poster
rickarro is offline Offline
107 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Login problem, need fresh set of eyes.
Next Thread in PHP Forum Timeline: Image Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC