[TUTORIAL] STAR RATING |
[eluser]javer[/eluser]
Hi all, recently I needed to implement star rating, but I didnt know how. I was quite unhappy with google results. I have found some articles but they are unusable for me. http://ellislab.com/forums/viewthread/71620/P50/ - this was too complicated and I didnt find demo files http://net.tutsplus.com/tutorials/html-c...x-and-php/ - this was pain to follow. Sometimes I had no idea whats going on there. http://webtint.net/tutorials/5-star-rati...nd-jquery/ - this wasnt working well So I took last one and modified it little bit and Im here to share what I've done. So lets start. Oh I almost forget This is my first tutorial and Im pretty new to all php, mysql, javascript stuff, so be nice to me ![]() 1. GO here http://webtint.net/wp-content/uploads/20...estars.zip and dowlnoad it. You are going to need only png files: star and blank_star. 2. Go to your view (where you want to have stars) and place there this code: Code: $rating = (int)$question_row['rating']; Note 4 things: 1. FU()K IT, this forum sucks so much that its not even possible. The code above is wrong because this stupid forum dont allow me to insert to code everything i want. YOU CAN DOWNLOAD RIGHT CODE FROM THE ATTACHEMENT IN THIRD POST. 2. The name of div is made from name star_container_ and a id number: Code: <?php echo $question_row['id']; ?> 3. Im using there this code Code: <?php echo base_url() ?> 4. In the begining there is Code: $rating = (int)$question_row['rating']; 3. Now we are going to modify css. Add this code to your css file: Code: .hover_star { ![]() 4. Now we are going to use jquery. So go to your header and place between tags <HEAD> </HEAD> this code Code: <!-- JQUERY --> a) replace K in skript to c, so its SCRIPT not SKRIPT. This forum doesnt allow script tags, it thinks its xss or something. b) u should be connected to net because this is downloading from net. If you arent, download jquery-latest.js, place it somewhere and and reference it from there. CONTINUE IN NEXT POST
[eluser]javer[/eluser]
5. And now we are going to add our own javascript code. here it is. Add this in header, between HEAD tags: Code: <!-- STAR RATING SCRIPT --> Note: Replace K to C again in word SCRIPT Note: in click function $("[id^=rating_]").children("[class^=star_]").click(function() there is this code Code: $('#star_container_'+rid).load('<?php echo base_url() ?>questions/submitRating', {rating: current_star, id: rid}); 6. So by now you should have stars in each question and stars should react on mouse hover, Now lets add rating code. This is in my submitRating method in controller questions Code: function submitRating() CONTINUE IN NEXT POST
[eluser]javer[/eluser]
7. Its all. I show you how looks my updateRation in questionmodel: Code: function updateRating($p_id, $p_rating) and thats really all. Hope it helps somebody. Please try it somebody if it works, if I didnt forget anything. THIS STUPID F(u)CKING FORUM SHOW SOME PIECES OF CODE WRONG. SO ALL PIECES CAN BE DOWNLOADED AS TXT FILE IN THE ATTACHEMENT |
Welcome Guest, Not a member yet? Register Sign In |