To allow for the creation of rows with horizontal bars in the HTML table, you can add another parameter to the generateHTMLTable
function to specify which rows should have horizontal bars and then add CSS classes accordingly. Here's the modified function:
<?php
function generateHTMLTable($data, $tableClass = '', $headerRowClass = '', $dataRowClass = '', $barRowIndices = []) {
if (empty($data)) {
return ''; // Return an empty string if the data array is empty
}
$html = '<table';
// Add a class to the table if provided
if (!empty($tableClass)) {
$html .= ' class="' . htmlspecialchars($tableClass) . '"';
}
$html .= '>';
// Create the table header row
$html .= '<thead><tr';
// Add a class to the header row if provided
if (!empty($headerRowClass)) {
$html .= ' class="' . htmlspecialchars($headerRowClass) . '"';
}
$html .= '>';
foreach (array_keys($data[0]) as $header) {
$html .= '<th>' . htmlspecialchars($header) . '</th>';
}
$html .= '</tr></thead>';
// Create the table body
$html .= '<tbody>';
foreach ($data as $index => $row) {
$html .= '<tr';
// Add a class to the data row if provided
if (!empty($dataRowClass)) {
$html .= ' class="' . htmlspecialchars($dataRowClass) . '"';
}
// Check if this row should have a horizontal bar
if (in_array($index, $barRowIndices)) {
$html .= ' style="border-bottom: 2px solid #000;"'; // Add a bottom border
}
$html .= '>';
foreach ($row as $cell) {
$html .= '<td>' . htmlspecialchars($cell) . '</td>';
}
$html .= '</tr>';
}
$html .= '</tbody>';
$html .= '</table>';
return $html;
}
// Example usage:
$data = [
['Name', 'Age', 'Country'],
['John', 30, 'USA'],
['Alice', 25, 'Canada'],
['Bob', 35, 'UK'],
];
// Apply classes and add horizontal bars to specific rows
$tableClass = 'custom-table';
$headerRowClass = 'header-row';
$dataRowClass = 'data-row';
$barRowIndices = [1]; // Rows with horizontal bars (indexing starts from 0)
echo generateHTMLTable($data, $tableClass, $headerRowClass, $dataRowClass, $barRowIndices);
?>
In this updated function, you can provide an array $barRowIndices
containing the indices of the rows that should have horizontal bars. The function will apply a bottom border to these rows to create the horizontal bars.