drhzn3911 2016-01-02 13:53
浏览 20

php博客分页排名

i am still a novice in PHP. please i need help. i am creating a blog page with pagination and i want the ranking to descend start from the last "id" from my database table not from the first "id" so that if i add a new post to my database, it will display at the top of my blog page. here is my coding:

    <?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>";


?>

and same with my comment page, i want the last comment to display first. here is my coding:

<?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条回答 默认 最新

  • dongshang3309 2016-01-02 13:59
    关注

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

    you can use the ORDER BY column_name ASC or DESC in you query to display it the ways you want.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用