Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: ORDER by "%" ?

  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    ORDER by "%" ?

    Okay, I am trying to change my old rating with my new one. this time how I calculate top rated games is by how much people like it from dont like. so be is converted to a % in PHP, but how do I place that into a query?

    my old query.
    PHP Code:
    $sql_query "SELECT * from games WHERE gamestatus = 1 ORDER BY rating DESC LIMIT 14"
    this is the new rating, and I want to somehow do ORDER by highest %
    PHP Code:
    $rate_yes $row['rate_yes'];
            
    $rate_no $row['rate_no'];
            
    $rate_t $rate_yes $rate_no;
            
    $abcd round($rate_yes $rate_t 100); 

    I making any sense, hope you can see what I'm trying to do.
    Thanks
    -Tim

  • #2
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    So basicly say I have 5 games


    Game 1 = 80%
    Game 2 = 21%
    Game 3 = 45%
    Game 4 = 66%
    Game 5 = 12%

    How would I 'ORDER by' in a query,

    database fields are listed above, and you have to do the rounding out to get the %, but how can I order that list on the site with highest % to lowest? thanks

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    I would make this line more clear on the order it does the math.

    PHP Code:
    $abcd round($rate_yes $rate_t 100); 
    To

    PHP Code:
    $abcd round($rate_yes $rate_t) * 100)); 
    Now I don't know if the math is correct for your needs, it's just a syntax example to make it more readable.


    ------------
    Leonard Whistler

  • #4
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    I ran this query, came out okay.. but just to be sure. on a large # of traffic. Is this safe to run through the database?

    PHP Code:
     $sql_query "SELECT * from games WHERE gamestatus = 1 AND (rate_yes > rate_no) ORDER BY (rate_yes / rate_no)*100  DESC LIMIT 14"

  • #5
    New Coder
    Join Date
    Dec 2008
    Posts
    16
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I cant find any issues with the code. But you could have remove the % from the field data making it to a integer/float value and simply use the order by asc/desc

    I cant see any specific reason to insert the % in to the DB, and if you want to update the field data you can use replace function in mysql.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •