Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Display A Stored Value (1-5) As A Star?

  • This topic is empty.
Viewing 6 posts - 16 through 21 (of 21 total)
  • Author
    Posts
  • #192606
    bruce-1980
    Participant

    Hi

    I hope you dont mid me joining the conversation. I’ve been following your post and have found it very useful up to now but I seem to have a problem with the rounding.

    Here is my code first of all…

    <php
    
    $sql = "SELECT AVG(rating) FROM artiste_reviews WHERE artiste_author_id=$author";
    
    $result = mysql_query($sql) or die(mysql_error());
    
    while($reviewrow = mysql_fetch_array($result))
    {
    
    `    // round down to get number of whole stars needed
        $wholeStars = floor( $reviewrow['AVG(rating)'] );
    
        // double, round, take modulo.
        // this will be 1 if you have a half-rating, 0 if not.
        $halfStar = round( $reviewrow['AVG(rating)'] * 2 ) % 2;
    
        // display blank stars.
        $blankStar = 5 - $wholeStars - $halfStar;
    
        // this will hold your html markup
        $HTML = "";
    
        // write img tags for each whole star
        for( $i=0; $i<$wholeStars; $i++ ){
            $HTML .= "<img src='/Graphics/rating-star-full.png' alt='Whole Star' />";
        }
        // write img tag for half star if needed
        if( $halfStar ){
            $HTML .= "<img src='/Graphics/rating-star-half.png' alt='Half Star' />";
        }
        // write img tag for blank stars if needed
        for( $i=0; $i&lt;5-$wholeStars-$halfStar; $i++ ){
            $HTML .= "<img src='/Graphics/rating-star-empty.png' alt='Blank Star' />";
        }
    
        // all done
        print $HTML;
    `
    
    }
    >
    

    I’ve added $blankstar too to display the full 5 stars, filled, half filled or empty.

    The problem is if for example my average value is output at 3.6 I get the correct 3 and a half stars displayed but if the average is say 3.8, I only get 3 stars displayed.

    The FLOOR $wholestars is returing 3 and the ROUND $halfStar is returning 0 which is correct but I cant figure out how to get it display an extra star for the rounded up remainder of 0.8 to get the 4 stars as it should be.

    You can see my working page at http://www.our-pub.co.uk/Artists/TomHolden2.php

    Hope you can help! :)

    #192633
    Paulie_D
    Member

    I hope you dont mind me joining the conversation.

    The conversation ended a year ago.

    Necro post.

    #192635
    Tom
    Participant

    @Paulie_D I hope you don’t mind me responding to @bruce-1980 to give him a tip even though this is a dead post.


    @bruce-1980
    The mysql extension in PHP is deprecated so you should consider switching over to either the mysqli extension or PDO to future proof your scripts. I also highly recommend looking into prepared statements to help prevent SQL injection.

    #192647
    MBM
    Participant

    I’ll pass on my code within the next day. I resolved this a while back including the rounding but I don’t code anymore unless I can help it so wouldn’t know exactly what I did.

    #193416
    bruce-1980
    Participant

    Thanks for the replies guys, didn’t realise the thread was a year old! Just saw January 6… didn’t notice the 2014 bit!
    I’ve implemented the star ratings without the halves for now but I’ll try the code above and see if I can get it going!
    And I’ll replace the mysql functions with mysqli
    Thanks again! :)

    #193419
    MBM
    Participant

    It’s all there. The code is marked up so you should be able to figure out what each section is doing.

    https://gist.github.com/gyprosetti/7d97b4fd83ddaaa05685

Viewing 6 posts - 16 through 21 (of 21 total)
  • The forum ‘Back End’ is closed to new topics and replies.