Hi guys! I don't know if i post it in the right forum page, but I would like to ask if it's possible to put color on the last row of your table (LAST ROW ONLY). the data is from SQL. I would appericiate any references or links, snippets, anything! Thanks! :)

Dani AI

Generated

Two practical ways to color only the last row of an HTML table fed from SQL: let the browser handle it with a CSS pseudo-class, or mark the final record when building the HTML on the server (or with JavaScript). pointed at the CSS route for a quick solution; suggested identifying the last DB record; linked a PHP walkthrough that shows adding classes while looping. Pick the approach that matches your browser-support needs and whether you need “last in the result set” or “last in the whole table.”

If you want a CSS-first solution but with a reliable fallback, give the last row a class and style that class. This avoids differences caused by thead/tfoot, multiple tbody elements, or older browsers that lack full CSS3 selector support (see the MDN docs and browser support tables linked below).

.last-row {
  background: #f7fff0;
}

Server-side marking is robust for paginated or sorted results: find the final record (SQL Server uses TOP 1 when ordering by the key descending), then while rendering rows in PHP add a class="last-row" only on the last iteration. Example workflow: fetch rows into an array, compute the last index, and emit the class for that index only. That gives predictable results regardless of client capabilities.

Troubleshooting tips: ensure rows are inside tbody (scope selectors there for accuracy), check for tfoot/thead that might change which tr is last, verify CSS specificity if another rule overrides the background, and remember “last in the page” is different from “last in the table in the DB” when using pagination. For details about the CSS selector and browser support see the MDN reference (https://developer.mozilla.org/en-US/docs/Web/CSS/:last-child) and compatibility info (https://caniuse.com/css-sel3). For SQL Server usage see Microsoft Docs (https://learn.microsoft.com/en-us/sql/t-sql/queries/top-transact-sql?view=sql-server-ver16).

Recommended Answers

All 3 Replies

In your table, set a field with primary key and auto increment options , ie ; Row ID;

Then ; in your query , set

SELECT  FieldNameId   FROM   your_Table ORDER BY FieldNameId DESC LIMIT 0, 1;

the result of this query will give last row Id

Member Avatar for Member #949455

Read this

It will show you how to put a color on any row including the last row.

Member Avatar for Member #120589
tr:last-child
{
background: green;
}

should do it?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.