我目前正在创建一个包含大量内容的小应用程序,我希望以有组织的方式显示内容,并且我可以看到这是使用分页的最佳方式。我已经看了一下bootstrap文档然而我无法理解我将如何使用它,因为我以前从未使用它。
型号:
public function showFilms($UserID){
//declare new film array
$film = array();
//SQL Query Selecting equivalent information to be displayed
$sqlQuery = 'SELECT FilmID, Title, Director,YearofRelease, Genre, Image, MainActor, MainActor1, MainActor2, Synopsis FROM films WHERE User ="' . $UserID . '" ORDER BY Title DESC';
// prepare a PDO statement
$statement = $this->_dbHandle->prepare($sqlQuery);
// Execute PDO statement
$statement->execute();
// While loop fetches each row matching query
while ($row = $statement->fetch()) {
$film[] = array('FilmID' => $row['FilmID'],
'Title' => $row['Title'],
'User' => $UserID,
'Director' => $row['Director'],
'YearofRelease' => $row['YearofRelease'],
'Genre' => $row['Genre'],
'Image' => $row['Image'],
'MainActor' => $row['MainActor'],
'MainActor1' => $row['MainActor1'],
'MainActor2' => $row['MainActor2'],
'Synopsis'=> $row['Synopsis']
);
}
//returning film array
return $film;
}
查看:
<div id="FilmArea">
<!-- Count for Films -->
<?php
if (count($film)) {
?>
<br>
<!-- Displaying each entry using count of hidden list value of id of entry -->
<?php foreach ($film as $list): ?>
<!-- Panel Start -->
<div class="panel panel-info">
<!-- Panel Header -->
<div class="panel-heading">
<h3 class="panel-title" style="font-family: sans-serif; font-size: 20px; color: black; ">
<?= ( $list['Title']) ?> <small> Directed By : <?= ( $list['Director']) ?></small>
</h3>
</div>
<!-- Panel Body -->
<div class="panel-body">
<div id='dvdHolder'>
<?= ($list['Image'] <> "" ? "<img style='max-width:200px; max-height:200px;' src='Images/{$list['Image']}'/>" : "") ?>
</div>
<div id='dvdInfo'>
<p style='font-weight: bold;'> Year Released : <?= ( $list['YearofRelease']) ?></p>
<p style='font-weight: bold;'> Film Genre : <?= ($list['Genre'] ) ?></p>
<p style="font-weight: bold;"> Starring : <?= ($list['MainActor']) ?> , <?= ($list['MainActor1']) ?> , <?= ($list['MainActor2']) ?> </p>
<p style='font-weight: bold;'> Synopsis : <?= ($list['Synopsis'] ) ?></p>
</div>
</div>
</div>
<!-- End of Foreach Statement for Entry -->
<?php endforeach; ?>
<!-- Else show this message if user has no entries -->
<?php
}else {
?>
<p><b>No Films Added yet!</b></p>
<?php } ?>
</div>
控制器:
<?php
session_start();
require_once('Model/UserDataSet.php');
require_once('Model/FilmDataSet.php');
$view = new stdClass();
$view->pageTitle = 'My Vault';
$aa = new FilmDataSet();
$film = array();
$film = $aa->showFilms($_SESSION['Email']);
require_once('View/home.phtml');
有关如何在我的应用程序中实现分页的任何帮助或信息都会很棒。
答案 0 :(得分:0)
首先,您必须编辑查询以获取每页要显示的记录数。
$sqlQuery = 'SELECT FilmID, Title, Director,YearofRelease, Genre, Image, MainActor, MainActor1, MainActor2, Synopsis FROM films WHERE User ="' . $UserID . '" ORDER BY Title DESC LIMIT '.$perPage.' OFFSET '.(($page - 1)*$perPage).'';
要呈现页数,您必须计算总记录数,以了解您拥有的页数。当您渲染页数时,Bootstrap必须添加指向页数的链接,例如:
<li><a href="localhost:3000/films?page=1">1</a></li>
<li><a href="localhost:3000/films?page=2">2</a></li>
<li><a href="localhost:3000/films?page=3">3</a></li>
控制器在这种情况下你必须捕获页面:
$page = htmlspecialchars($_GET["page"]);
$film = $aa->showFilms($_SESSION['Email'], $page);
您还可以查看本教程,实现了一种用类分页的方法:
http://code.tutsplus.com/tutorials/how-to-paginate-data-with-php--net-2928
最后,我建议您使用MVC框架,这可以帮助您更多地使您的代码更有条理,安全功能并具有分页选项。