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 6571117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:50:28+00:00 2026-05-25T14:50:28+00:00

I have a set of IDs passed in a particular order which needs to

  • 0

I have a set of IDs passed in a particular order which needs to be retained.
I am querying from several left joins the data related to each ID.
The IDs are returned from a search so the order must be kept to make the results valid (otherwise it makes searching rather pointless).

My code looks something like;

$this->db->where_in('id', $array_of_ordered_ids);

For example –

$this->db->where_in('id', array(4,5,2,6));

Will return the results in order 2,4,5,6.

I’d like for it to retain the order and return the results 4,5,2,6.

Thank you,

  • 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-05-25T14:50:28+00:00Added an answer on May 25, 2026 at 2:50 pm

    To order the result by the order in your array, you can do the following:

    $array_of_ordered_ids = array(4,5,2,6);
    

    As you already know the order of the numbers, you can use the Mysql FIELD()Docs function:

    ORDER BY FIELD(id, 4, 5, 2, 6);
    

    To create such a string, you can use implodeDocs:

    $order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids));
    

    Give it a try:

    $array_of_ordered_ids = array(4,5,2,6);
    $this->db->where_in('id', $array_of_ordered_ids);
    $order = sprintf('FIELD(id, %s)', implode(', ', $array_of_ordered_ids));
    $this->db->order_by($order); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of ids in a table (column id), lets say from
I have a set of ids and names in an associative array and in
I have a draggable event set up like such: $(ids.label).draggable({ containment: ids.wrapper, revertDuration: 100,
I have set a background on the data-role=page element like so <div data-role=page style=background:
I have a varchar variable been defined like this. declare @IDs varchar(50) set @IDs
The Problem is .. I have a Set of Lists IDs .. Each one
To give you a background - I have a 4 MSI's which comes from
I have a TableView loading a custom cell and loading the data from a
Suppose I have a CMS application written in Node.js which persists data on a
I have a simple table, where I have set IDs for headers and IDs

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.