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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:04:34+00:00 2026-05-18T08:04:34+00:00

This is for a sorting script. I want to order my posts by votes.

  • 0

This is for a sorting script. I want to order my posts by votes. I’m using an if-statement in a function to check what order by was selected and display posts according to that.

The following function inserts data in my query.

function vote_orderby( $orderby )
{
  global $vote_sort;

  if( $vote_sort == "most_voted") {
     // order by most votes
     return "votes DESC"; //inserts into query
  }

  // return default order by if no votes
  return $orderby;
}

HTML

$vote_sort = "most_voted"; //should be picked up by function

..function
..loop

I want to generate,

$query = "SELECT * FROM posts ORDER BY votes DESC";

But posts are ordered by date. It works however without the if-statement, this ensures that I have no MYSQL errors.

How is it possible to pass the $vote_sort value into the function?

Note: I’m aware of the SQL injection risk, I will filter everything soon.

  • 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-18T08:04:34+00:00Added an answer on May 18, 2026 at 8:04 am

    To answer your comment and expand on Michael’s solution above, here is what you might want to do…

     function vote_orderby($orderby) {  
        switch($orderby) {
           // Sort by most votes
           case 'most_voted': $sort = 'votes DESC'; break;
           // Sort by least votes
           case 'least_voted': $sort = 'votes ASC'; break;
           // Default to most votes if does not match any other option
           default: $sort = 'votes DESC'; break;
        }
        return $sort; // Return query statement
     }
    

    Then your query statement would be such as…

    $sortby = $_GET['sort']; // However you want to get the way to sort
    // If $sortby is empty, it will default to most_voted via function
    $order = vote_orderby($sortby);
    $query = "SELECT * FROM posts ORDER BY ".$order;
    

    This is more of a permanent way of coding the system and prevent people to inject your MYSQL query and minor mistakes will not result in the future.

    I have coded many similar systems in the past and this is one of the best ways to go.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This recent question about sorting randomly using C# got me thinking about the way
This question is similar to GWT Table that supports sorting, scrolling and filtering However
Trying to follow this example. (Section String sorting...) Is there anything obvious that would
I am using James Padolsey's jQuery sorting script for a HTML table in an
I have this jQuery script: // sorting the names of the fields we're hiding
Hi I am using a table manipulation (sorting) script taken directly from Learning jQuery
Script is as follows; $(#Results a).live('dblclick', function(event){ var src = $(this).attr(href); if (event.type ===
Hi, I have made this simple script but I need to have it check
I created this script for educational reasons in pure JS. It adds sorting to
I have a PHP script that loads a function which uses Curl to log

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.