Advertisement
Guest User

fix column names

a guest
Apr 29th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. echo '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example">';
  2.  
  3. // The column names need to be renamed to their proper names
  4. $table="col2desc";
  5.  
  6. // Read the mapping table
  7. $query = "select fieldname,realfieldname from $table";
  8.  
  9. // query
  10. $result=mysql_query($query);
  11.  
  12. // push all the data into an array
  13. while ( $row = mysql_fetch_assoc($result) )
  14. {
  15.     $name                   = $row['fieldname'];
  16.     $desc                   = $row['realfieldname'];
  17.     $mapping["$name"]       = $desc;
  18. }
  19.  
  20. echo '
  21. <thead>
  22. <tr>
  23. ';
  24.  
  25. $table  = "books";
  26. $query  = "show columns from $table";
  27. $result = mysql_query($query);
  28.  
  29. while ( $row = mysql_fetch_assoc($result) )
  30. {
  31.     $fieldname = $row['Field'];
  32.     if ( $fieldname == 'id' ) continue;
  33.  
  34.     # and map the field name to the description
  35.    if (array_key_exists("$fieldname", $mapping))
  36.     {
  37.         $desc = $mapping["$fieldname"];
  38.     }
  39.     else
  40.     {
  41.         $desc = $fieldname;
  42.     }
  43.  
  44.     echo "<th>$desc</th>";
  45. }
  46.  
  47. echo '
  48. </tr>
  49. </thead>
  50. <tbody>
  51. ';
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement