按升序(时间)对我的 post 进行排序
Sort my post by ascendant order (time)
经过一番努力,我终于能够在我的 index.php
中显示博客条目,但是它们的顺序是新的在底部,旧的在顶部。
这是我的代码:
<?php
$connect = new PDO('mysql:host=localhost;dbname=posts','root','');
$cmd = $connect->query('SELECT * FROM posts');
$resultado = $cmd->fetchAll(PDO::FETCH_ASSOC);
foreach($resultado as $item):
?>
<div class="post">
<div class="post-body">
<a href="" class="post-title"><?php echo $item['titulo']?></a>
<div class="title-separator"></div>
<p class="post-body-text"><?php echo $item['cuerpo'] ?></p>
</div>
<div class="post-body-footer">
<img src="images/posted.png" alt="" class="posted"> <span class="posted-text">Posted on </span><span class="date"><?php echo $item['fecha']?></span>
</div>
</div>
<?php
endforeach;
?>
为了"order"您的数据以您希望的方式列出,您可以使用
ORDER BY your_column
可选择 ASC
或 DESC
。
如果在查询中省略,ORDER BY
默认为 ASC
。
即:
SELECT * FROM posts ORDER BY titulo DESC
咨询:
手册中的示例:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
经过一番努力,我终于能够在我的 index.php
中显示博客条目,但是它们的顺序是新的在底部,旧的在顶部。
这是我的代码:
<?php
$connect = new PDO('mysql:host=localhost;dbname=posts','root','');
$cmd = $connect->query('SELECT * FROM posts');
$resultado = $cmd->fetchAll(PDO::FETCH_ASSOC);
foreach($resultado as $item):
?>
<div class="post">
<div class="post-body">
<a href="" class="post-title"><?php echo $item['titulo']?></a>
<div class="title-separator"></div>
<p class="post-body-text"><?php echo $item['cuerpo'] ?></p>
</div>
<div class="post-body-footer">
<img src="images/posted.png" alt="" class="posted"> <span class="posted-text">Posted on </span><span class="date"><?php echo $item['fecha']?></span>
</div>
</div>
<?php
endforeach;
?>
为了"order"您的数据以您希望的方式列出,您可以使用
ORDER BY your_column
可选择 ASC
或 DESC
。
ORDER BY
默认为 ASC
。
即:
SELECT * FROM posts ORDER BY titulo DESC
咨询:
手册中的示例:
SELECT
[ALL | DISTINCT | DISTINCTROW ]
[HIGH_PRIORITY]
[STRAIGHT_JOIN]
[SQL_SMALL_RESULT] [SQL_BIG_RESULT] [SQL_BUFFER_RESULT]
[SQL_CACHE | SQL_NO_CACHE] [SQL_CALC_FOUND_ROWS]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[ASC | DESC], ... [WITH ROLLUP]]
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]