The php query operation supports voting and php query and voting. The php query operation implements the voting function. The php query and voting function is used in this article to share the code of the php query operation to implement the voting function for your reference. the specific content is as follows: the php query operation supports voting and php query and voting.
The example in this article shares with you the code for the php query operation to implement the voting function for your reference. the specific content is as follows:
Question:
Summary of solutions:
Method 1:
1. voting homepage:
Vote <? Php // calculates the total number of users: $ sqlzs = "select sum (numbers) from diaoyanxuanxiang where timudaihao = '{$ tmdh }'"; $ zrs = $ db-> query ($ sqlzs); foreach ($ arrxx as $ v) {$ name = $ v [1]; $ number = $ v [2]; if ($ zrs [0] [0] = 0) {$ bfb = 0;} else {$ bfb = ($ number/$ zrs [0] [0]) * 100;} $ bfb = round ($ bfb, 2); echo"
{$ Name}
{$ Number} {$ bfb} %
";}?>Script function Showjieguo () {document. getElementById ("list "). style. display = "none"; document. getElementById ("jieguo "). style. display = "block";} function Showfanhui () {document. getElementById ("list "). style. display = "block"; document. getElementById ("jieguo "). style. display = "none";} script
2. process the voting page:
<? Php $ arr = $ _ POST ["xx"]; include (".. /DBDA. class. php "); $ db = new DBDA (); foreach ($ arr as $ v) {$ SQL = "update diaoyanxuanxiang set numbers = numbers + 1 where ids = '{$ v}'"; $ db-> Query ($ SQL, 1 ); // 1 indicates the $ SQL type} header ("location: TouPiao. php ");?>
3. create a database access class and encapsulate it for reference:
<? Php // execute an SQL statement and return the corresponding result class DBDA {public $ host = "localhost"; // database server address public $ uid = "root "; // database username public $ password = ""; // database password // The method for executing the SQL statement // $ SQL indicates the SQL statement to be executed; $ type is the type of SQL statement. 0 indicates Query, and 1 indicates other statements (add, delete, modify, and delete). $ db indicates the function Query of the database to be operated ($ SQL, $ type = 0, $ db = "mydb") {// create a connection object $ dbconnect = new MySQLi ($ this-> host, $ this-> uid, $ this-> password, $ db); // determines whether the connection has an error! Mysqli_connect_error () or die ("connection failed! "); // Execute the SQL statement $ result = $ dbconnect-> query ($ SQL); // Determine the SQL statement type if ($ type = 0) {// if it is a two-dimensional array return $ result-> fetch_all ();} else {// if it is another statement, return true or false return $ result ;}}}
Method 2:
1. voting homepage:
Vote
2. process the voting page:
<? Php $ arr = $ _ POST ["xx"]; include (".. /DBDA. class. php "); $ db = new DBDA (); foreach ($ arr as $ v) {$ SQL = "update diaoyanxuanxiang set numbers = numbers + 1 where ids = '{$ v}'"; $ db-> Query ($ SQL, 1 ); // 1 indicates the $ SQL type} header ("location: TouPiao. php ");?>
3. view the voting result page:
View Results<? Phpinclude (".. /DBDA. class. php "); $ db = new DBDA (); // find the question code and name from the Survey table $ SQL =" select * from diaoyantimu limit "; $ arr = $ db-> query ($ SQL); $ tmmc = $ arr [0] [1]; echo"{$ Tmmc }:
"; // Output option content from the survey option table: $ sqlxx =" select * from diaoyanxuanxiang where timudaihao = '{$ arr [0] [0]}' "; $ arrxx = $ db-> query ($ sqlxx); // calculates the total number of users: $ sqlzs = "select sum (numbers) from diaoyanxuanxiang where timudaihao = '{$ arr [0] [0]}' "; $ zrs = $ db-> query ($ sqlzs); foreach ($ arrxx as $ v) {$ name = $ v [1]; // survey project name $ number = $ v [2]; // select the number of people for this item // Determine whether the total number is 0 if ($ zrs [0] [0] = 0) {$ bfb = 0 ;} else {$ bfb = ($ number/$ zrs [0] [0]) * 100; // calculates the percentage} $ bfb = round ($ bfb, 2 ); // Two digits after the decimal point echo"{$ Name}
{$ Number} {$ bfb} %
";}?>
Display result:
The above is all the content of this article. I hope it will help you learn php programming.
Examples in this article share with you the code for the php query operation to implement the voting function for your reference. the specific content is as follows: solution...