CodeIgniter Forums
How to order data from newest to oldest - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: How to order data from newest to oldest (/showthread.php?tid=37552)



How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Michal1[/eluser]
Hi guys I slowly leak into the codeigniter and here is a thing I am not sure about.

I am basically getting data from my database and then showing them in a view. I am using a simple foreach loop.

Code:
<?php if(isset($records)) : foreach($records as $row) :?>

    <h2> &lt;?php echo $row->title; ?&gt; </h2>
        <div>&lt;?php echo $row->content; ?&gt;</div>
---------------------------------------------------
        &lt;?php endforeach ;?&gt;

    &lt;?php else : ?&gt;

        <H2>No articles found</H2>
    &lt;?php endif; ?&gt;

The thing is that articles (records) are ordered in the way, that oldest articles are on the top and the newest on the bottom. But of course I need to have it opposite, so it should go from newest at the top to oldest at the bottom like every other CMS. I am pretty sure this will just require a little edit, but I dont know what to do.

COuld somebody help?

Thank you very much


How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Bart v B[/eluser]
I think you can better do that in your sql statement.

Something like: ORDER BY datefield ASC or DESC.
You can play with ASC and DESC one does the newest and the other the oldest.
But i forget witch one you need. Wink


How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]mi6crazyheart[/eluser]
If u r using CI active records to make u'r sql query just use "$this->db->order_by()" in u'r model function.

ha ha, most probably that might be "DESC"

Example:
Code:
$this->db->order_by("dateField", "desc");

// Produces: ORDER BY title DESC

Ref: http://ellislab.com/codeigniter/user-guide/database/active_record.html


How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Michal1[/eluser]
Thanks guys.

For getting a data from database I got this functin in a model class
Code:
function get_records()

    {


            $query = $this->db->get('data');
                
        return $query->result();

    }



How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Michal1[/eluser]
So what do you think is the best method for this?


How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Bart v B[/eluser]
Then it would be something like this:

Code:
&lt;?php
function get_records()

    {                                      
            $this->db->order_by("dateField", "desc");
            $query = $this->db->get('data');
                
        return $query->result();

    }



How to order data from newest to oldest - El Forum - 01-13-2011

[eluser]Michal1[/eluser]
thank you Bart. I did it needlesly too much difficult :-).