PHP Practice Questions: Voting

Source: Internet
Author: User

By connecting to the database, the database can be added and deleted to achieve a poll and the results of the display:

Method One:

Main Page

<! DOCTYPE html Public"-//w3c//dtd XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >{margin:0px Auto; Padding:0px; }.title{Height:50px; Margin:20px 0px 0px 20px;}.List{width:300px; Height:200px; Margin-left:20px;}.xx{Width:300px; Height:30px;}.jieguo{Width:300px; Height:200px; Margin-left:20px;}.xxnr{Width:300px; Height:30px;}</style>PHPinclude("dbda.php"); $db=NewDbda (); $sql= "SELECT * FROM Diaoyantimu"; $result=$db->query ($sql); //Topic title        Echo"<div class= ' title ' >{$result[0] [1]} </div> "; $code=$result[0] [0]; $SQLX= "SELECT * from Diaoyanxuanxiang where Timudaihao = {$code}"; $resultx=$db->query ($SQLX); $xian= ""; if(@$_get["BS"]==1)        {            $xian= "Display:none"; }        Else        {            $xian= "Display:block"; }                //div for topic options        Echo"<div class= ' list ' style= ' {$xian} ' > ';  for($i= 0;$i<Count($resultx);$i++)        {            Echo"<div class= ' xx ' >"; Echo"<input type= ' checkbox ' value= ' {$resultx[$i][0]} ' name= ' opt[] '/> '; Echo"<span>{$resultx[$i][1]}</span> "; Echo"</div>"; }        Echo"</div>"; //here's the DIV for the poll results        $xianshi= ""; if(@$_get["BS"]==1)        {            $xianshi= "Display:block"; }        Else        {            $xianshi= "Display:none"; }        Echo"<div class= ' Jieguo ' style= ' {$xianshi} ' > '; //total number of people seeking        $sqlcount= "Select sum (Numbers) from Diaoyanxuanxiang"; $attrcount=$db->query ($sqlcount);  for($j= 0;$j<Count($resultx);$j++)        {            $rs=$resultx[$j][2]; if($attrcount[0] [0]==0)            {                $BFB= 0; }            Else            {                $BFB= ($rs/$attrcount[0] [0]) *100; }                        Echo"<div class= ' Xxnr ' >"; Echo"<span style= ' Float:left ' >{$resultx[$j][1]}</span> "; Echo"<div style= ' float:left;margin:10px 0px 0px 10px;width:100px, height:4px, border:1px solid #000 ' > < Div style= ' width:{$BFB}%; height:4px;margin-left:0px; Background-color: #666 ' ></div> </div>"; Echo"<span style= ' float:left; margin-left:10px ' >{$resultx[$j][2]}</span> "; Echo"</div>"; }                                Echo"</div>"; ?> <div style= "margin-left:20px; width:200px "> <input id=" TJ "type=" submit "style=" float:left;<?php Echo$xian;? >;margin:0px 0px 0px 10px "value=" Submit "/> <input id=" FH "type=" button "style=" float:left;<?php Echo$xianshi;? >;margin:0px 0px 0px 10px "onclick=" SHOWTP () "value=" return "/> <input type=" button "style=" Float:left;display : block;margin:0px 0px 0px 10px "onclick=" Showjieguo () "value=" Show results "/> </div> </form></body>&lt Script type= "Text/javascript" >functionShowjieguo () {var List= Document.getelementsbyclassname ("list"); varJieguo = Document.getelementsbyclassname ("Jieguo"); varTJ = document.getElementById ("TJ")); varFH = document.getElementById ("FH"); FH. style.display= "Block"; TJ. style.display= "None"; List[0].style.display= "None"; jieguo[0].style.display = "Block";}functionSHOWTP () {var List= Document.getelementsbyclassname ("list"); varJieguo = Document.getelementsbyclassname ("Jieguo"); varTJ = document.getElementById ("TJ")); varFH = document.getElementById ("FH"); FH. style.display= "None"; TJ. style.display= "Block"; List[0].style.display= "Block"; jieguo[0].style.display = "None";}</script>View Code

Process the page, receive the votes cast and make the processing

<?PHPinclude("dbda.php");$db=NewDbda ();$attr=$_post["Opt"]; for($i= 0;$i<Count($attr);$i++){        $sql= "Update Diaoyanxuanxiang set Numbers = numbers+1 where Ids = {$attr[$i]}"; $db->query ($sql, 0);}Header("Location:test.php?bs=1");
View Code

Create a class that accesses the database, encapsulating it for reference

<?PHPclassdbda{ Public $host= "localhost";//Server Address     Public $uid= "Root";//user name of the database     Public $pwd= "123";//Database Password//Execute SQL statement, return the corresponding result of the function//$sql is to execute the SQL statement//$type is the type of SQL statement, 0 for adding or deleting, 1 for the query//$DB represents the database to operate     Public functionQuery ($sql,$type=1,$db= "MyDB")    {        //Connecting Objects        $conn=NewMysqli ($this->host,$this->uid,$this->pwd,$db); //determine if the connection is successful!Mysqli_connect_error() or die("Connection Failed! "); //Execute SQL statement        $result=$conn->query ($sql); //determine the type of SQL statement        if($type==1)        {            //If the query statement returns a two-dimensional array of result sets            return $result-Fetch_all (); }        Else        {            //returns TRUE or False if it is a different statement            return $result; }    }    }
View Code

Method Two:

PHP Practice Questions: Voting

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.