Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Reverse order if data in PHP

  • 18-08-2004 06:47PM
    #1
    Registered Users, Registered Users 2 Posts: 1,747 ✭✭✭


    Hi i am putting together a news page and at the moment i have this code which works and shows the first 5 news items in the database with a next and previous link as needed. This was taken from a tutorial on building a feedback form.

    My problem is that as it is news i need it to show the last news item (latest) first instead of the otherway around as is happening here.

    What do i need to change?


    <?php if ($totalRows_rsNews == 0) { // Show if recordset empty ?>
    <p>No news has yet been posted </p>
    <?php } // Show if recordset empty ?>
    <?php if ($totalRows_rsNews > 0) { // Show if recordset not empty ?>
    <p><strong>Latest News</strong></p>
    <?php } // Show if recordset not empty ?>
    <?php do { ?>

    <p><span class="date"><?php echo $row_rsNews; ?></span><br />
    <strong><?php echo $row_rsNews; ?></strong><br />
    <?php echo $row_rsNews; ?><br />
    <span class="newsposter">Posted by <?php echo $row_rsNews; ?></span>
    </p>
    <table width="182" border="0" cellpadding="0" cellspacing="0" background="images/news_break.gif">
    <tr>
    <td background="images/news_break.gif"><img src="images/news_break.gif" width="12" height="1" /></td>
    </tr>
    </table>
    <?php } while ($row_rsNews = mysql_fetch_assoc($rsNews)); ?>
    <br />
    <?php if ($pageNum_rsNews > 0) { // Show if not first page ?>
    <a href="<?php printf("%s?pageNum_rsNews=%d%s", $currentPage, max(0, $pageNum_rsNews - 1), $queryString_rsNews); ?>"><< previous news items</a>
    <?php } // Show if not first page ?>
    <?php if ($pageNum_rsNews < $totalPages_rsNews) { // Show if not last page ?>
    <a href="<?php printf("%s?pageNum_rsNews=%d%s", $currentPage, min($totalPages_rsNews, $pageNum_rsNews + 1), $queryString_rsNews); ?>">more news items >></a>
    <?php } // Show if not last page ?>


Comments

  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    I'm thinking change the database query so that it orders the data in the fashion tha you want....

    so
    SELECT * FROM mytable ORDER BY date DESC

    :)


  • Registered Users, Registered Users 2 Posts: 2,243 ✭✭✭zoro


    the first thing I thought of from the topic was php's built in SORT() method ... the reverse of this method is RSORT()

    dunno if that's helpful or not


  • Registered Users, Registered Users 2 Posts: 927 ✭✭✭decob


    Sort by the date in your sql statement.


  • Registered Users, Registered Users 2 Posts: 1,747 ✭✭✭Figment


    So if i change
    $query_rsNews = "SELECT * FROM tblNews";

    to

    $query_rsNews = "SELECT * FROM tblNews ORDER BY date DESC";

    it should do it?

    And then i could use something like
    <?php echo date('Y m d'); ?>
    To populate todays date in the text box?


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Yeah like they say, order by DESC (descending) in the sql statement. Don't order it with PHP, only lower performance


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,747 ✭✭✭Figment


    Cool, that works brilliantly.

    My next question is how do i break up the data that i am getting from
    <?php echo $row_rsNews; ?>

    into the year, month, day, hour, min, sec so that i can re arrange the date to the order we use?


  • Registered Users, Registered Users 2 Posts: 927 ✭✭✭decob


    use date_format in your mysql statement

    DATE_FORMAT

    SELECT DATE_FORMAT*, (newsDate, '%d %M %Y %H:%i:%s') as blah FROM table_name ORDER BY newsDate DESC;

    or similar, the above link has all the option for the output format

    then to output it...

    [PHP]
    <?=$row_rsNews?>
    [/PHP]


Advertisement