下面我的代码基本上从数据库中获取了一堆电影,并将年份设置为数组键,然后每年回收每部电影。这很酷,虽然我的结果很长,需要分页。我无法找到任何有助于我的情况的例子,因为php中的大多数分页都适用于while循环 - 感谢任何帮助或反馈。
我不介意它的jquery或php还是两者的组合。
<?php
require_once('Connections/timeline.php');
mysql_select_db($database_timeline, $timeline);
// select all the events from the database ordered by date:
$res = mysql_query("SELECT * FROM film2 ORDER BY `year` ASC");
$filmArray = null;
while($row_res = mysql_fetch_assoc($res)) {
$year = $row_res['year'];
$filmArray[$year][] = $row_res;
}
//for each year in the film array echo out each film within that year
foreach($filmArray as $year => $films) {
echo $year;
echo '<br />';
foreach($films as $film) {
echo $film['event'];
echo '<br />';
echo $film['name'];
echo '<br />';
}
}
?>
答案 0 :(得分:0)
您需要拥有GET参数,例如page。 网址:localhost / film.php?page = 1
然后
if (isset($_GET['page']))
$page = $_GET['page'];
else
$page = 1;
$entriesPerPage = 20;
$limitFrom = $page - 1;
$limitTill = $limitFrom + $entriesPerPage;
然后你的SQL查询:
$res = mysql_query("SELECT * FROM film2 ORDER BY `year` ASC LIMIT $limitFrom, $limitTill");
最后,创建HTML链接。
<a href="film.php?page=<?=$page-1?>">Previous</a>
<a href="film.php?page=<?=$page+1?>">Next</a>