Solved

Display Column Name with PHP using MYSQL dbase

Posted on 2004-09-11
5
370 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

11 Experts available now in Live!

Get 1:1 Help Now