I just started using PDO (loving it!). Still trying to learn the basics. Currently I can make a connection and display the data I want. However, I want to display my blog posts from newest to oldest based on the date. Below is the code I’m using:
<?php
while($row = $sth->fetch()) { ?>
<ul class="blog article_list large">
<li>
<img class="img_frame" src="images/blog_large/post01.jpg" alt="" />
<article>
<h2><a href="#"><?php echo $row['title'] ?></a></h2>
<?php echo $row['slug'] ?>
<small class="details">
By <a href="#" class="article_author"><?php echo $row['author'] ?></a>
on <time datetime="2011-09-28" pubdate="pubdate"><?php echo date("m-d-Y", strtotime($row['date'])) ?></time>
in <a href="#" class="cat"><?php echo $row['category'] ?></a>.
</small>
</article>
</li>
</ul>
<?php } ?>
Is there a PDO statement to do this?
Um, wouldn’t that be a part of your SQL? Using
ORDER BYor some other sorting method? PDO is just a data access layer – you’d do better to sort like that on the database side then just pull the results out as they come.