php博客分页排名

时间:2016-01-02 13:53:02

标签: php mysql pagination

我仍然是PHP的新手。拜托,我需要帮助。我正在创建一个带有分页的博客页面,我希望排名从我的数据库表中的最后一个“id”开始而不是从第一个“id”开始,这样如果我向我的数据库添加一个新帖子,它将显示在我的博客页面顶部。这是我的编码:

    <?php


$per_page = 5;

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

if($page<=1)
$start = 0;
else
    $start = $page * $per_page - $per_page;

include 'connect.php';

$sql = "SELECT * FROM pagination";

$num_rows = mysql_num_rows(mysql_query($sql));

$num_pages = ceil($num_rows / $per_page);



$sql .= " LIMIT $start, $per_page";

$result = mysql_query($sql);
While($row = mysql_fetch_array($result)){
    echo $row['name']. "<br>";
}

$prev = $page - 1;
$next = $page + 1;

echo "<hr>";

if($prev > 0)
echo "<a href='?page=$prev'>Previous</a> ";

if($page < ceil($num_rows/$per_page))
echo " <a href='?page=$next'>Next</a>";


?>

与我的评论页面相同,我想要先显示最后一条评论。这是我的编码:

<?php

include 'connect.php';

function getuser($id, $field) {
    $query = mysql_query("SELECT $field FROM suggest WHERE id='$id'");
    $run = mysql_fetch_array($query);
    return $run[$field];
}

$readq = mysql_query("SELECT id FROM suggest");
while($run_p = mysql_fetch_array($readq)){
    $id = $run_p['id'];
    $name = getuser($id, 'name');
    $title = getuser($id, 'title');
    $post = getuser($id, 'post');

?>
<table width="60%">
<tr>
<td><b><font color="blue"><?php echo $title; ?></font><br><br><?php echo $post; ?><br>Suggestion From: <font color="blue"><?php echo $name; ?></font></b><hr width="50%"></td>
</tr>
</table>
<?php
}
?>

2 个答案:

答案 0 :(得分:0)

SELECT column_name, column_name
FROM table_name
ORDER BY column_name ASC|DESC

您可以在查询中使用ORDER BY column_name ASC或DESC以您希望的方式显示它。

答案 1 :(得分:0)

要获取最新记录,您需要在查询中使用ORDER BY DESC

您可以使用此查询:

SELECT id FROM suggest

就像那样:

SELECT id FROM suggest ORDER BY id DESC

旁注:我建议使用mysqli_ *或PDO,因为不推荐使用mysql_ *并在PHP 7中关闭。