Welcome Guest, Not a member yet? Register   Sign In
How to order data from newest to oldest
#1

[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
#2

[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
#3

[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-gui...ecord.html
#4

[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();

    }
#5

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

[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();

    }
#7

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




Theme © iAndrew 2016 - Forum software by © MyBB