国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > PHP > 正文

用PHP來(lái)制作調(diào)查

2024-05-04 23:05:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
  有什么問(wèn)題請(qǐng)與我聯(lián)系:http://www.survivalescaperooms.com [email protected]
 轉(zhuǎn)載請(qǐng)注明出處

  今天給大家講一個(gè)如何用php做調(diào)查的例子,通過(guò)這個(gè)例子你可以學(xué)會(huì)如何用php和mysql來(lái)實(shí)現(xiàn)編程,這個(gè)例子不難,希望您能有收獲。

  首先創(chuàng)建一個(gè)mysq
數(shù)據(jù)庫(kù)的表,表中具體包括的字段及設(shè)置如下:

# table structure for table poll_data
#
create table poll_data (
id tinyint(4) not null auto_increment,
option1 tinyint(4) not null default '0',
option2 tinyint(4) not null default '0',
option3 tinyint(4) not null default '0',
votes tinyint(4) not null default '0',
title varchar(25) not null default '',
question varchar(50) not null default '',
primary key (id)
) type=myisam;
  建立好了數(shù)據(jù)庫(kù)表之后,我們就要開(kāi)始寫(xiě)代碼來(lái)解決這個(gè)問(wèn)題了。通常我們解決一個(gè)問(wèn)題,愿意把這個(gè)問(wèn)題分解,從而可以各個(gè)擊破,以至于整個(gè)問(wèn)題得到解決。下面我們就利用這種方法來(lái)編寫(xiě)程序代碼。
  這個(gè)調(diào)查的主要功能是:
  1.顯示調(diào)查的標(biāo)題和調(diào)查的問(wèn)題。
  2.顯示每個(gè)投票項(xiàng)目的標(biāo)題, 總投票的百分?jǐn)?shù), 每個(gè)項(xiàng)目的投票的百分?jǐn)?shù)。
  3.創(chuàng)建一個(gè)允許在調(diào)查中投票的程序文件。
  下面我們把問(wèn)題分解一下:
  1.顯示調(diào)查的標(biāo)題和調(diào)查的問(wèn)題。
    a. 連接數(shù)據(jù)庫(kù)代碼
    b. 從數(shù)據(jù)庫(kù)中選擇最新的記錄。
  2.顯示每個(gè)投票項(xiàng)目的標(biāo)題, 總投票的百分?jǐn)?shù), 每個(gè)項(xiàng)目的投票的百分?jǐn)?shù)。
    a. 為每個(gè)項(xiàng)目能選擇投票數(shù),并且能在調(diào)查中顯示總的投票數(shù)。
    b. 投票數(shù)和總數(shù)相除能夠得出百分?jǐn)?shù)。
  3.創(chuàng)建一個(gè)允許在調(diào)查中投票的程序文件。
    a. 用戶投票之后結(jié)果要更新

  我們使用一些變量來(lái)幫助我們連接數(shù)據(jù)庫(kù)。代碼如下:
  $dbhost = "localhost";
  $dbname = "yourdatabase";
  $dbuser = "yourusername";
  $dbpass = "yourpass";

  然后我們開(kāi)始連接數(shù)據(jù)庫(kù):
  $link_id = mysql_connect($dbhost, $dbuser, $dbpass);
  mysql_select_db($dbname);
  現(xiàn)在我們所做的就是具體有關(guān)數(shù)據(jù)庫(kù)的內(nèi)容了,要求數(shù)據(jù)庫(kù)能自動(dòng)增加記錄,并且程序取出的是最新的記錄,id能自動(dòng)升序,數(shù)據(jù)庫(kù)代碼如下:

$sql = "select `title`,`question`,`id` from `poll_data` order by `id` desc limit 1";
if(!($result = mysql_query($sql))) die(mysql_error());
$polldata = mysql_fetch_array($result)

下面我們來(lái)具體選擇投票的項(xiàng)目和計(jì)算具體的百分?jǐn)?shù)!

$sql = "select `option1`,`option2`,`option3`,`votes` from `poll_data` where `id` = '" . $polldata['id'] . "' order by `id` desc limit 1";
if(!($result = mysql_query($sql))) die(mysql_error());
if(!($votedata = mysql_fetch_array($result))) die(mysql_error());

  現(xiàn)在我來(lái)具體實(shí)現(xiàn)百分?jǐn)?shù)的代碼,當(dāng)然我們這的百分?jǐn)?shù)為了顯示更加具體形象,可以顯示圖形來(lái)代替!
if($votedata["option1"] != 0) {
$votepercent1 = round(($votedata["option1"] / $votedata["votes"]) * 100) . "%";
} else {
$votepercent1 = 0 ."%";
}
if($votedata["option2"] != 0) {
$votepercent2 = round(($votedata["option2"] / $votedata["votes"]) * 100) . "%";
} else {
$votepercent2 = 0 ."%";
}
if($votedata["option3"] != 0) {
$votepercent3 = round(($votedata["option3"] / $votedata["votes"]) * 100) . "%";
} else {
$votepercent3 = 0 ."%";
}

  我們這步做一個(gè)20象素高1象素寬的任何顏色的圖片。我們?cè)O(shè)置代碼來(lái)用圖象表示百分?jǐn)?shù):
