显示数据库中的条目,从最新到最旧列出它们并在一定数量后停止?

时间:2012-07-25 19:30:26

标签: php mysql database post blogs

我的数据库中有以下表格:

  1. ID
  2. 标题
  3. 航向
  4. 含量
  5. 日期
  6. 标记
  7. 用户名
  8. 反感
  9. 评论
  10. 我已将数据库连接起来,以便显示行中的内容,但我不知道如何制作它以便每次创建新条目时它都会生成并列出“post block”。我还希望将它们从最新的帖子列到最旧的帖子,同时将帖子限制为每页10-20个。

    <?php
    
    $connect = mysql_connect('localhost','root','','andrewryan') or die('error connection');
    
    mysql_select_db('andrewryan');
    
    $query = "Select * FROM entry";
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    
    $username = $row['username'];
    $title = $row['title'];
    $heading = $row['heading'];
    $content = $row['content'];
    $tags = $row['tags'];
    $like = $row['like'];
    $dislike = $row['dislike'];
    $comment = $row['comment'];
    $date = $row['date'];
    ?>
    

    我还有一个只有一个html块的页面。哪个是post框架,所有变量都在需要的地方给出。如果有帮助,可以在此链接中找到有关我的目的的任何参考:HERE

2 个答案:

答案 0 :(得分:1)

  1. 选择您有兴趣获取的所有字段。
  2. 使用DATE_FORMAT函数格式化日期字段并分配给新别名。我将其命名为ndate。
    1. 然后ORDER BY ndate
  3. SELECT用户名,标题,标题,内容,标签,喜欢,不喜欢,评论,DATE_FORMAT(日期,'%d-%M-%Y')AS ndate FROM entry ORDER BY ndate

    将日期作为$ row ['ndate']

    获取

答案 1 :(得分:1)

至于查询:

// Tildes on date since it is a keyword and some databases will error out without them
// Order by date is if you are using a compatible storing: unix timestamp or DateTime field
$query = "Select * FROM entry ORDER BY `date` DESC";
$result = mysql_query($query);
$rowCount = 0;
while($row = mysql_fetch_array($result)) {
    $username[$rowCount] = $row['username'];
    $title[$rowCount] = $row['title'];
    $heading[$rowCount] = $row['heading'];
    $content[$rowCount] = $row['content'];
    $tags[$rowCount] = $row['tags'];
    $like[$rowCount] = $row['like'];
    $dislike[$rowCount] = $row['dislike'];
    $comment[$rowCount] = $row['comment'];
    $date[$rowCount] = $row['date'];
    $rowCount++;
}

这就是显示帖子:

// limit is used to show the max posts per page.
$limit = 20;

//The following for is for paging
for($i = 1; $i <= $numofpages; $i++){
    if($i == $page){
        echo $i . " ";
    }else{
        echo "<a href='page.php?page=" . $i . "'>" . $i . "</a> ";
    }
}

// The following is to show your post blocks
for($j = 0; $j < $limit; $j++){
    //This will give you the appropriate post for the page
    $temp = $j + (($page * $limit) - $limit);
    // Your code to show your post blocks
    echo $username[$temp]; //For example, just format for your site layout.
}
相关问题