Jump to content

Code to display fields of a table


kemper

Recommended Posts

My page is displaying all content of the table, but I only want to display the fields from of table.  Assistance is greatly appreciated.  This is what I have thus far:

<?php

  include("config.inc.php");

  if ($db == 1) {
    echo "Using MySQL database info:<br><br>\n\n";
  } elseif ($file_db == 1) {
    echo "Using file-based database:<br><br>\n";
  } else {
    echo "Sorry. In order to use the administration portal you must use the MySQL or file-based database option.\n\n";
  }

  if ($file_db == 1 && $db != 1) {
    $filedb = fopen("data.dat","r") or die ("<br>Could not open data file to read.");
    $columns_str = fgets($filedb,4096);
    $columns = explode("|",$columns_str);
    echo "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" width=\"90%\">\n";
    echo "<tr><td colspan=\"" . (sizeof($columns)+2) . "\">\n";
    echo "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n";
    echo "<tr><td class=\"small\" bgcolor=\"#000000\" align=\"center\" valign=\"middle\">\n";
    echo "<font color=\"#ffffff\">Records Table</td></tr></table></td></tr><tr>\n";

    for ($i=0;$i<sizeof($columns);$i++) {
      echo "<td class=\"small\"  align=\"center\" valign=\"middle\"><b>".$columns[$i]."</b></td>";
    }
    $i=0;
    while (!feof($filedb)) {
      $temp = fgets($filedb,4096);
      if ($temp != "") {
        $records[$i]=$temp;
        $i++;
      }
    }

    for($j=0;$j<$i;$j++) {
      echo "<tr>";
      $holder = explode("|",$records[$j]);
      for ($k=0;$k<sizeof($holder);$k++) {
        echo "<td class='small'><center>$holder[$k]</td>";
      }
    }
    echo "</table>";
  } else if($db==1) {
    // mySQL Table
    $db_con = mysql_connect("MyHost", "MyUser", "MyPassword") or die("Connetion to database failed!");
    mysql_select_db($dbname);
    $query = "select * from ".$table;
    $result = mysql_query($query);
    $i = 0;

    while ($i < mysql_num_fields($result)) {
      $meta = mysql_fetch_field($result);
      $columns[$i] = $meta->name;
      $i++;
    }

    echo "<table cellspacing=\"0\" cellpadding=\"4\" border=\"1\" width=\"90%\">\n";
    echo "<tr><td colspan=\"" . (sizeof($columns)+2) . "\">\n";
    echo "<table cellspacing=\"0\" cellpadding=\"2\" border=\"0\" width=\"100%\">\n";
    echo "<tr><td class=\"small\" bgcolor=\"#000000\" align=\"center\" valign=\"middle\">\n";
    echo "<font color=\"#ffffff\">Records Table</td></tr></table></td></tr><tr>\n";

    for($i=1;$i<sizeof($columns);$i++) {
      echo "<td class='small'><center><b>".$columns[$i]."</td>";
    }
    $query = "select * from ".$table;
    $result = mysql_query($query);
    $j=0;

    while($row=mysql_fetch_array($result)) {
      echo "<tr>";

      for($i=1;$i<sizeof($columns);$i++) {
        echo "<td class='small'><center>".$row[$columns[$i]]."</td>";
      }

      $j=$row[$columns[0]];
    }
    echo "</table>";
  }

  // mySQL ends

?>

<br /><br />
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR></TD></TR>
<TR>
<TD class=small align=right>© 2006-2007</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
</center></form>

</body></html>
Link to comment
https://forums.phpfreaks.com/topic/30305-code-to-display-fields-of-a-table/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.