<?php
define('ROWS_PER_PAGE', 10);
function display_table($fields, $data, $rows, $page){
if(isset($_GET['page'])){
$page = $_GET['page'];
} else {
$page = 1;
}
$firstRecord = ($page - 1) * ROWS_PER_PAGE;
$pageNumbers = ceil($rows / ROWS_PER_PAGE);
echo '<div class="table-responsive w-75 mx-auto py-3">
<table class="table table-dark table-bordered table-sm">
<thead>
<tr>';
foreach($fields as $key){
echo '<th class="py-2">' . $key . '</th>';
}
echo '</tr>
</thead>
</tbody>';
$keys = array_keys($fields);
$lastRecord = min($firstRecord + ROWS_PER_PAGE, $rows);
for($record = $firstRecord; $record < $lastRecord; $record++){
$row = $data[$record];
echo '<tr>';
for($recordCount = 0; $recordCount < count($keys); $recordCount++){
$column = $keys[$recordCount];
echo '<td class="py-2">' . $row[$column] . '</td>';
}
echo '</tr>';
}
echo '</tbody>
</table';
for($pages = 1; $pages <= $pageNumbers; $pages++){
echo '<a class="btn btn-dark mx-1" href=?page=' . $pages . '</a>';
}
}
$fields = array(
"id" => "Id",
"emailaddress" => "Email",
"firstname" => "First Name",
"lastname" => "Last Name",
"salesperson" => "Salesperson",
"phonenumber" => "Phone Number",
"extension" => "Extension",
"type" => "Type"
);
$data = array(
array('id' => 1, 'emailaddress' => 'user1@example.com', 'firstname' => 'User', 'lastname' => 'One', 'salesperson' => 'y', 'phonenumber' => '1235556789', 'extension' => '1', 'type' => 'a')
, array('id' => 2, 'emailaddress' => 'user2@example.com', 'firstname' => 'User', 'lastname' => 'Two', 'salesperson' => 'y', 'phonenumber' => '1235556789', 'extension' => '2', 'type' => 'a')
);
display_table($fields, $data, 2, 1);
preferences:
66.2 ms | 402 KiB | 5 Q