Question

Im making a blog and im trying to make it so it displays my posts from soonest to oldest. I thing the easiest way it to use the array_reverse() function.

I have never used it before so i need some advice on doing so.

here is my current code how do i put it in?

<?php
$connect = mysql_connect("****","****","******");
mysql_select_db("****");
$posts = mysql_query("SELECT * FROM Posts");
  WHILE($data = mysql_fetch_array($posts)):
    $picture = $data['Picture'];
    $pname = $data['Name'];
    $date = $data['Date'];
    $poster = $data['Poster'];
    $ptext = $data['Text'];
?>        
<div class="row">
  <li class="span12">
    <div class="thumbnail">
      <div class="img-wrap">
        <center>
          <a href="#">
            <img src="<?php echo "$picture"; ?>" alt=""/>
          </a>
        </center>
      </div>  
      <h3><?php echo "$pname"; ?></h3>
      <div class="info-panel clearfix">
        <span class="pull-left">
          <time datetime="">
            <i class="icon-calendar "></i> <?php echo "$date"; ?>
          </time>
        </span>
        <span class="pull-right">
          <i class="icon-user"></i> By <?php echo "$poster"; ?>
        </span>
      </div>  
      <p><?php echo "$ptext"; ?></p>
    </div>  
  </li>
</div>          
<?php
  endwhile;
?>
Was it helpful?

Solution

Consider user the ORDER BY clause in your SQL to read out the post already sorted on the correct order instead of reversing the array. SELECT * FROM Posts ORDER BY Date DESC

OTHER TIPS

Just use an ORDER BY clause in your SQL query:

$posts = mysql_query("SELECT * FROM Posts ORDER BY Date DESC");

That's all you need to do.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top