Displaying Column Headings from a PHP Query

Hello. I currently have a table displaying results when users click on submit button. I want to be able to do two things.
(1) add the column headings to the table and (2) hide the field that displays the record number. Are there easy functions to do this? I tried looking in my book and on the web but cannot see code that would exactly fit in my code. The code I have is as follows. Thanks for any help or guidance you can offer.

$query = “SELECT * FROM clients WHERE Category=”{$_POST[‘Category’]}"";
$result = mysql_query($query,$cxn);
$num_rows = mysql_num_rows($result);
print “There are $num_rows records.

print “


while ($get_info = mysql_fetch_row($result)){
print “

foreach ($get_info as $field)
print “tn”;
print “n”;
print “

As for the “Header” row, you can just add that segment at the top of your table before you go into the individual

's and 's of the table with the data.

Next, to only display the data you want (i.e., not the ID field), then you can either NOT use a foreach statement to loop through each element of the query, OR you can modify the query to return ONLY the data you want to display.

$query = “SELECT field1, field2, field3,… fieldN, FROM clients WHERE Category=”{$_POST[‘Category’]}"";
where fieldX represents the actual field names form the table CLIENTS that you want to grab.

Thank you for getting back to me. Your reply raises other questions since I am still very new to PHP.

The reason I used the foreach statement was because I figured out that if I use that, it will leave out a column that is null for the entire category. For instance, one category is going to be restauarants. If a user selects that, a column called cuisine will be dispalyed. Another category is Houses of Worship, if a user selects that, Denomination will be displayed. Now obviously, I do not want Denomination listed when restaurants is selected and likewise, I do not want cuisine if Houses of Worship is selected. When I used the above code, it left out blank columns but the trade off was, I had the ID column and the Category Column displayed.

Also, I wasn’t sure how to have the columns appear that way with headers and omit the blank columns. I figured I could hard code them in but then again I thought I would be stuck with having the Denomination column appear under restaurants. Is there a way to suppress blank columns, Columns I don’t want the user to see and place the column headings on with the script commands I am using or do I have to get much more detailed? LIke I said I am very new to PHP and kind of blindly stumbling through this.

Thank you.

Sponsor our Newsletter | Privacy Policy | Terms of Service