onjegolders Posted November 12, 2012 Share Posted November 12, 2012 Just though I'd post in case anyone else was interested in creating a 5 star rating system. It is very rudimentary I should point out and am sure there are thousands of better ways but it seems to get the job done OK for me. It's only using cookies to check if someone's already voted so it's not at all foolproof! If any advanced users have any pointers to improve it, that would be great! <?php include("./header_plain.inc"); ?> <div id="new_blog_view" class="row"> <div id="blog_header"> <a href="<?php echo $config->urls->root; ?>"><img src="<?php echo $config->urls->templates; ?>styles/images/ag_logo_2.png" alt="" /></a> <h4><a href="<?php echo $page->parent->url; ?>">From the Blog</a></h4> </div> <h1><?php echo $page->title; ?></h1> <h6><?php echo $page->entry_date; ?></h6> <?php if ($page->image) { ?> <img src="<?php echo $page->image->url; ?>" alt="<?php echo $page->title; ?>" id="blog_image" /> <?php } ?> <?php $class = "new_blog_view_content"; if (!$page->image) { $class .= " divide"; } ?> <div class="<?php echo $class; ?>"> <?php echo $page->body; ?> </div> <?php echo $page->comments->render(); ?> <?php echo $page->comments->renderForm(); ?> <?php if ($page->vote_count > 0) { $vote_average = round($page->vote_score / $page->vote_count, 2); if ($vote_average > 0 && $vote_average < 1) { $url = $config->urls->templates . "styles/images/stars_0.png"; } elseif ($vote_average >= 1 && $vote_average < 2) { $url = $config->urls->templates . "styles/images/stars_1.png"; } elseif ($vote_average >= 2 && $vote_average < 3) { $url = $config->urls->templates . "styles/images/stars_2.png"; } elseif ($vote_average >= 3 && $vote_average < 4) { $url = $config->urls->templates . "styles/images/stars_3.png"; } elseif ($vote_average >= 4 && $vote_average < 5) { $url = $config->urls->templates . "styles/images/stars_4.png"; } elseif ($vote_average = 5) { $url = $config->urls->templates . "styles/images/stars_5.png"; } ?> <h5>Current rating:</h5> <p><img src="<?php echo $url; ?>" width="100" height="25" alt="<?php echo $page->name . " - " . $vote_average . " stars"; ?>"/><span> (<?php echo $vote_average . "/5"; ?>)</span></p> <?php } ?> <?php $rating = $page->vote_score; $votes = $page->vote_count; $out = ""; if (isset($input->post->submit_rating) && $input->post->test == "") { $out = "<h5>Thank you for rating!</h5>"; $new_rating = $input->post->rating + $rating; $votes++; $page->of(false); $page->set("vote_score", $new_rating); $page->set("vote_count", $votes); $page->save(); $voted = true; $page->of(true); // set cookie to ensure only 1 vote (not foolproof!) setcookie('voted',$page->name,time() + (86400 * 7)); echo $out; ?> <?php } elseif(isset($_COOKIE['voted']) && $_COOKIE['voted'] == $page->name) { echo "<br>You have already voted, thank you!"; } else { ?> <div id="rating_box"> <h4>Please feel free to rate this article!</h4> <form action="" method="post"> <select name="rating" id="rating">Your rating <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> </select> <input type="text" id="test" name="test"> <input type="submit" id="rating_submit" name="submit_rating" value="Submit rating"> </form> </div> <?php } ?> <a href="<?php echo $pages->get("/blog/")->url; ?>" id="back_blog_link">← Return to Blog</a> </div> <?php include("./footer_plain.inc"); ?> 1 Link to comment Share on other sites More sharing options...
ryan Posted November 12, 2012 Share Posted November 12, 2012 Thanks for posting this. I've thought a few times that we should have a five-start type voting module at some point and this looks like a good start on that. One thing I want to mention is that you might want to add validation for your $input->post->rating, because it looks like it should be limited to 1-5 and it appears that someone could manipulate the post variable to have any number in it, whether "-500" or "99999", etc. So I'd suggest adding some validation, perhaps something like this: $r = (int) $input->post->rating; if($r > 0 && $r <= 5) $new_rating = $page->vote_score + $r; else $new_rating = $page->vote_score; Link to comment Share on other sites More sharing options...
onjegolders Posted November 13, 2012 Author Share Posted November 13, 2012 Thanks for the feedback Ryan, I'll add that validation and I think you're right, it would be nice to have a rating module (it's a completely non-critical module but still ) It all adds up! Link to comment Share on other sites More sharing options...
onjegolders Posted November 13, 2012 Author Share Posted November 13, 2012 For the rating itself, it would be nice to have the user hover over the stars to vote, I'd have to figure out the CSS of that and how to convert stars to an integer in PHP (I'm learning on the job!) Link to comment Share on other sites More sharing options...
Luis Posted December 30, 2012 Share Posted December 30, 2012 Have you tried this ? http://www.fyneworks.com/jquery/star-rating/ 1 Link to comment Share on other sites More sharing options...
arjen Posted December 30, 2012 Share Posted December 30, 2012 Would be nice to see those implemented! Link to comment Share on other sites More sharing options...
Pete Posted December 30, 2012 Share Posted December 30, 2012 Yep - I think a jQuery rating system (as Luis has linked to) as part of a module as ryan says would be great in the longer term and it would be easy to rate ANY content since it's all Pages and just a case of adding the functionality to each template Obviously it would need to take into consideration things like multiple votes from one IP address, flood control (set by session and/or cookie) so someone can't spoof different IP addresses and spam it but ultimately all the checks in the world won't stop someone from finding a way around multiple voting unless you require them to log in first - that's a bit much for most sites' needs though! In the meantime, this code looks great and does the job nicely Link to comment Share on other sites More sharing options...
Luis Posted January 4, 2013 Share Posted January 4, 2013 a Andre just remembered a css star hover tut. check this: http://css-tricks.com/star-ratings/ 1 Link to comment Share on other sites More sharing options...
onjegolders Posted January 4, 2013 Author Share Posted January 4, 2013 Thanks Luis Link to comment Share on other sites More sharing options...
Luis Posted January 24, 2013 Share Posted January 24, 2013 Hey dude, any progress achieved? Link to comment Share on other sites More sharing options...
onjegolders Posted January 28, 2013 Author Share Posted January 28, 2013 Hey dude, any progress achieved? Hey buddy, not really had a chance to look at it in a while, will definitely check back though when I get a few minutes! 2 Link to comment Share on other sites More sharing options...
Mass Harry Posted September 22, 2014 Share Posted September 22, 2014 thaks ojegolderstutorial Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now