Jump to content

mysqli display field name /conditionally format table.


TheAlmightyOS
 Share

Recommended Posts

I want to say I am almost there. I can connect to the DB, I have a query stored and it runs fine and I can output the result into a table. But this is where I have hit a wall. Code Below:

$conn = mysqli_connect($db_host, $db_user, $db_pwd, $database);$result = mysqli_query($conn,$query) or trigger_error($querry . ' - has encountered an error at:<br />' . mysqli_error($conn));$fields = mysqli_fetch_fields($conn);$field_count = mysqli_num_fields($conn);echo '<table border="1" style="width:100%">' . "n" . '<tr>';$i = 0;foreach($fields as $field){	if(++$i == 1) {		echo '<th colspan="' . $field_count . '">' . $field->table . '</th></tr><tr>';	}echo "n" . '<th>' . $field-name . '</th>';}echo '</tr>';while($row = mysqli_fetch_row($result)) {	echo '<tr><td>' . implode('</td><td>' , $row) . '</td></tr>';}echo '</table>';

This gives me a nice table but it needs some formatting. Will do that with CSS later. My core concern is this: everything prints out but the column titles. I went looking through the mysqli_fetch functions to see if there was a fetch header or something similar but no dice. The mysql API had a field_name function. Does mysqli have something similar?

 

 

The second part of my question I might be able to figure out with enough poking around. I want the script to look at the value of the cell and if it meets the criteria it is edited in a specific way. For instance, if the contents of a cell starts with "HTTP" I want to put link tags around it. Is it possible?

 

 

 

Link to comment
Share on other sites

Your "fetch header" is called a Key if I understand you correctly:

 

http://www.w3schools.com/php/php_ref_array.asp

 

look for descriptions dealing with Keys..

 

Your second question will require an if statement:

 

http://www.w3schools.com/php/php_if_else.asp

Edited by niche
Link to comment
Share on other sites

My core concern is this: everything prints out but the column titles.

You're trying to do that on line 23 but the syntax isn't right.

For instance, if the contents of a cell starts with "HTTP" I want to put link tags around it. Is it possible?

You can use strpos to check if a string exists in another one.
Link to comment
Share on other sites

You're trying to do that on line 23 but the syntax isn't right.

 

I have been staring at that line for hours and googling for an answer. I know I screwed up that line but I can not figure out where. I do know it is something stupid and it is going to smack me in the face hard when I find it.

 

BTW, I am VERY new to php and programming in general. I'm a server admin, not a programmer. Don't laugh too hard.

Link to comment
Share on other sites

That should be fine. Writing $field-name is telling PHP to subtract the constant name from the variable $field. You're trying to refer to a property of an object.http://php.net/manual/en/language.oop5.properties.phpYou can use print_r if you want to see what mysqli_fetch_fields returned:print_r($fields);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...