好的,首先让我从我的数据库(MySQL)中的内容开始:http: //gyazo.com/6e94124bf9387bfead4acb84be7a6452
到目前为止,我能做的是从我的数据库中提取内容。我还希望能够从数据库中提取标题,但不确定如何操作。
这是我的文件:
users_list.php:
<?php
include('CORE/init.inc.php');
$page = (isset($_GET['page'])) ? (int)$_GET['page'] : 1;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Pagination Test</title>
</head>
<body>
<div>
<?php
foreach (fetch_users($page, 5) as $user) {
echo "<p>{$user}</p>";
}
$total_pages = ceil(fetch_total_users() / 5);
for ($i = 1; $i <= $total_pages; ++$i) {
echo " <a href=\"?page={$i}\">{$i}</a> ";
}
?>
</div>
</body>
</html>
初始化.inc.php:
<?php
mysql_connect('localhost', 'root', '123456');
mysql_select_db('dbcontent');
$path = dirname(__FILE__);
include("{$path}/INC/users.inc.php");
?>
users.inc.php:
<?php
function fetch_users($page, $per_page) {
$start = (int)($page - 1) * $per_page;
$per_page = (int)$per_page;
$query = mysql_query("SELECT `content` FROM `page` WHERE `user_id` = ".$page);
while(($row = mysql_fetch_assoc($query)) !== false) {
$users[] = $row['content'];
}
return $users;
}
function fetch_total_users() {
$result = mysql_query("SELECT COUNT(`user_id`) FROM `page`");
return mysql_result($result, 0);
}
?>