Hello all,
Is there any way that I can count the number of lines in one column in a table? Such as I want to count all the names in this column on First name:
How would I do so?
Thanks in advance! :)
Hello all,
Is there any way that I can count the number of lines in one column in a table? Such as I want to count all the names in this column on First name:
How would I do so?
Thanks in advance! :)
The phrase "count the number of lines" is ambiguous: it can mean the number of rows (entries) in the First name column, the number of non-empty cells in that column, or the number of visual lines inside a single table cell (wraps or <br>s). asked for a count of names; pointed toward server-side PHP and suggested a client-side approach. Both are valid depending on where the authoritative data lives.
When the data comes from a database, counting at the source is simplest and most accurate — use SQL aggregation (COUNT) rather than parsing HTML afterwards (MySQL COUNT docs). If HTML must be parsed on the server, use a proper parser (PHP's DOMDocument) instead of string functions to avoid fragile edge cases (PHP DOMDocument).
For browser-side counting when the page is already rendered, select the column cells and filter out empties using DOM APIs rather than fragile text hacks. Example (vanilla JS):
const cells = document.querySelectorAll('#myTable tbody tr td:nth-child(2)');
const count = Array.from(cells).filter(td => td.textContent.trim() !== '').length; If the goal is to count visual lines inside one cell (wrapped lines), measure rendered line boxes with Range.getClientRects — each rect usually corresponds to a visual line. That method is more expensive and affected by CSS, inline elements and transforms, so use it only when necessary (Range.getClientRects). Also account for header rows, hidden rows, colspan/rowspan and duplicate or whitespace-only cells when deciding whether to count at source or in the DOM.
Jump to Post— MagicMedia 10…Hello all,
Is there any way that I can count the number of lines in one column in a table? Such as I want to count all the names in this column on First name:How would I do so?
Thanks in advance! :)
Hello all,
Is there any way that I can count the number of lines in one column in a table? Such as I want to count all the names in this column on First name:How would I do so?
Thanks in advance! :)
Are you counting rows or columns? Either way, you might want to use PHP.
Count rows:
substr_count($table, '<tr>'); Count columns:
$row = preg_replace('/.*\<\tr>(.*?)\<\/tr>.*/','$1',$table);
substr_count($row, '<td>'); Could also use jquery
$('#tableID tr').size()
or
$('#tableID tr').length()
or
$('#tableID tr').count() We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.