<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Code The World — Simple Voting Script]]></title>
		<link>https://codetheworld.com/viewtopic.php?id=63</link>
		<atom:link href="https://codetheworld.com/extern.php?action=feed&amp;tid=63&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Simple Voting Script.]]></description>
		<lastBuildDate>Sun, 15 Jun 2008 16:29:40 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Simple Voting Script]]></title>
			<link>https://codetheworld.com/viewtopic.php?pid=167#p167</link>
			<description><![CDATA[<p>Here&#039;s an example simple voting script.</p><p>It is a great thing to have if you have multiple pages and use includes for the pages (Like you use PHP Switches (EG: Your URL: index.php?action=about)) as it is highly adaptable. I created it with simplicity in mind. Now you dont have to have PHP Switches in order for this to work.</p><p>If you want to use it with PHP Switches (EG: Your url is <a href="http://site.com/scripts.php?id=cms)">http://site.com/scripts.php?id=cms)</a> You would do this:<br /></p><div class="codebox"><pre><code>&lt;?php
require(&#039;vote_functions.php&#039;);
startvoting(&quot;SWITCH&quot;,&quot;GET&quot;);
?&gt;</code></pre></div><p>Now, &quot;SWITCH&quot; needs to be replaced with the you guessed it, PHP Switch, as in if your URL is index.php?action=w/e you put in &quot;action&quot; or if its index.php?id=w/e youd put in &quot;id&quot; in place of &quot;SWITCH&quot;. What does the &quot;GET&quot; mean? It simply tells it, to get the current information it should get or where it should store it in can be gotten from $_GET[&#039;SWITCH&#039;].</p><p>Now without the PHP Switch you would do:<br /></p><div class="codebox"><pre><code>&lt;?php
require(&#039;vote_functions.php&#039;);
startvoting(&quot;PAGE&quot;,&quot;NONE&quot;);
?&gt;</code></pre></div><p>Now you would replace &quot;PAGE&quot; with whatever you would like such as &quot;script1&quot; or &quot;page1&quot; or &quot;w/e&quot;. and &quot;NONE&quot; means there is nothing special it has to get from the URL.</p><p>Here is the code:</p><p><strong><span class="bbu"><span style="color: red">db.sql:</span></span></strong><br /></p><div class="codebox"><pre><code>DROP TABLE IF EXISTS `voting_ratings`;

CREATE TABLE `voting_ratings` (
  `page` text NOT NULL,
  `ip` text NOT NULL,
  `time` text NOT NULL,
  `rating` text NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;</code></pre></div><p><strong><span class="bbu"><span style="color: red">vote_settings.php:</span></span></strong><br /></p><div class="codebox"><pre><code>&lt;?php

   // Edit from here!

   

   $voting[&#039;host&#039;] = &#039;localhost&#039;; // This is your MySQL Host it is usually always localhost

   $voting[&#039;db_user&#039;] = &#039;&#039;; // Put your MySQL User

   $voting[&#039;db_name&#039;] = &#039;&#039;; // Put your MySQL DB

   $voting[&#039;db_pass&#039;] = &#039;&#039;; // Put your MySQL User Password 

   

   // This is the end of the Settings you need to set.
   

   mysql_connect($voting[&#039;host&#039;], $voting[&#039;db_user&#039;], $voting[&#039;db_pass&#039;]) or die(&quot;&lt;p align=\&quot;center\&quot;&gt;Error Connecting to MySQL Server&lt;/p&gt;&quot;);

   mysql_select_db($voting[&#039;db_name&#039;]) or die(&quot;&lt;p align=\&quot;center\&quot;&gt;Error selecting MySQL DB&lt;/p&gt;&quot;);

?&gt;</code></pre></div><p><strong><span class="bbu"><span style="color: red">vote_functions.php:</span></span></strong><br /></p><div class="codebox"><pre><code>   // Include the settings file.
   require(&#039;vote_settings.php&#039;);

   

   // DO NOT EDIT BELOW THIS LINE UNLESS YOU KNOW WHAT YOU AR DOING ;)
   

   // This is the main function &gt;:)

   function startvoting($page,$type) {

      // What type is it? If its GET lets do it =D

      if($type==&#039;GET&#039;) {

         $temp = $_GET[$page];

         $page = $temp;

            if(isset($_POST[&#039;vote&#039;]))

            {



               $ip = $_SERVER[&#039;REMOTE_ADDR&#039;];

               $result = mysql_query(&quot;Select * from voting_ratings where page=&#039;$page&#039;&quot;);

                  while($row = mysql_fetch_array($result)) {

                  $voting[&#039;check&#039;]=stripslashes($row[&#039;ip&#039;]);

                  }

               if($voting[&#039;check&#039;]==null)

                  {

                  $time = date(&#039;n/j/Y&#039;);

                  $rating = $_POST[&#039;rating&#039;];

                  mysql_query(&quot;INSERT INTO voting_ratings (`page`,`ip`,`time`,`rating`) VALUES(&#039;$page&#039;,&#039;$ip&#039;,&#039;$time&#039;,&#039;$rating&#039;)&quot;) or die(mysql_error());

                  echo &quot;&lt;p align=\&quot;center\&quot;&gt;Thank you! Your rating of $rating has been submitted.&lt;/p&gt;&quot;;

            }

            else

            { echo &quot;&lt;p align=\&quot;center\&quot;&gt;Sorry! You have already voted.&lt;/p&gt;&quot;; }

            }

            if(!(isset($_POST[&#039;vote&#039;])))

            {

               $result = mysql_query(&quot;SELECT * FROM voting_ratings&quot;);

                while($row = mysql_fetch_array($result)) {

                 if($row[&#039;page&#039;]==$page)

                 {

                 $voting[&#039;number&#039;]++;

                 $voting[&#039;current&#039;]=stripslashes($row[&#039;rating&#039;]);

                 $voting[&#039;total&#039;]=$voting[&#039;total&#039;]+$voting[&#039;current&#039;];

                 }

                }

                

                echo &quot;&lt;div align=\&quot;center\&quot;&gt;&quot;;

                      if($voting[&#039;number&#039;]!=null)

                      {

                      $voting[&#039;user_rating&#039;] = $voting[&#039;total&#039;]/$voting[&#039;number&#039;];

                      echo &quot;&lt;p align=\&quot;center\&quot;&gt;User Rating Average: &quot;,$voting[&#039;user_rating&#039;],&quot; from &quot;,$voting[&#039;number&#039;],&quot; votes&lt;/p&gt;&lt;br /&gt;&quot;;

                      }

                      else

                      {

                      echo&quot;&lt;p align=\&quot;center\&quot;&gt;There are no votes yet! Be the first one!&lt;/p&gt;&quot;;

                      }

                      echo &quot;&lt;form action=\&quot;\&quot; method=\&quot;POST\&quot;&gt;

                      Rating: &lt;select name=\&quot;rating\&quot;&gt;

                              &lt;option value=\&quot;1\&quot;&gt;1&lt;/option&gt;

                              &lt;option value=\&quot;2\&quot;&gt;2&lt;/option&gt;

                              &lt;option value=\&quot;3\&quot;&gt;3&lt;/option&gt;

                              &lt;option value=\&quot;4\&quot;&gt;4&lt;/option&gt;

                              &lt;option value=\&quot;5\&quot;&gt;5&lt;/option&gt;                             

                             &lt;/select&gt;

                              &lt;br /&gt;

                      &lt;input type=\&quot;submit\&quot; name=\&quot;vote\&quot; value=\&quot;Submit Rating\&quot;/&gt;

                      &lt;/form&gt;

                      &lt;/div&gt;&quot;;

                      

            }

         }



      if($type==&#039;NONE&#039;) {

            if(isset($_POST[&#039;vote&#039;]))

            {

               $ip = $_SERVER[&#039;REMOTE_ADDR&#039;];

               $result = mysql_query(&quot;Select * from voting_ratings where page=&#039;$page&#039;&quot;);

                  while($row = mysql_fetch_array($result)) {

                  $voting[&#039;check&#039;]=stripslashes($row[&#039;ip&#039;]);

                  }

               if($voting[&#039;check&#039;]==null)

                  {

                  $time = date(&#039;n/j/Y&#039;);

                  $rating = $_POST[&#039;rating&#039;];

                  mysql_query(&quot;INSERT INTO voting_ratings (`page`,`ip`,`time`,`rating`) VALUES(&#039;$page&#039;,&#039;$ip&#039;,&#039;$time&#039;,&#039;$rating&#039;)&quot;) or die(mysql_error());

                  echo &quot;&lt;p align=\&quot;center\&quot;&gt;Thank you! Your rating of $rating has been submitted.&lt;/p&gt;&quot;;

            }

            else

            { echo &quot;&lt;p align=\&quot;center\&quot;&gt;Sorry! You have already voted.&lt;/p&gt;&quot;; }

            }

            if(!(isset($_POST[&#039;vote&#039;])))

            {

               $result = mysql_query(&quot;SELECT * FROM voting_ratings&quot;);

                while($row = mysql_fetch_array($result)) {

                 if($row[&#039;page&#039;]==$page)

                 {

                 $voting[&#039;number&#039;]++;

                 $voting[&#039;current&#039;]=stripslashes($row[&#039;rating&#039;]);

                 $voting[&#039;total&#039;]=$voting[&#039;total&#039;]+$voting[&#039;current&#039;];

                 }

                }

                

                echo &quot;&lt;div align=\&quot;center\&quot;&gt;&quot;;

                      if($voting[&#039;number&#039;]!=null)

                      {

                      $voting[&#039;user_rating&#039;] = $voting[&#039;total&#039;]/$voting[&#039;number&#039;];

                      echo &quot;&lt;p align=\&quot;center\&quot;&gt;User Rating Average: &quot;,$voting[&#039;user_rating&#039;],&quot; from &quot;,$voting[&#039;number&#039;],&quot; votes&lt;/p&gt;&lt;br /&gt;&quot;;

                      }

                      else

                      {

                      echo&quot;&lt;p align=\&quot;center\&quot;&gt;There are no votes yet! Be the first one!&lt;/p&gt;&quot;;

                      }

                      echo &quot;&lt;form action=\&quot;\&quot; method=\&quot;POST\&quot;&gt;

                      Rating: &lt;select name=\&quot;rating\&quot;&gt;

                              &lt;option value=\&quot;1\&quot;&gt;1&lt;/option&gt;

                              &lt;option value=\&quot;2\&quot;&gt;2&lt;/option&gt;

                              &lt;option value=\&quot;3\&quot;&gt;3&lt;/option&gt;

                              &lt;option value=\&quot;4\&quot;&gt;4&lt;/option&gt;

                              &lt;option value=\&quot;5\&quot;&gt;5&lt;/option&gt;                             

                             &lt;/select&gt;

                              &lt;br /&gt;

                      &lt;input type=\&quot;submit\&quot; name=\&quot;vote\&quot; value=\&quot;Submit Rating\&quot;/&gt;

                      &lt;/form&gt;

                      &lt;/div&gt;&quot;;

                      

            }

         }

   }                     

?&gt;</code></pre></div><p>Installation:<br />1) Create the sql table from the db.sql.<br />2) save vote_settings.php and vote_functions.php are in the same directory.<br />3) Open up vote_settings.php and fill out the details (The MySQL user, pass, db, host etc)<br />4) Put the code(s) in your site as stated above.</p><p>NOTE: This script only allows 1 vote per IP <img src="https://codetheworld.com/img/smilies/wink.png" width="15" height="15" alt="wink" /></p>]]></description>
			<author><![CDATA[null@example.com (Terry)]]></author>
			<pubDate>Sun, 15 Jun 2008 16:29:40 +0000</pubDate>
			<guid>https://codetheworld.com/viewtopic.php?pid=167#p167</guid>
		</item>
	</channel>
</rss>