<html>
<head>
<title>basic poll - written by VeVb.com</title>
</head>
<body>
<form method="post" action="vote.php">
<table width="500" border="1" cellspacing="0" cellpadding="8">
<tr>
<td colspan="3"><b><?=$polldata['title']?> - <?=$polldata['question']?></b></td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="vote" value="option1">
yes</td>
<td width=60%>
<img src="http://www.survivalescaperooms.com/htmldata/2005-03-04/bar.gif" width="<?=$votepercent1?>" height="20">
</td>
<td><?=$votedata["option1"]?> votes</td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="vote" value="option2">
no </td>
<td width=60%>
<img src="http://www.survivalescaperooms.com/htmldata/2005-03-04/bar.gif" width="<?=$votepercent2?>" height="20">
</td>
<td><?=$votedata["option2"]?> votes</td>
</tr>
<tr>
<td width="35%">
<input type="radio" name="vote" value="option3" >
not sure</td>
<td width="60%">
<img src="http://www.survivalescaperooms.com/htmldata/2005-03-04/bar.gif" width="<?=$votepercent3?>" height="20">
</td> <td><?=$votedata["option3"]?> votes</td>
</tr>
<tr>
<td colspan="3">
<center>
<input type="submit" name="submit" value="vote">
</center>
</td>
</tr>
</table>
</form>
</body>
</html>

  現(xiàn)在我們來(lái)設(shè)置用戶的投票的代碼,當(dāng)有投票之后,我們就自動(dòng)計(jì)算百分?jǐn)?shù),和更新數(shù)據(jù)庫(kù),我們使用一個(gè)變量來(lái)接收投票: $_post.

if(empty($_post["vote"])) die("you did not enter your vote");

  我們選擇表中的最新的記錄 。

$dbhost = "localhost";
$dbname = "misc";
$dbuser = "root";
$dbpass = "trigger";

$link_id = mysql_connect($dbhost, $dbuser, $dbpass);
mysql_select_db($dbname);

$sql = "select `option1`,`option2`,`option3`,`votes`,`id` from `poll_data` order by `id` desc limit 1";
if(!($result = mysql_query($sql))) die(mysql_error());
if(!($polldata = mysql_fetch_array($result))) die(mysql_error());

  我們更新數(shù)據(jù)表中的記錄。我們?cè)O(shè)置1為當(dāng)前的投票數(shù), 總的投票數(shù)也設(shè)置為1. 然后我們通過(guò)程序來(lái)實(shí)現(xiàn)把提交的數(shù)據(jù)來(lái)更新數(shù)據(jù)表中的記錄:

if($_post["vote"] == "option1") {
$votes1 = $polldata["option1"] + 1;
$totalvotes = $polldata["votes"]+ 1;

$sql = "update `poll_data` set `option1`='$votes1', `votes`='$totalvotes' where `id` = '". $polldata['id'] . "' limit 1";

if(!($result = mysql_query($sql))) die(mysql_error());
echo "vote successful! <a href=/"index.php/">back</a> to the poll.";
}
else if ($_post["vote"] == "option2"){
$votes2 = $polldata["option2"] + 1;
$totalvotes = $polldata["votes"]+ 1;

$sql = "update `poll_data` set `option2`='$votes2', `votes`='$totalvotes' where `id` = '". $polldata['id'] . "' limit 1";
if(!($result = mysql_query($sql))) die(mysql_error());
echo "vote successful! <a href=/"index.php/">back</a> to the poll.";

}
else {
$votes3 = $polldata["option3"] + 1;
$totalvotes = $polldata["votes"] + 1;

$sql = $sql = "update `poll_data` set `option3`='$votes3', `votes`= '$totalvotes' where `id` = '". $polldata['id'] . "' limit 1";
if(!($result = mysql_query($sql))) die(mysql_error());
echo "vote successful! <a href=/"index.php/">back</a> to the poll.";

}

  好了整個(gè)程序到這就結(jié)束了,希望您在看這個(gè)程序時(shí)能掌握一些技巧和學(xué)到一些知識(shí)。謝謝!國(guó)內(nèi)最大的酷站演示中心!
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 大名县| 道孚县| 谢通门县| 迁西县| 肃北| 黄大仙区| 长宁区| 磴口县| 张家界市| 慈溪市| 昭平县| 瓦房店市| 长兴县| 慈利县| 封开县| 洪江市| 丰都县| 湘阴县| 山东| 怀化市| 平阳县| 文水县| 白朗县| 亳州市| 连平县| 衡山县| 易门县| 贡觉县| 清涧县| 辉县市| 邓州市| 应用必备| 白沙| 卓资县| 岗巴县| 祁东县| 淳化县| 志丹县| 荥经县| 南溪县| 镇雄县|