Jump to content
Sign in to follow this  
TheAlmightyOS

mysqli display field name /conditionally format table.

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?

 

 

 

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Compare it with line 21 where you're getting the table name.

I thought it might be that I missed the > in $field-name but that does not work. Changing it to $field->name gives me the same result.

 

I don't even know if I am going about this the right way.

Edited by TheAlmightyOS

Share this post


Link to post
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);

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...