Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8483871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:13:33+00:00 2026-06-10T20:13:33+00:00

I am using CodeIgniter and I have 2 tables in my database. 1st called

  • 0

I am using CodeIgniter and I have 2 tables in my database.

1st called continents and looks like this:

    id     | continent_name
    -----------------------
    1      | Africa
    2      | Antarctica
    3      | Asia
    4      | Australia
    5      | Europe
    6      | North America
    7      | South America

2nd called states and looks like this:

        id | continent_id  | country_name
        -----------------------------------
        1  | 5             | Denmark
        2  | 1             | Angola
        3  | 7             | Peru
        4  | 5             | Germany
        5  | 7             | Venezuela
        6  | 1             | Egypt
        7  | 5             | Spain
        8  | 5             | France
        9  | 7             | Argentina
        10 | 6             | Canada

And now I want an ordered list grouped by continent_id and oredered from most records (in my case Europe) to the least (Asia) result like this:

Europe (4)
South America (3)
Africa (2)
North America (1)
Asia (0)

The number of records is not important I will be using another foreach loop inside. But I need somehow achieve the count, group and order active record functions in one query:

So far I have this in my model:

function get_top_continents() {

            $q = $this->db->from('states')
                          ->join('continents', 'states.continent_id = continents.id')
                          ->group_by('continent_id')
                          ->get();
            return $q;


    }

But I need to order the result also on the number of items in group.

How to do such order by condition?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:13:35+00:00Added an answer on June 10, 2026 at 8:13 pm
    $q = $this->db->select('continents.id as continent_id,max(continents.continent_name) as   continent_name, COUNT(states.id) as states_count',FALSE)
                          ->from('continents')
                          ->join('states', 'continents.id=states.continent_id','left')
                          ->group_by('continents.id')
                          ->order_by('states_count', 'desc');
                          ->get();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function on my controller (Im using CodeIgniter) that reads the database,
I have this syntax for grabbing some data from database, I using codeigniter's active
I have built a local DVD Database using Codeigniter, With film names etc in.
I'm using CodeIgniter (because it's awesome) and I have something like: <?php echo anchor(/,
I'm using codeigniter for this project. I have a search form and search form
I'm using codeigniter 2.0++ or specifically 3.0-dev. The thing is I have multiple database
I have this big database on mysql (104 tables), where almost every single table
i have been using codeigniter active record class for database acces, But one thing
i am using codeigniter. i have a table to be displayed in a view
I have been using CodeIgniter all these while, and have solid foundation in OOP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.