-2

好的,首先让我从我的数据库(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);
}

?>
4

2 回答 2

0

您可以更清楚您的目标,但我将假设“拉标题”意味着将标题从数据库中取出。你没有提到桌子叫什么,所以我就叫它tablename

为此,sql是

select title from tablename
where user_id = something

您必须在 where 子句中输入您想使用的任何标准来选择标题。

于 2012-04-23T23:19:04.497 回答
0
$query = mysql_query("SELECT `content`,`title` FROM `page` WHERE `user_id` = ".$page);

这样,当您获取 $query 时,您也会有一个“标题”键。从现在开始,您决定如何使用它。

例如

while(($row = mysql_fetch_assoc($query)) !== false) {
    $ret[] = array($row['title'],$row['content']);
}

return $ret;

并在 user_list

foreach (fetch_users($page, 5) as $user) {
            echo "<h1>{$user[0]}</h1><p>{$user[1]}</p>";
        }
于 2012-04-23T23:18:15.860 回答