Karthiga
Karthiga

Reputation: 240

Codeingiter Query For Order by in my particular column

I want order by query. My issue for the website is below.

I have a news table in my database and the table having order and date columns.

Now i want to display a news by today date with order of news.

For example : XXXx news having order 1 and yyyy news having order 1 and zzzz news having order 1 and aaa news order is 2.

I have displayed this value in ascending..so the result is

xxxx
yyyy
zzzz
aaaa

When i addded another news ie bbbb and order 1 then it's comes under zzzz

ie

xxxx
yyyy
zzzz
bbbb
aaaa

But i want the result is

bbbb
xxxx
yyyy
zzzz
aaaa

Note : The date is same date ie. today date

how do i get this? let me know

Upvotes: 0

Views: 65

Answers (2)

Girish Kumar Sinha
Girish Kumar Sinha

Reputation: 832

if your requirement is to show result from current date and order by order in ci then use this:

$this->db->where("date",date('Y-m-s'));//whatever date format you have store in table place here
$this->db->order_by("order","ASC");  //$this->db->order_by("order","DESC");// according to requirement

Or if your requirement is to show result date wise on order wise both order and date then:

$this->db->order_by("date","ASC");
$this->db->order_by("order","ASC");
$this->db->order_by("id","DESC");

Upvotes: 0

Maxim Zhukov
Maxim Zhukov

Reputation: 10140

You have to use ORDER BY with multiply columns. That's what you have:

(value) (order)
xxxx    order=1
yyyy    order=1
zzzz    order=1
bbbb    order=1
aaaa    order=2

And to get this:

(value) (order)
bbbb    order=1
xxxx    order=1
yyyy    order=1
zzzz    order=1
aaaa    order=2

Just ORDER BY order and then by value.

ORDER BY order, value

Upvotes: 1

Related Questions