TheAlmightyOS Posted November 18, 2014 Share Posted November 18, 2014 (edited) 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 November 18, 2014 by TheAlmightyOS Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/ Share on other sites More sharing options...
Ch0cu3r Posted November 18, 2014 Share Posted November 18, 2014 mysqli_fetch_fields and mysqli_num_fields require the mysqli result object ($result) returned by mysqli_query not the mysqli object ($conn) Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/#findComment-1496956 Share on other sites More sharing options...
TheAlmightyOS Posted November 18, 2014 Author Share Posted November 18, 2014 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. 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! Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/#findComment-1496963 Share on other sites More sharing options...
Ch0cu3r Posted November 18, 2014 Share Posted November 18, 2014 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)); Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/#findComment-1496964 Share on other sites More sharing options...
Barand Posted November 18, 2014 Share Posted November 18, 2014 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; } Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/#findComment-1496970 Share on other sites More sharing options...
TheAlmightyOS Posted November 19, 2014 Author Share Posted November 19, 2014 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! Quote Link to comment https://forums.phpfreaks.com/topic/292555-mysqli-display-field-name-conditionally-format-table/#findComment-1497025 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.