Need HELP PHP about round up and DESC

Discussion in 'PHP' started by Darkler, Feb 28, 2008.

  1. #1
    This is query

    $query = "SELECT a.*, (r.rating_sum/r.rating_count) AS rating, r.rating_sum as userrate"
    . "\n FROM #__mmovies AS a"
    . "\n LEFT JOIN #__mmov_movrating AS r ON r.movie_id=a.id"
    . "\n WHERE a.published <> '0'"
    . "\n ORDER BY rating_sum DESC"
    . "\n LIMIT $count"

    This is Echo

    ?>
    <tr>
    <td>
    <a href="<?php echo $link; ?>"">
    <?php echo htmlspecialchars($row->title, ENT_QUOTES);?>
    </a>
    </td>
    <td>
    <?php echo '<b>'.$row->rating.'</b>';?>
    </td>
    </tr>
    <?php
    }
    ?>

    I want to round up into 2 decimal, but I don't know how to do.

    And there is a problem with order by, the rating is not order by DESC (Not arranged)

    Please help me PHP EXPERT,

    Thanks,
    William
     
    Darkler, Feb 28, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    When you say round, you mean turn "2.3567" into "2.36" or something? Well, check out the round function: http://us2.php.net/round

    Now, for your order by. You said it's not ordering by 'rating', did you want it to? You have it set to 'ORDER BY rating_sum' when I assume you meant to put 'ORDER BY rating' (if you did mean to put rating_sum and it's still not working, try putting r.rating_sum since that's what you named the table).

    One more thing, a nice easy method to echoing small amounts of data in HTML, you can do <?='<b>'.$row->rating.'</b>'?> instead of your entire echo statement.


    EDIT: If you meant you want to round in the query itself, check out the MySQL round function: http://dev.mysql.com/doc/refman/5.0/en/mathematical-functions.html#function_round
     
    zerxer, Feb 28, 2008 IP
  3. Darkler

    Darkler Active Member

    Messages:
    596
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thanks a lot, Sir. It didn't change anything when I put r.rating_sum, It didn't change anything.

    I have already solved the Round_up problem, the main problem actually is ORDER BY.

    For example, A = 8, b = 7, c =9, d =10.

    The listed show,

    D = 10
    b = 7
    c = 9
    A = 8,

    How to solve this.
     
    Darkler, Feb 28, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Ohh, it's ordering them as if they are strings and not numbers.

    Try one of the following:

    ORDER BY CAST(r.rating_sum AS INTEGER) DESC
    or
    ORDER BY CONVERT(r.rating_sum, INTEGER) DESC


    I honestly don't know which to use or if those are even typed up correctly.. I used this as my reference: http://dev.mysql.com/doc/refman/4.1/en/cast-functions.html#function_cast (underneath of that one is the convert function)

    I'd assume that casting it as an integer would make it order by numerically.. Is your rating_sum not of type int already?
     
    zerxer, Feb 28, 2008 IP
  5. chadhaajay

    chadhaajay Member

    Messages:
    57
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #5
    Round function will do the job for you.
     
    chadhaajay, Feb 28, 2008 IP
  6. Darkler

    Darkler Active Member

    Messages:
    596
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #6
    Thanks for all your help, zerxer and chadhaajay.
     
    Darkler, Feb 29, 2008 IP