Solved

Display Column Name with PHP using MYSQL dbase

Posted on 2004-09-11
5
373 Views
Last Modified: 2013-12-12
I have mysql table and would like to display column names along data in vertical fashion. My PHP code is as follows where instead of static (dummy) FieldName I would like to dump column name and if possible replace underscore with space e.g (First_Name with First Name):

<?php
         for ( $counter= 0;
         $row = mysql_fetch_row( $result );
         $counter++ ){          
         foreach ( $row as $key => $value )
         print( "<tr><td>FieldName<td>$value</td></tr>" );              
      }
      mysql_close( $database );
?

at the moment my php displays:
FieldName    John
FieldName    Smith
FieldName    35
etc....

tks mates.
0
Comment
Question by:miamati
  • 4
5 Comments
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033947
try:

         for ( $counter= 0;
         $row = mysql_fetch_row( $result );
         $counter++ ){          
         foreach ( $row as $key => $value )
         print "<tr><td>";
         echo mysql_field_name($result,$key);
         print "<td>$value</td></tr>";              
      }
      mysql_close( $database );
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033966
I think your code will work better like this

      for ($counter= 0;$row = mysql_fetch_row($result);$counter++ ){
       foreach ($row as $key => $value) {       
          print "<tr><td>";
          echo mysql_field_name($result,$key);
          print "<td>$value</td></tr>";
       }
      }
      mysql_close( $database );
0
 
LVL 27

Expert Comment

by:Diablo84
ID: 12033976
oh and this accounts for replacing the underscore too:

 for ($counter=0;$row=mysql_fetch_row($result);$counter++){
  foreach ($row as $key => $value) {
   echo "<tr><td>";
   $field = mysql_field_name($result,$key);
   $field = str_replace("_"," ",$field);
   echo $field;
   echo "<td>$value</td></tr>\n";
  }
 }
 mysql_close($database);
0
 

Author Comment

by:miamati
ID: 12034027
ok many thanks..succeeded! Before I accept answer is there any way to code add to your last solution in order to convert first column name lower case to upper case? Eg. col surname to Surname or col work experience to Work experience. Thanks again.
0
 
LVL 27

Accepted Solution

by:
Diablo84 earned 500 total points
ID: 12034438
use the ucfirst function, eg.

 for ($counter=0;$row=mysql_fetch_row($result);$counter++){
  foreach ($row as $key => $value) {
   echo "<tr><td>";
   $field = mysql_field_name($result,$key);
   $field = str_replace("_"," ",$field);
   echo ucfirst($field);
   echo "<td>$value</td></tr>\n";
  }
 }
 mysql_close($database);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now