So having a decent understanding of programming but new to PHP I'm stuck on this somewhat simple problem. I have column titles being listed from my database as follows:

//This section gathers the field names
// and puts them in the first row of the table
$sql = "SELECT * FROM `vendors` ORDER BY `Company/Name` ASC";
$query = mysql_query($sql);
$fields_nums = mysql_num_fields($query); // Code, Account Info, Ph#, User, PW, Web, Active
		$fieldname = array();
		for($i=0; $i<$fields_nums; $i++)
			{
			$fields = mysql_fetch_field($query);
			$fieldname[$i] = $fields->name;
			echo "<td id='headers'>".$fieldname[$i]."</td>";
			}

Now how do I hide the ID column or column[x] if you will. Cause on other pages I intended to have extra fields as part of the table but I won't want them displayed on the page. Thanks in advance for any help you can offer on this.

the easiest approach would be NOT to use SELECT * ... . Instead, mention explicitly which fields you want AND make sure the ones you want to hide appear first: SELECT ID, Code, Account Info, Ph#, User, PW, Web, Active That way if you wanted to "hide" bod ID and Code, then instead of for($i=0;...) you would use for($i=[B]2[/B];...) NOTE: if you do not want/need ID at all, then don't SELECT it. Simply list the columns/fields that you DO need/want.

So I believe I understand your statement but with it I still have a question. Why can't I say i=1 now (skipping 0)? I thought by calling the way I was it was doing in short what you did by calling all of them.

And

Why do you include ID at all in the query then, can't you just leave it out of the query? What if ID wasn't the first field in the table? Would I just be calling it first anyways?

Thanks so much for your help with this.

What if ID wasn't the first field in the table?

The order in which the field is defined in the TABLE does NOT matter. What matters is the order in which you list them when execute your query.

If you execute: SELECT `Code`, `Account Info`,`ID`... then ID will be the THIRD column in the result set (index=2), not the first column (index=0).

Why can't I say i=1 now (skipping 0)? I thought by calling the way I was it was doing in short what you did by calling all of them.

You can do that BUT that relies on the ID ALWAYS being defined first in your table. There's no way for me to know for a fact that ID is the first field in your table when all you have shown is SELECT * . However, if you had shown that you were executing SELECT ID,.... then I would have suggested $i=1

I'm going to try your method right now of listing them individually, but right now when ID indexed first I tried $i=0 and instead of removing that field it removes the last field and leaves me one header short.

So I tried your method of listing them individually, but left the loop as is since now the query itself has been adjusted; Great job that works! (sort of). Problem now would be that without the ID field queried I can't call a single-record for edit by ID, right?

Problem now would be that without the ID field queried I can't call a single-record for edit by ID, right?

correct. If all you want is the field->name (as opposed to other field metadata), then the easiest approach would be to get a row of data and get name from that row. This is probably what you are after:

<?php
//This section gathers the field names
// and puts them in the first row of the table
$sql = "SELECT `ID`, `Code`, `Account Info`, `Ph#`, `User`, `PW`, `Web`, `Active` FROM `vendors` ORDER BY `Company/Name` ASC";
$query = mysql_query($sql) or die(mysql_error());

/*
lookup these functions in the PHP manual if you are not familiar with them:
	mysql_num_rows()
	mysql_fetch_assoc()
	implode()
	array_keys()
	array_slice()
*/
if( mysql_num_rows($query) > 0)
{
	$row=mysql_fetch_assoc($query);
	
	echo '<table>';
	echo '<tr><th>'.array_keys( array_slice($row,1) ).'</th></tr>';
	do{
		//save the value of id
		$id=$row['ID'];
		
		//"erase" ID from $row
		unset($row['ID']);
		
		//implode will "join" all the $row elements using '</td><td>' as the 'glue'
		echo '<tr><td>'.implode('</td><td>',$row).'</td><td><a href="?id='.$id.'">[Edit]</a></td></tr>';
	}while($row=mysql_fetch_assoc($query));
} 
else
{
	echo 'No Records found';
}
?>
commented: Great help, patient enough to help me figure out a complicated issue +1

Looks like your code effectively accomplished what I was looking for. It messed up my formatting but I think I can fix that. I'm researching those functions and studying your script, I'll get back with you soon. Thanks for the help. Do you think you can help with my other post I posted just before this one?

. It messed up my formatting

I forgot the closing table tag.

Do you think you can help with my other post I posted just before this one?

I really don't feel like deciphering someone else's code. I don't know what is in the js file but it should be difficult to achieve what you want if you make ajax requests.

I've never done anything with AJAX before. I made some progress with it, later tonight I might reply to that thread with an update of what I have now to see if that helps.

I will do as much work as I can to show the effort and make things clear when I post, I'm just hoping for wisdom from the community cause I don't know much about PHP just yet. I'm getting your code to work well now, just had to change some things so that I still have my headers and the formatting that I had. Making much more sense though so thank you. I've got about 5-6 more pages needing the same sort of work so this help goes a long way.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.