Jump to content

mysqli display field name / conditionally format table


TheAlmightyOS

Recommended Posts

Alright, I looked though the read me's, went over the FAQ's... Think it is time to post...

 

So, this lump is what I got from going though google and piecing together the bits that looked good. 

<?php
$db_host = "****";
$db_user = "****";
$db_pwd = "****";
$database = "****";
$table = "****";
$query = "SELECT * FROM {$table}";
$conn = mysqli_connect($db_host, $db_user, $db_pwd, $database);
$result = mysqli_query($conn,$query) or trigger_error($query . ' - 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>';





?>

The goal is to display the table and conditionally format the contents.  

 

Let's forget about the conditional part (thinking if/than but don't know how I'm going to do that yet) and focus on the key problem: displaying the table.

 

I have tested this code. It works...to a point. it displays the table and its contents but no headers. I know my issue is in line 20 and 22 but I can not for the life of me figure it out. If it is stupidly easy (for you) please remember that I am not a coder. At best I am a scripter when it comes to linux. I am over my head on this stuff.

 

Thank you all for any help you can give me

Edited by TheAlmightyOS
Link to comment
Share on other sites

Thank you! It worked! 

Here I thought it was a different problem entirely.

 

So the Table Headers are Solved.

 

My second question (I will make another thread if asked) is the conditional formatting. I know my way around an if/than statement but there is so much going on here in regards to the PHP, table code and variables that I do not know where to start it.  :confused:

 

There are a few goals here: to identify data that is website location (ex http://) and format it as a hyperlink and tag specific text with special formatting.  

 

I think I can figure out the if statement, but where it goes? On line 35 or line 36, I am uncertain. And would it be if($row or if($result?. Heading to w3c to do some more reading. Thank you for the help thus far!

Link to comment
Share on other sites

 

 

Heading to w3c to do some more reading. 

No no no! The first place you should look is php.net and read the documentation there.

 

 

 

I think I can figure out the if statement, but where it goes? On line 35 or line 36,

Your if statement will go inside the while loop. $row will be an array containing the data for the current row in the result set.

 

You can use print_r to see what data is returned for each row. Example

printf('<pre>%s</pre>', print_r($row, 1));
Link to comment
Share on other sites

I use this to print query results into a table with headings (useful for testing queries)

echo query2HTMLtable($conn, "SELECT * FROM tablename");

function query2HTMLtable($db, $sql)
{
    $output = "<table border='1' cellpadding='2' style='border-collapse:collapse'>\n";
        // Query the database
    $result = $db->query($sql);
        // check for errors
    if (!$result) return ("$db->error <pre>$sql</pre>");
    if ($result->num_rows == 0) return "No matching records";
        // get the first row and display headings
    $row = $result->fetch_assoc();
    $output .= "<tr><th>" . join('</th><th>', array_keys($row)) . "</th></tr>\n";
    
        // display the data
    do {
       $output .= "<tr><td>" . join('</td><td>', $row) . "</td></tr>\n"; 
    } while ($row = $result->fetch_assoc());
    $output .= "</table>\n";
    return $output;
}

Link to comment
Share on other sites

 

No no no! The first place you should look is php.net and read the documentation there.

Alright. I have been going back and forth between the two. Will focus more on php.net

 

 

I use this to print query results into a table with headings (useful for testing queries)

<code>

Might try that out. Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.