Page 1 of 1

How would I keep the ID field from showing in the table..

Posted: Sun May 31, 2009 1:14 pm
by Herzlos
Up till this point I was going to use a Part Number for the KeyID field. But some Items put into the DB rows won't have a Part Number so I need to use a Column as an ID field that will auto increment for my KeyID. I don't want the ID numbers to show in the Table that in printed to the website. I am hoping someone will know the answer to this as I have yet to figure it out but I am still looking for the answer.
Here is the code I have:

Code: Select all

 
 
<?php
//change to table name for page
    echo "<table  class=\"wire\" align=\"center\">";
    echo "<thead class=\"wire1\">";
    echo "<tr>";
 $result = mysql_query("SHOW COLUMNS FROM $table");
 if (!$result) {
     echo 'Could not run query: ' . mysql_error();
     exit;
 }
 if (mysql_num_rows($result) > 0) {
     while ($row = mysql_fetch_assoc($result)) {
         print_r("<th>" . $row[Field] . "</th>");
     }
 }
  
 echo "</tr>";
 echo "</thead>";
 echo "<tfoot class=\"wire3\">";
 echo "<tr>";
  
 $qColumnNames = mysql_query("SHOW COLUMNS FROM $table",$connection) or die("mysql error");
 $numColumns = mysql_num_rows($qColumnNames);
 echo "<td colspan=\"$numColumns\">The data shown is approximate and subject to standard industry tolerances.</td>";
 echo "</tr>";
 echo "</tfoot>";
 echo "<tbody class=\"wire2\">";
 
  
 $data1 = mysql_query("SELECT * FROM $table", $connection);
 
 $num_rows = mysql_num_rows($data1);
  if (!$num_rows) {
     die("database query failed: " . mysql_error());
  }
while($num_rows >0){
    if($num_rows >=2){
    echo "<tr class=\"odd\">";
    $data2 = mysql_fetch_array($data1, MYSQL_ASSOC);
foreach ($data2 as $rowdata)
{
  print "<td> " . $rowdata . "</td>";
} 
 echo "</tr>";
$num_rows--;    
 
    echo "<tr>";
    $data2 = mysql_fetch_array($data1, MYSQL_ASSOC);
foreach ($data2 as $rowdata)
{
  print "<td> " . $rowdata . "</td>";
} 
 echo "</tr>";
$num_rows--;    
 
}
 
elseif($num_rows =1){
    echo "<tr class=\"odd\">";
    $data2 = mysql_fetch_array($data1, MYSQL_ASSOC);
foreach ($data2 as $rowdata)
{
  print "<td> " . $rowdata . "</td>";
} 
 echo "</tr>";
$num_rows--;    
}
}
  
 echo "</tr>";
 echo "</tbody>";
 echo "</table>";
 echo "<br /> <br />";
?>
 
 
To sum it up, I am trying to figure out what I need to add to my code above to make it so the Table won't show the DB field of 'ID'. as 'ID' will be the only column I know every table will have.

Thanks for any help. I am sure this is a simple problem for most. As I am new to PHP I have yet to learn it.

Re: How would I keep the ID field from showing in the table..

Posted: Sun May 31, 2009 3:34 pm
by mikemike
Hi there,

I'd try and steer away from using SHOW COLUMNS, but as I'm not sure what it is exactly you're doing I'll just leave you to it.

To hide your ID colum just do an inline if-statement. Below is a revision of your line 15 in your code:

Code: Select all

if($row[Field] != 'ID') { 
  print_r("<th>" . $row[Field] . "</th>"); 
}
This will only print fields not equal to 'ID'

Re: How would I keep the ID field from showing in the table..

Posted: Sun May 31, 2009 5:45 pm
by Herzlos
Thanks that works for the TH area, But after do some more searching the only way I seems to find to TD area would be use Show columns. I know there has to be a way to keep the TD area from displaying the ID fields as well without changing the code to the Show columns way I found doing a search.



Also, Just so I know, Why would you avoid using Show columns?



Thanks for all the help.

Re: How would I keep the ID field from showing in the table..

Posted: Sun May 31, 2009 6:39 pm
by mikemike
Because you run into problems like this. It's very very unlikely that you'd want to show the user all of the comments in a table. Do some basic PHP and MySQL tutorials and they should mostly use the format of running a single query to grab the specified columns and loop through, you can then show what you need to where you need it. The columns inside a TH are normally hard-coded.