Website Ratings Addon Utility Script Tutorial
The coolest ratings php script the NoSQL way. Plug and play.
Hi there!. In this thumbs up php ratings script tutorial
I will show you how easy it is going NoSQL like me.I have written this script to make it plug and play.
Just include it in pages you want a ratings sytem.
I go thru the steps and include logic and process comments so you know how I think.
I could have done it in less code but I wanted to make it easier for the people to use.
All you need to do is save the images or substitute your own. Be creative.
I hope you enjoy examining it and getting use out of it.I will change post to ajax or target Iframe. No page jump. Thank you very much. ttyl
P.S. By the way it is at the bottom of this page try it out.
1: <?php
2: session_start();
3: // The Thumbs Up Rating Utility NoSQL php Script.
4: //Copyright 2021 by Handicapped George at CMXads.com
5: //Link must remain intact and visible for free usage.
6:
7: //NOTE: I went to the trouble to make this a plug and play script.
8: // no need to make and files or the data directory.
9: //just include in every page you want a rating.
10: // seperate file is made based on page name /uri/self//
11: //Just save the images or substitute your own
12:
13: $allowRating = 1;// set to 1 to enable rating script
14: $thumbsup='';
15: $content='';
16: $vote = '';
17: $qstr='';
18: $page ='';
19: $rate_file ='';
20: if(isset($_SERVER['QUERY_STRING'])){
21: $uri = $_SERVER['PHP_SELF'];
22: }else{
23: $uri = $_SERVER['REQUEST_URI'];
24: }
25: //prepare filename to write too based on the page this script is used in
26: // get file name from uri basename
27: if(isset($_GET['id'])){// validate the get value to base page name they should match
28: $_SESSION['rated'] = $_SERVER['REMOTE_ADDR'];
29: $page = trim($_GET['id']);// remove extension from basename
30: $direct = str_replace(array('.php','.htm','.html'), '', basename($uri));
31: if($page != $direct){
32: $allowRating = '';// if they do not match we do not continue
33: }
34: }else{
35: $direct = str_replace(array('.php','.htm','.html'), '', basename($uri));
36: }
37: if($allowRating)
38: {
39: $page = basename($uri);
40: $rate_file = 'ratings/'.$direct .'.dat';// make file path to write too
41: if(isset($_REQUEST['voted']) && strlen($_REQUEST['voted']) == 1 && ctype_digit($_REQUEST['voted'])){
42: $vote = trim($_REQUEST['voted']);// if we got a vote number
43:
44: }
45:
46: if(!is_dir('ratings/')){// if no directory make it
47: mkdir('ratings/', 755);
48: }
49: // if the first time no file. so make it and add 1 to it
50: if(!file_exists($rate_file) || filesize($rate_file)==0 ){
51: $data_in = '1' . PHP_EOL . '5' . PHP_EOL;
52: file_put_contents( $rate_file, $data_in);
53: }
54: // now we should have file not empty
55: if($vote && file_exists($rate_file) and filesize($rate_file) > 1){
56: $rates = file($rate_file, FILE_IGNORE_NEW_LINES);// get the 2 file lines in array
57: $newCount = $rates[0]+ 1;// firt array value is vote count.. we add one to it
58: $newRating = $rates[1]+ $vote;// second value is rating, add to it
59: $newIn = array( $newCount.PHP_EOL, $newRating);// assemble replace array
60:
61: // array replace file array will updated numbers
62: //$update_rate = array_replace($rates, $newIn);//DUH!!
63: //write file
64: file_put_contents( $rate_file, $newIn );
65:
66: }
67:
68:
69: //now we get the file data
70: if(file_exists($rate_file) and filesize($rate_file) > 1){
71: $data = file($rate_file, FILE_IGNORE_NEW_LINES);
72: }
73: // we divide the count by the rating number
74: $content .="<table style='text-align:center;width:420px;'><tbody><tr><td><b>Rate me please.</b></td></tr><tr><td>";
75: $current = $data['1'] / $data['0'];
76: $content .="Current Rating: " . round($current, 1) . " Thumbs up!</td></tr><tr><td>";
77: $thumbsup = round($current, 1);// we get a round percentage number to one digit
78: if($thumbsup >= 4.7){// else if compare and load images based on the number
79: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'>";
80: }
81: elseif($thumbsup < 4.7 && $thumbsup >= 4.3){
82: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/half-thumbs-up.gif'>";
83: }
84: elseif($thumbsup < 4.3 && $thumbsup >= 3.7){
85: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'>";
86: }
87: elseif($thumbsup < 3.7 && $thumbsup >= 3.3){
88: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/half-thumbs-up.gif'>";
89: }
90: elseif($thumbsup < 3.3 && $thumbsup >= 2.7){
91: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'>";
92: }
93: elseif($thumbsup < 2.7 && $thumbsup >= 2.3){
94: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'> <img src='images/half-thumbs-up.gif'>";
95: }
96: elseif($thumbsup < 2.3 && $thumbsup >= 1.7){
97: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/one-thumbs-up.gif'>";
98: }
99: elseif($thumbsup < 1.7 && $thumbsup >= 1.3){
100: $content .="<img src='images/one-thumbs-up.gif'> <img src='images/half-thumbs-up.gif'>";
101: }
102: elseif($thumbsup < 1.3 && $thumbsup >= 0.7){
103: $content .="<img src='images/one-thumbs-up.gif'>";
104: }else{
105: $content .="<img src='images/half-thumbs-up.gif'>";
106: }
107:
108: $content .="</td></tr><tr><td>";
109: //This creates 5 links to vote a 1, 2, 3, 4, or 5 rating for each particular item
110: //$content .="Rate it: ";
111: if(!isset($_SESSION['rated']) ){
112: $content .="<a href='?voted=1&id=$direct'>Vote 1</a> | ";
113: $content .="<a href='?voted=2&id=$direct'>Vote 2</a> | ";
114: $content .="<a href='?voted=3&id=$direct'>Vote 3</a> | ";
115: $content .="<a href='?voted=4&id=$direct'>Vote 4</a> | ";
116: $content .="<a href='?voted=5&id=$direct'>Vote 5</a> ";
117:
118: }
119: $content .="</td></tr><tr><td>";
120: $content .="<p style='font-size:0.7em;'><a href='https://cmxads.com/' title='this link must remain intact and visible for legal usage. visit cmxads.com for more scripts tips and advertising services'>© 2021 CMXads.com Thumbs up rating script.</a></p>";
121: $content .="</td></tr></tbody></table>";
122: echo $content;
123: }
124: ?>