WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
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 Resources
Web Development Content
Internet Security Software
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
פרייסז - הכח לקנות עובר לידיים שלך
Texas Holdem Poker Evangelists

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
Suraj Thapaliya
Date : Apr 26th 2006
Grade : 4 of 5 (graded 7 times)
Viewed : 15019
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

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

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>



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
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
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
A script to generate a report from a valid mysql connection. The user has to supply which fields he wants to display in table. All properties are changable.
Categories : PHP, PHP Classes, Databases, MySQL, HTML and PHP
Education Center is a set of PHP-scripts to administer a corporate education and examination system via Internet/intranet written in PHP for MySQL.
Categories : PHP, Databases, MySQL, Complete Programs
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
myCSV-dump converts a simple CSV-flatfile-database into an MySQL-dump.
Categories : PHP, MySQL, Databases, Complete Programs
AITSH Download
Categories : PHP, Complete Programs, MySQL, Databases
Convert SQL from oracle,mysql,mssql,sqlite and odbc to SQL compatible
Categories : PHP, PHP Classes, Databases, MySQL, MS SQL Server
DDN FFA Network Script
Categories : PHP, MySQL, Complete Programs, HTML and PHP, Databases
Web Self Service Resource Scheduler Using Session Variables under php4 includes Calendar building code - requires MySQL
Categories : PHP, Complete Programs, Calendar, MySQL, Databases
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
DirtSearch Version 3.5 full function robust PHP and MySQL (and other databases) Site or Web Wide Search Engine
Categories : PHP, MySQL, Complete Programs, Search, Databases