我在 pagination.php 文件中使用了这个分页代码,我已将其包含在主索引文件中。
<?php
$allItems = [];
for ($i=0; $i<25; $i++)
{
$allItems[] = $i;
}
$totalrows = count($allItems);
$perpage = 10;
$totalpages = ceil($totalrows / $perpage); // total number of pages
if ($totalpages < 1)
{
$totalpages = 1;
}
$page = 1;
if (isset($_GET['page']))
{
$page = (int)$_GET['page'];
}
if ($page < 1)
{
$page = 1;
}
elseif ($page > $totalpages)
{
$page = $totalpages; // set page to totalpages page number
}
$offset = ($page - 1) * $perpage;
if ($offset < 0)
{
$offset = 0;
}
$allItems = array_slice($allItems, $offset, $perpage);
$pagination = "";
if ($totalpages != 1)
{
$pagination .= '<ul class="pagination">';
// first and previous link
if ($page > 1)
{
$previous = $page - 1;
$pagination .= '<li class="page-item"><a class="page-link" href="' . $_SERVER['PHP_SELF'] . '?page=1" class="btn btn-default">First</a></li>'; // goto first page
$pagination .= '<li class="page-item"><a class="page-link" href="' . $_SERVER['PHP_SELF'] . '?page=' . $previous . '" class="btn btn-default">Previous</a></li>';
}
// display page numbers
for ($i = 1 ; $i <= $totalpages; $i++)
{
if ($i != $page)
{
$pagination .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?page='. $i .'" class="btn btn-default">'.$i.'</a></li> ';
}
else
{
$pagination .= '<li class="page-item"><button type="button" class="btn btn-default">' . $i . '</button></li>';
}
}
// last and next link
if ($page != $totalpages)
{
$next = $page + 1;
$pagination .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?page='. $next . '" class="btn btn-default">Next</a></li>';
$pagination .= '<li class="page-item"><a class="page-link" href="'.$_SERVER['PHP_SELF'].'?page='. $totalpages . '" class="btn btn-default">Last</a></li>'; // goto totalpages page
}
$pagination .= '</ul>'; // close ul
}
目前它只显示页码和First / Last,Next / Previous链接,但不是当我转到相应页面时显示的实际数组数据。
请帮助我,如何在以下代码中显示此数组数据。
index.php 档案
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<tbody>
<tr>
<?php
// <td> how should I display here in table </td>
?>
</tr>
</tbody>
</table>
<div><?php echo $pagination; ?></div>
</div>
答案 0 :(得分:0)
您只需将$allItems
值添加到foreach
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover">
<tbody>
<?php
$str = "";
foreach ($allItems as $key => $value) {
$str .= "<tr><td>$value</td></tr>";
}
echo $str;
?>
</tbody>
</table>
<div><?php echo $pagination; ?></div>
</div>