WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PHP Polling System with Mysql Database
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs Click here to Update Your Picture
Suraj Thapaliya
Date : Apr 26th 2006
Grade : 3 of 5 (graded 4 times)
Viewed : 6118
File : 4387.zip
Images : No Images for this code example.
Search : More code by Suraj Thapaliya
Action : Grade This Code Example
Tools : My Examples List

 
Like this code?
Show the author your appreciation.
Submit your own code examples 
 

This is the simple but more attractive polling system ver 1.0 in this version i can't add the admin section by the next version it will be avaiable.

The include files, sql statement are also found in the attached file.


host_conf.php
<?
    $host
="localhost";
   
$user_name="username";
   
$password="password";
   
$db="database";

   
define("C_DB_HOST",$host);
   
define("C_DB_USER",$user_name);
   
define("C_DB_PASS",$password);
   
define("C_DB_NAME",$db);
       
   
// Table Defination
   
define("TBL_NEWS","huforn_news");
?>



mysql.lib.php
<?php
/*******************
Mr. Suraj Thapaliya
PHP Programmer
http://www.surajthapaliya.com.np
*/
class db
{
    var
$Host = C_DB_HOST;            // Hostname of our MySQL server
   
var $Database = C_DB_NAME;        // Logical database name on that server
   
var $User = C_DB_USER;            // Database user
   
var $Password = C_DB_PASS;        // Database user's password
   
var $Link_ID = 0;                // Result of mysql_connect()
   
var $Query_ID = 0;                // Result of most recent mysql_query()
   
var $Record    = array();            // Current mysql_fetch_array()-result
   
var $Row;                        // Current row number
   
var $Errno = 0;                    // Error state of query
   
var $Error = "";

    function
halt($msg)
    {
        echo(
"</TD></TR></TABLE><B>Database error:</B> $msg<BR>\n");
        echo(
"<B>MySQL error</B>: $this->Errno ($this->Error)<BR>\n");
        die(
"Session halted.");
    }

    function
connect()
    {
        if(
$this->Link_ID == 0)
        {
           
$this->Link_ID = mysql_connect($this->Host, $this->User, $this->Password);
            if (!
$this->Link_ID)
            {
               
$this->halt("Link_ID == false, connect failed");
            }
           
$SelectResult = mysql_select_db($this->Database, $this->Link_ID);
            if(!
$SelectResult)
            {
               
$this->Errno = mysql_errno($this->Link_ID);
               
$this->Error = mysql_error($this->Link_ID);
               
$this->halt("cannot select database <I>".$this->Database."</I>");
            }
        }
    }

    function
query($Query_String)
    {
       
$this->connect();
       
$this->Query_ID = mysql_query($Query_String,$this->Link_ID);
       
$this->Row = 0;
       
$this->Errno = mysql_errno();
       
$this->Error = mysql_error();
        if (!
$this->Query_ID)
        {
           
$this->halt("Invalid SQL: ".$Query_String);
        }
        return
$this->Query_ID;
    }
   
function
query_fetch($fetch=0)
{
    if(
$fetch==0) {
       
$result=@mysql_fetch_assoc($this->Query_ID);
    } else {
       
$result=@mysql_fetch_array($this->Query_ID);
    }
   
    if(!
is_array($result))
    return
false;
   
$this->total_field=mysql_num_fields($this->Query_ID);

    foreach(
$result as $key=>$val){
       
$result[$key]=trim(htmlspecialchars($val));
    }
     return
$result;
}

function
num_field()
{
    return
mysql_num_fields($this->Query_ID);
}
/*
function fetch_field()
{
    return mysql_fetch_field($this->Query_ID,2);
}
*/
function next_record()
    {
       
$this->Record = mysql_fetch_array($this->Query_ID);
       
$this->Row += 1;
       
$this->Errno = mysql_errno();
       
$this->Error = mysql_error();
       
$stat = is_array($this->Record);
        if (!
$stat)
        {
           
mysql_free_result($this->Query_ID);
           
$this->Query_ID = 0;
        }
        return
$this->Record;
    }

    function
num_rows()
    {
        return
mysql_num_rows($this->Query_ID);
    }
    function
maxRow($tablename,$field)
    {
       
$sql="select max($field) from $tablename";
       
$this->query($sql);
       
$result=@mysql_fetch_array($this->Query_ID);
        return
$result[0];
    }
    function
affected_rows()
    {
        return
mysql_affected_rows($this->Link_ID);
    }

    function
optimize($tbl_name)
    {
       
$this->connect();
       
$this->Query_ID = @mysql_query("OPTIMIZE TABLE $tbl_name",$this->Link_ID);
    }

    function
clean_results()
    {
        if(
$this->Query_ID != 0) mysql_freeresult($this->Query_ID);
    }

