Page 1 of 1

Returning Column Headings From Mysql Table

Posted: Fri Oct 02, 2009 10:43 am
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>";

Re: Returning Column Headings From Mysql Table

Posted: Fri Oct 02, 2009 10:57 am
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>";
 

Re: Returning Column Headings From Mysql Table

Posted: Wed Oct 07, 2009 10:42 am
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.

Re: Returning Column Headings From Mysql Table

Posted: Wed Oct 07, 2009 11:02 am
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 ))

Re: Returning Column Headings From Mysql Table

Posted: Thu Oct 08, 2009 8:31 am
by laythingy59
that's brilliant.... thanks