How can PHP be used to automatically generate tables for website design?
To automatically generate tables for website design using PHP, you can create a function that takes in parameters such as the number of rows, columns, and data to populate the table. Within the function, use loops to iterate through the rows and columns, generating the necessary HTML code for the table structure and data insertion.
<?php
function generateTable($rows, $cols, $data) {
echo '<table>';
for ($i = 0; $i < $rows; $i++) {
echo '<tr>';
for ($j = 0; $j < $cols; $j++) {
echo '<td>' . $data[$i][$j] . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
// Example usage
$data = array(
array('John', 'Doe', '30'),
array('Jane', 'Smith', '25'),
array('Mike', 'Johnson', '35')
);
generateTable(3, 3, $data);
?>