Home > Backend Development > PHP Tutorial > Learn a simple online voting system (php) in one hour

Learn a simple online voting system (php) in one hour

烟雨青岚
Release: 2023-04-09 08:28:01
forward
3533 people have browsed it

Learn a simple online voting system (php) in one hour

The function of the voting results page is to display the current voting results based on the voting statistics. Can only be viewed if logged in.

1. Database connection part

<?php         if($ouser == &#39;&#39;){
            echo "<script>alert('请先登录!');location.href='index.php';";
        }else{
            $voteid=@$_GET['vid'];
            if($voteid == ''){
                echo "location.href='index.php';";
            }else{
                $sql="select vname,starttime,uid,vintro,open,more,max,min from vote where vid='".$voteid."'";
                $voteitem=mysql_query($sql);
                $vitem=mysql_fetch_array($voteitem);

                $sql2="SELECT itemcount,item,num FROM votetitle where vid='".$voteid."'";
                $votetitle=mysql_query($sql2);
                $vtitle=mysql_fetch_array($votetitle);

                $sql3="SELECT username FROM users where uid='".$vitem['uid']."'";
                $uname=mysql_query($sql3);
                $username=mysql_fetch_row($uname);
        ?>
Copy after login

2. Display of basic information of voting activities

<center><h2><?php  echo $vitem[&#39;vname&#39;]; ?></h2></center>
            <p>
            	</p>
Copy after login
                                                                             
发布人: 发布时间:
                

                                 

                

3. Output the results one by one

<?php                 	$itemcount=$vtitle[&#39;itemcount&#39;];
					$option=explode(&#39;*&#39;,$vtitle[&#39;item&#39;]);
					$ballot=explode(&#39;*&#39;,$vtitle[&#39;num&#39;]);
					$total=0;
					$rate;
					$ratewidth;
					for($i=0;$i<count($ballot);$i++){
						$total=$total+$ballot[$i];
					}
					for($i=0;$i<count($ballot);$i++){
						if($total==0){
							$rate[$i]=0;
							$ratewidth[$i]=0;
						}else{
							$rate[$i]=round(($ballot[$i]/$total)*100);
							$ratewidth[$i]=$rate[$i]*2;
						}
					}
					for($i=0;$i<$itemcount;$i++){
						echo "<p class=&#39;vote_contend_item&#39;> ".$option[$i]."".
							"<p></p><p></p>".
							"<p> 票数:".$ballot[$i]." 得票率:".$rate[$i]."%</p>";
					}
                	?>
Copy after login

(1) Calculate the vote rate through the method and form a chart

for($i=0;$i<count for if><p><strong>4, Effect</strong></p>
<p><img src="https://img.php.cn/upload/image/376/567/418/1594870264994704.png" title="1594870264994704.png" alt="Learn a simple online voting system (php) in one hour"></p>
<p>Thank you all for reading, I hope you will benefit from it. </p>
<p> Recommended tutorial: "<a href="https://www.php.cn/php-weizijiaocheng.html" target="_blank">PHP Tutorial</a></p></count>
Copy after login

The above is the detailed content of Learn a simple online voting system (php) in one hour. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template