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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:37:46+00:00 2026-06-02T01:37:46+00:00

I had some trouble descriping this problem. Please feel free to change the title

  • 0

I had some trouble descriping this “problem”. Please feel free to change the title to something more descriptive for this subject.

I have a database and want to give users the opportunity to choose how to sort the results, search the database, etc.

I am using PHP and MySQL.

I made this table setup as an example:

table Persons
+----+------+----------+------------+------+-----+
| id | age  | gender   | date       | hits | car |
+----+-----------------+------------+------+-----+
| 1  | 18   | male     | xx.xx.xxxx | 1040 | 1   |
| 2  | 35   | female   | xx.xx.xxxx | 443  | 2   |
| 3  | 67   | male     | xx.xx.xxxx | 453  | 2   |
| 4  | 10   | male     | xx.xx.xxxx | 3454 | 4   | ==> 4 means Citroen
| 5  | 98   | female   | xx.xx.xxxx | 323  | 6   |
+----+------+----------+------------+------+-----+

table Cars
+----+----------+
| id | model    |
+----+----------+
| 1  | Porche   |
| 2  | Ferrari  |
| 3  | Volvo    |
| 4  | Citroen  |
| 5  | VW       |
+----+----------+

A way to select data from the database could be like this:

$sql = "SELECT *, DATE_FORMAT(date, '%b %e, %Y') AS show_date FROM persons ORDER BY date DESC LIMIT 100";

But, what if i want to give the users the opportunity to sort by date, hits, rating (another table) both descending and ascending and also the opportunity to only select persons below age X with the gender of male (for example). How do i do this? Do i need to make if else for all possible querys or is it possible in one single query?

I dont need a finish code snippet, i just need to know if there are any good articles or a “Best Practice”-way to do this. The easiest and most secure method is prefered.

P.S. what do you call this kind of ‘problem’?

  • 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-02T01:37:49+00:00Added an answer on June 2, 2026 at 1:37 am

    In your search query function, just take the search fields, options, etc. and build up your query string. If the SELECT statement will always pull the same fields, then you can start by creating the query up to the WHERE clause, and build from there. (Make sure to protect user input from sql injection attacks. Use prepared statements, or suggestive methods from the manual (defined by getSqlVal() in my ex. code.)) Here is a small example:

    $sql = "SELECT ... FROM Persons p, Cars c WHERE p.car = c.id";
    
    if(isset($_POST["age"]))
        $sql .= " AND age < " . getSqlVal($_POST["age"], 'int');
    if(isset($_POST["gender"]))
        $sql .= " AND gender = '" . getSqlVal($_POST["gender"], 'text') . "'";
    if(isset($_POST["sortBy"])){
        $sql .= " ORDER BY " . getSqlVal($_POST["sortBy"], 'text');
        if(isset($_POST["sortDirection"]) && $_POST["sortDirection"] == "DESC")
            $sql .= " DESC";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had some trouble with IE8 (I don't know about 9 at this
I am currently having some trouble wrapping my head around this issue. I had
I've recently had some trouble moving the location of the indexes folder (for the
I use DropBox and I've had some trouble reaching to my files from other
For some reason I've always had trouble remembering the backwards/forwards compatibility guarantees made by
I know that people have had some trouble with undefined CssClass values when using
I've had some trouble finding information about Apple's policies regarding in-app ads. I'm interested
I'm having some trouble with displaying a popup div with CSS. The problem is
I am having trouble uploading an image with JSP in Tomcat. I had some
I'm some trouble finding the problem with my program. Getting the error: Use of

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.