Returning Column Headings From Mysql Table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laythingy59
Forum Newbie
Posts: 3
Joined: Fri Oct 02, 2009 10:30 am

Returning Column Headings From Mysql Table

Post by laythingy59 »

Hi all

I'm quiet new to php and mysql, i do have some basic knowledge of the two and of programming in general.
i have a database with several tables.
i can successfully display data, but i would like the columns to have headings
the code im using is at the bottom
attached is a screen shot of the current output.

also if anyone could point me in the direction of a really goo guide to php i would appreciate it. i currently have a couple of pdfs but im finding parts of them difficult to understand....... something like a free dummies guide would be good i guess....

thanks

Adam

// Collects data from "players" table
$data = mysql_query("SELECT * FROM players,pdetails WHERE players.ID = pdetails.ID")
or die(mysql_error());


Print "<table border cellpadding=3>";

// puts the "players" info into the $info array
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th><b>ID:</b> </th> <td>".$info['ID'] . "</td>";
Print "<th><b>Name:</b> </th> <td>".$info['Name'] . "</td>";
Print "<th><b>Num:</b> </th> <td>".$info['Num'] . "</td>";
Print "<th><b>Pos:</b> </th> <td>".$info['Pos'] . "</td>";
Print "<th><b>DOB:</b> </th> <td>".$info['DOB'] . "</td>";
Print "<th><b>Height:</b> </th> <td>".$info['Height'] . "</td>";
Print "<th><b>Weight:</b> </th> <td>".$info['Weight'] . "</td>";
Print "<th><b>Shoots:</b> </th> <td>".$info['Shoots'] . "</td></tr>";
}
Print "</table>";
Attachments
output.jpg
output.jpg (49.71 KiB) Viewed 80 times
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Returning Column Headings From Mysql Table

Post by Mark Baker »

Code: Select all

 
Print "<table border cellpadding=3>";
 
// puts the "players" info into the $info array 
while($info = mysql_fetch_array( $data )) 
{
   Print "<tr>"; 
   foreach($info as $heading => $value) {
      Print "<th><b>" . $heading . "</b> </th> <td>" . $value . "</td>"; 
   }
   Print "</tr>"; 
} 
Print "</table>";
 
or

Code: Select all

 
Print "<table border cellpadding=3>";
 
$count = 0;
// puts the "players" info into the $info array 
while($info = mysql_fetch_array( $data )) 
{
   if ($count == 0) {
      $headings = array_keys($info);
      Print "<tr>"; 
      foreach($headings as $heading) {
         Print "<th>" . $heading . "</th>";
      }
      Print "</tr>"; 
   }
 
   Print "<tr>"; 
   foreach($info as $heading => $value) {
      Print "<td>" . $value . "</td>";
   }
   Print "</tr>"; 
   $count++;
} 
Print "</table>";
 
laythingy59
Forum Newbie
Posts: 3
Joined: Fri Oct 02, 2009 10:30 am

Re: Returning Column Headings From Mysql Table

Post by laythingy59 »

thanks for the reply. i have tried both options, the output of both still isnt right, the second option you gave is better. i dont have enough php knowledge to figure out whats wrong but im going to try and figure it out. attached are the two different outputs

the top image as you view it should be option 2, ive labeled the iamges

thanks again.
Attachments
Option 2
Option 2
option2.jpg (63.97 KiB) Viewed 57 times
Option 1
Option 1
Option1.jpg (89.4 KiB) Viewed 58 times
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Returning Column Headings From Mysql Table

Post by Mark Baker »

In each case, replace

Code: Select all

while($info = mysql_fetch_array( $data ))
with

Code: Select all

while($info = mysql_fetch_assoc( $data ))
or with

Code: Select all

while($info = mysql_fetch_array( $data, MYSQL_ASSOC ))
laythingy59
Forum Newbie
Posts: 3
Joined: Fri Oct 02, 2009 10:30 am

Re: Returning Column Headings From Mysql Table

Post by laythingy59 »

that's brilliant.... thanks
Post Reply