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

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

烟雨青岚
烟雨青岚forward
2020-07-16 11:38:293457browse

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);
        ?>

2. Display of basic information of voting activities

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

                                 

                

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>";
					}
                	?>

(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>

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete