php query headers

Thread Solved
Reply

Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

php query headers

 
0
  #1
May 6th, 2008
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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 71
Reputation: amigura is an unknown quantity at this point 
Solved Threads: 7
amigura's Avatar
amigura amigura is offline Offline
Junior Poster in Training

Re: php query headers

 
0
  #2
May 6th, 2008
guessing this is wat you mean. fill in missing names.

  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>";
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: php query headers

 
0
  #3
May 6th, 2008
Ok, i think I answered my own question, someone please tell me if this was the wrong way to do this, but it works.
  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,
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 100
Reputation: rickarro is an unknown quantity at this point 
Solved Threads: 1
rickarro rickarro is offline Offline
Junior Poster

Re: php query headers

 
0
  #4
May 6th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC