Integrating Internal CSS Stylesheets into PHP Output
You have a set of CSS rules defining the look and feel of your data tables, including specific margins for the <table> element, font styling for headers (<th>), and border configurations for cells (<td>). You also have a PHP block that iterates through MySQL results to generate HTML. The core challenge here isn't about "PHP CSS" as a distinct languageâthere is noneâbut rather understanding how server-side logic interacts with client-side styling when generating dynamic content.
In your current setup, the <style> block is valid HTML, but its placement determines whether it applies globally or only to specific tables. If you place that <style> tag inside every PHP-generated page, it works, but it creates maintenance overhead. A more scalable approach separates the presentation layer from the logic layer.
To apply your styles effectively without cluttering your PHP files, you should move the CSS into an external file (e.g., styles.css) and link to it in your <head> section. This is a standard practice in modern web development. If you must keep the CSS internalâfor instance, in a single-page tool or a legacy templateâyou can embed the block at the top of your HTML output.
Here is how you would structure the combined file. First, declare the styles:
<style type="text/css">
table {
margin: 8px;
}
th {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
background: #666;
color: #FFF;
padding: 2px 6px;
border-collapse: separate;
border: 1px solid #000;
}
td {
font-family: Arial, Helvetica, sans-serif;
font-size: .7em;
border: 1px solid #DDD;
}
</style>
Next comes the PHP logic. Your original code uses mysql_fetch_row, which belongs to the deprecated mysql_* extension. That extension has been removed in PHP 7.0 and is no longer safe to use due to SQL injection vulnerabilities. You must migrate this to either mysqli or PDO. Furthermore, your HTML generation relies heavily on string concatenation (echo "<..."), which is prone to errors when handling user data.
Here is a safer, modern implementation using mysqli and prepared statements logic (though the snippet focuses on structure):
<?php
// Assume $result is a mysqli_result or PDOStatement object from your query
echo "<table>";
echo "<tr><th>ID</th><th>hashtag</th></tr>";
// Check if the result set has rows to avoid empty output
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
// Replace with fetch_row() logic if strictly needed, but fetch_assoc is safer for named columns
echo "<tr onmouseover=\"hilite(this)\" onmouseout=\"lowlite(this)\">";
// Ensure data is escaped to prevent XSS attacks
echo "<td>" . htmlspecialchars($row['ID']) . "</td>";
echo "<td>" . htmlspecialchars($row['hashtag']) . "</td>";
echo "</tr>\n";
}
}
echo "</table>";
?>
There are a few specific technical details in your original code that require attention. The onmouseover and onmouseout attributes trigger JavaScript functions named hilite and lowlite. These functions must exist in the same document, typically defined within a <script> tag before they are called. If these functions are missing or misspelled, the table will not highlight on hover, even though the CSS is present.
Additionally, your CSS uses border-collapse: separate for the header row but td has a border defined. When borders are separate, browsers may render double lines between headers and body cells unless you explicitly set padding or margins to hide the gap. Your current CSS sets padding: 2px 6px on th, which helps visually separate them, but ensuring consistent spacing often requires tweaking the border-spacing property if you switch to a collapsed border model later.
Finally, consider where your PHP runs. If you are building a dynamic site, injecting styles directly into every response is inefficient. However, if this is a standalone reporting script, embedding the <style> block at the very top of your output ensures the browser renders it immediately before parsing the table tags. Just remember that internal stylesheets have no priority over external ones in terms of browser caching; they are loaded fresh every time the page is requested.
