gpt4 book ai didi

php博客分页排名

转载 作者:行者123 更新时间:2023-11-29 21:40:36 24 4
gpt4 key购买 nike

我还是一个 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
}
?>

最佳答案

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

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

关于php博客分页排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com