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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:31:23+00:00 2026-05-26T04:31:23+00:00

There are 6 possible keys in a MySQL field. Lets call them types. Through

  • 0

There are 6 possible keys in a MySQL field. Lets call them types. Through PHP, I have defined an array, that is called $order, and arranges these types in order I want them to appear.

There is a table, articles, which has a field articles.type . Any article can have 0-6 types added to it. Now, what I want to do, is grab all of the articles, and order them from the prototype. What is the best way to do this? Can this be done in MySQL, since I suppose that would be faster? And if not, how can it be done in PHP?

Example:

Table:

id     articleId     type
1      3             type1
2      3             type2
3      3             type3
4      3             type4
5      4             type5
6      4             type6
7      5             type5
8      7             type1
9      7             type5

Order:

$order=array('type1','type2','type3','type4','type5','type6');

How do I fetch the results ordered by my $order variable?

  • 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-26T04:31:23+00:00Added an answer on May 26, 2026 at 4:31 am

    You’d need to massage that array into a mysql-style if/case statement:

    $order_by = "ORDER BY CASE";
    $pos = 1;
    foreach ($order as $clause) {
       $order_by .= " CASE `type`='$clause' THEN " . $pos++;
    }
    $order_by .= " ELSE " . $pos++;
    

    which would generate something like

    ORDER BY CASE
       WHEN `type`='type1' THEN 1
       WHEN 'type`='type2' THEN 2
       ...
       ELSE n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a possible htaccess directive that can transparently forward request from index.php to
I have an PHP/MySQL eCommerce application that has a few innoDB tables (for the
Is there any possible way to apply more than CSS to a control through
I was wondering ( if possible ) if there was a program/tool/utility that when
We have got a custom MembershipProvider in ASP.NET . Now there are 2 possible
Possible Duplicate: Is there a tool that can display a SVN repository visually (
I have an incredibly simple query (table type InnoDb) and EXPLAIN says that MySQL
I have a complex MySQL query that joins three tables and self-joins one table
I have a view which is defined as a SELECT bellow. Is there any
We have an XMPP application that uses MySQL to store information. We are not

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.