Cakephp按日期顺序创建

时间:2017-04-16 20:15:48

标签: php mysql

如何让我的帖子按创建日期排序,而不是在创建日期时进行排序?所以,从新到旧。自动。

<?php

$db = new PDO('mysql:dbname=forum;host=localhost', 'root', '');

// User input
$page = isset($_GET['page']) ? (int)$_GET['page'] :1;
$perPage = isset($_GET['per-page']) && $_GET['per-page'] <= 50 ? (int)$_GET['per-page'] : 5;

// Positioning
$start = ($page > 1) ? ($page * $perPage) - $perPage : 0;

// Query
$posts = $db->prepare(
    "
    SELECT SQL_CALC_FOUND_ROWS id, title, photo_dir, photo, body, created
    FROM posts
    LIMIT {$start}, {$perPage}
    "
);

$posts->execute();

$posts = $posts->fetchAll(PDO::FETCH_ASSOC);

// Pages
$total = $db->query("SELECT FOUND_ROWS() as total")->fetch()['total'];
$pages = ceil($total / $perPage);

?>

<body>
    <div id="cont">
            <?php foreach($posts as $post): ?>
                <h2 id="naslov"><?php echo $post['title']; ?></h2>
                <p id="vri"><?php echo $post['created']; ?></p>
            <?php endforeach; ?>
        <div class="pagination">
            ...
        </div>
    </div>
</body>

1 个答案:

答案 0 :(得分:1)

// Query
$posts = $db->prepare(
    "
    SELECT SQL_CALC_FOUND_ROWS id, title, photo_dir, photo, body, created
    FROM posts
    ORDER BY created desc
    LIMIT {$start}, {$perPage}
    "
);