    function
close()
    {
        if(
$this->Link_ID != 0) mysql_close($this->Link_ID);
    }
}
?>



SQL Structure
CREATE TABLE polling (
  poll varchar(50) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `polling`
--

INSERT INTO polling VALUES ('What is the main objective of Democracy');
INSERT INTO polling VALUES ('Peace');
INSERT INTO polling VALUES ('Constitution');
INSERT INTO polling VALUES ('Republic');
INSERT INTO polling VALUES ('Nation');



CREATE TABLE poll (
  result varchar(100) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

--
-- Dumping data for table `poll`
--

INSERT INTO poll VALUES ('Constitution');
INSERT INTO poll VALUES ('Constitution');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Nation');
INSERT INTO poll VALUES ('Republic');
INSERT INTO poll VALUES ('Department of Employment');
INSERT INTO poll VALUES ('Constitution');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Constitution');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');
INSERT INTO poll VALUES ('Peace');


poll.php
<table width="550" border="0" align="center" cellpadding="0" cellspacing="0">
                <tr>
                  <td width="275" valign="top" style="border-right:1.5px solid #cccccc;">
                  <form name="form1" method="post" action="<? $PHP_SELF ?>">
                    <table width="275"  border="0" align="center" cellpadding="0" cellspacing="0">
                      <?
include ("lib/host_conf.php");
include (
"lib/mysql.lib.php");
$objp=new db;
$objp->connect();

$query="select * from polling";
$objp->query($query);
$i=0;
$option=array();
while(
$row=$objp->query_fetch(1))
   {
   
$result=$row[0];
   
$option[$i]=$result;
   if(
$i==0)
    {
?>
                      <tr>
                        <td colspan="2"><? echo("$row[0]"); ?></td>
                      </tr>
                      <?  }
    else
      {
?>
                      <tr>
                        <td valign="bottom">
                                <input name="poll" type="radio" class="pollingcheckbox" value="<? echo($result);?>">
                                <? echo("$option[$i]");?>
                                <?
       
}
       
$i++;
        }
       
       
?>
                                <br>
                        </td>
                      </tr>
                      <tr>
                        <td colspan="2"> 
                            <input name="Submit2" type="submit" class="newsdate" value="Vote">
  </td>
                      </tr>
                      <?
$result
=$_POST["poll"];
if(
strlen($result)>1)
{
$query_insert="insert into poll (result) values ('$result')";
$objp->query($query_insert);
}


?>
                    </table>
                  </form></td>
                  <td width="275" valign="top"><?
               
echo "<table width=275>";
           
           
$obj=new db;
           
$obj->connect();
           
$obj1=new db;
           
$obj1->connect();
$vote=array();
$vote_option=array();
$perc=array();
$i=0;
$total=0;
$query="select * from polling LIMIT 1,10";
$obj->query($query);
while(
$row=$obj->query_fetch(1))
    {
   
$option=$row["poll"];
   
$vote_option[$i]=$option;
   
$query_result="select COUNT(result) from poll where result='$option'";
   
$obj1->query($query_result);
    while(
$row_result=$obj1->query_fetch(1))
         {
         
$vote[$i]=$row_result[0];
         }
   
$total=$total+$vote[$i];
   
$i++;
    }

           
?>
                    <?
   
for($j=0;$j<$i;$j++)
        {
       
$perc[$j]=(($vote[$j]/$total)*100);
       
?>
                    <?
       
echo "<tr><td>".($vote_option[$j]);
       
?>
                    <td><img src="picture/poll.gif" width="<? echo($perc[$j]); ?>" height="8">
                    <?

           
echo "<td>".($vote[$j]."  votes")."</tr>";
            }
            echo
"<tr><td colspan=2> <class=pollingresultred><strong>Total Votes: $total</strong></tr>";
            echo
"</table>";
       
?></td>
                </tr>
                <tr>
                  <td colspan="2"> </td>
                </tr>
                          </table>



Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Create and restore backup of MySQL databases
Categories : MySQL, Databases, PHP, PHP Classes, Complete Programs
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
Accepts a database & hostname from a user and then HTTP username and password. Uses this to connect to a MySQL database. Produces a form based on the tables it finds there to allow the user to do SELECTs, INSERTs, and DELETEs.
Categories : Databases, PHP, MySQL, Complete Programs
phpAds, a complete banner and ad management system with detailled tracking and stats.
Categories : MySQL, Complete Programs, Ecommerce, PHP, Databases
Point and Click Interface ala MS Access for creating SQL statements.
Categories : MySQL, Complete Programs, General SQL, PHP, Databases
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
Browse a MySQL database & draw a tree view & load final items into a template page.
Categories : MySQL, Complete Programs, Algorithms, PHP, Databases
Shopping Basket On-Line Ordering System.
Categories : Complete Programs, MySQL, PHP, Ecommerce, Databases
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides