lot bi trc, chng ta bn ti cc ng dng qun l ngi s dng, xy dng b
m, to m bo mt phng chng nhng ngi c dng xu trn website. Tip theo,
trong bi ny chng ta s bn v cch thc xy dng h thng cho php ngi dng bnh chn khi h tin hnh thm ving website ca chng ta. xy dng h thng bnh chn chng ta cn phn tch xem h thng ny cn c bao nhiu bng quan h. A- Phn tch v thit k c s d liu: Trong tnh hung ny chng ta cn 1 bng cu hi v 1 bng cu tr li. Phn tch quan h gia chng ta c: 1 Cu hi c nhiu cu tr li. 1 cu tr li ch dnh cho 1 cu hi. Vy gia 2 bng ny s pht sinh mt kha ngoi, l kha dng lin kt gia hai bng ny.
Vy ta c c php to 2 bng nh sau. Bng cu hi ( question) mysql> create table question(qid -> qtitle VARCHAR(255) NOT NU -> qdate DATE NOT NULL DEFA -> PRIMARY KEY(qid));
1 2 3 4 mysql> create table question(qid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, -> qtitle VARCHAR(255) NOT NULL, -> qdate DATE NOT NULL DEFAULT '0000-00-00', -> PRIMARY KEY(qid)); Bng cu tr li (anwser) mysql> create table answer(aid -> qid INT(10) UNSIGNED NOT N -> atitle VARCHAR(255) NOT NU -> acount INT(10) NOT NULL DE -> PRIMARY KEY(aid));
1 2 3 4 5 mysql> create table answer(aid INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, -> qid INT(10) UNSIGNED NOT NULL, -> atitle VARCHAR(255) NOT NULL, -> acount INT(10) NOT NULL DEFAULT '0', -> PRIMARY KEY(aid)); Nhp liu bng CSDL ta c: mysql> Insert into question(qtitle 2009-10-10"); mysql> Insert into answer(qid,a mysql> Insert into answer(qid,a mysql> Insert into answer(qid,a
1 2 3 4 5 6 mysql> Insert into question(qtitle,qdate) values("Ban thay qhonline the nao ?"," 2009-10-10"); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin rat dep",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin Dep",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin Cung duoc",0); mysql> Insert into answer(qid,atitle,acount) values("1","Nhin qua xau",0); B- Xy dng trang bnh chn poll.php Kt ni c s d liu: <?php $conn=mysql_connect("localho mysql_select_db("poll_exam",$c ?>
1 2 3 4 <?php $conn=mysql_connect("localhost","root","root") or die("can not connect database"); mysql_select_db("poll_exam",$conn); ?> La chn cu hi c trong c s d liu lit k ra trn website, sau ta li tip tc lit k cc cu tr li ca cu hi bng cch s dng du chn la (radio). Nh vy chng ta s phi chy cng lc 2 cu truy vn lng nhau trong sut qu trnh truy xut. <?php $sql="select * f rom question ord $query=mysql_query($sql); if (mysql_num_rows($query) > 0 {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <?php $sql="select * from question order by qid desc"; $query=mysql_query($sql); if(mysql_num_rows($query) > 0) { $row=mysql_fetch_array($query); $qid=$row[qid]; echo "<form action='poll.php?questionid=$qid' method='post' >"; echo "<h2>$row[qtitle]</h2>"; $sql2="select * from answer where qid='$qid' order by aid"; $query2=mysql_query($sql2); if(mysql_num_rows($query2) > 0) { while($row2=mysql_fetch_array($query2)){ echo "<input type=radio name=answer value=$row2[aid]>$row2[atitle]<br />"; } } echo "<input type='submit' name='ok' value='Binh Chon'>"; echo "<a href='result.php?questionid=$qid'>Xem Ket Qua</a>"; echo "</form>"; } ?> V mn hnh s xut ra nh hnh bn di:
V khi ngi dng nht nt Bnh chn, chng ta s gi li chnh trang x l d liu m ngi dng va la chn. Vy ta phi s dng lnh isset() kim tra xem ngi dng c nhn nt bnh chn khng, tip n ta ly ra id m ngi dng va tin hnh chn form bn di. Cui cng ta cp nht d liu bng cch ly s trong c s d liu cng tip cho 1 n v na. V a ngi dng sang trang kt qu. V trong on code ny c lnh header nn chng ta phi t qu trnh x l ny trn form. Nu khng s b li d liu khi gi. <?php if (isset($_POST['ok'])) { $id=$_POST['answer']; $qid=$_GET['questionid'];
1 2 3 4 5 6 7 8 9 10 11 <?php if(isset($_POST['ok'])) { $id=$_POST['answer']; $qid=$_GET['questionid']; $sql3="update answer set acount=acount + 1 where aid='".$id."'"; mysql_query($sql3); header("location: result.php?questionid=$qid"); exit(); } ?> Ton b code ca trang poll.php ny nh sau. <?php $conn=mysql_connect("localho mysql_select_db("poll_exam",$c if (isset($_POST['ok'])) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?php $conn=mysql_connect("localhost","root","root") or die("can not connect database"); mysql_select_db("poll_exam",$conn); if(isset($_POST['ok'])) { $id=$_POST['answer']; $qid=$_GET['questionid']; $sql3="update answer set acount=acount + 1 where aid='".$id."'"; mysql_query($sql3); header("location: result.php?questionid=$qid"); exit(); } $sql="select * from question order by qid desc"; $query=mysql_query($sql); if(mysql_num_rows($query) > 0) { $row=mysql_fetch_array($query); $qid=$row[qid]; echo "<form action='poll.php?questionid=$qid' method='post' >"; echo "<h2>$row[qtitle]</h2>"; $sql2="select * from answer where qid='$qid' order by aid"; $query2=mysql_query($sql2); if(mysql_num_rows($query2) > 0) 24 25 26 27 28 29 30 31 32 33 { while($row2=mysql_fetch_array($query2)){ echo "<input type=radio name=answer value=$row2[aid]>$row2[atitle]<br />"; } } echo "<input type='submit' name='ok' value='Binh Chon'>"; echo "<a href='result.php?questionid=$qid'>Xem Ket Qua</a>"; echo "</form>"; } ?> C- Xy dng trang kt qu bnh chn result.php trang ny, ta s ly gi tr question id ca ngi bnh chn t trang poll.php v tnh ton xem t l phn trm ca cc cu hi y nh th no. u tin, ta ly gi tr t poll.php?questionid=1. vy 1 l gi tr ta s c c t $_GET['questionid']. Tip n ta li dng gi tr va ly c ny lit k thng tin cu hi v tnh ton gi tr. tnh ton gi tr, ta cn bit tng s bnh chn trong ton b cc cu tr li hin nay l bao nhiu. Bng cu lnh SUM ta c th lm c iu . $sql2="select qid, SUM(acount)
1 $sql2="select qid, SUM(acount) as total from answer group by qid having qid='".$qid."'"; Cu lnh ny cho php ta ly ra tng s bnh chn trong c s d liu ca cu hi m ta truyn vo. SUM c acount ta cn phi GROUP(gom nhm) chng ta li theo m cu hi m chng ta cn tm. having l mnh theo sau c ngha tng ng vi where, n thng c s dng theo GROUP. Nh vy ly ra tng s phiu bnh chn ta ch vic thc thi cu truy vn ny, v ly tn ct tm l total. <?php $sql2="select qid, SUM(acount) $query2=mysql_query($sql2); $row2=mysql_f etch_array($qu $total=$row2[total];
1 2 3 4 <?php $sql2="select qid, SUM(acount) as total from answer group by qid having qid='".$qid."'"; $query2=mysql_query($sql2); 5 6 $row2=mysql_fetch_array($query2); $total=$row2[total]; ?> K tip, ta li tnh ton s phiu ca tng cu hi trong c s d liu, da vo tng s cu hi ny. V d: Tng s lt bnh chn l 10 cu 1 l 3 cu 2 l 4 cu 3 l 2 cu 4 l 1 Vy suy ra phn trm trn tng s 10 ca 4 cu ny c tnh bng cch ly s liu ca tng cu chia cho tng s cu hi v nhn cho 100. V d cu 1 l: (3/10)*100 = 30% lm trn kt qu ta s dng hm round(). Gip d liu a v s ng b v r rng nht. Vy code x l ca chng ta nh sau: <?php $sql3="select * f rom answer w $query3=mysql_query($sql3); if (mysql_num_rows($query3) > {
1 2 3 4 5 6 7 8 9 10 11 <?php $sql3="select * from answer where qid='".$qid."' order by aid"; $query3=mysql_query($sql3); if(mysql_num_rows($query3) > 0) { while($row3=mysql_fetch_array($query3)){ $percent=round(($row3[acount]/$total)*100,2); echo "<h4 style='color:red; font:12px verdana; '>$row3[atitle] : $row3[acount] ($percent %)</h4>"; } } ?> Kt qu khi xut ra s c hnh nh sau:
Vy ton b m ngun ca trang result.php ny nh sau: <?php $conn=mysql_connect("localho mysql_select_db("poll_exam",$c if (isset($_GET['questionid'])) {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 <?php $conn=mysql_connect("localhost","root","root") or die("can not connect database"); mysql_select_db("poll_exam",$conn); if(isset($_GET['questionid'])) { $qid=$_GET['questionid']; $sql="select * from question where qid='".$qid."'"; $query=mysql_query($sql); $row=mysql_fetch_array($query); echo "<h2>$row[qtitle]</h2>"; $sql2="select qid, SUM(acount) as total from answer group by qid having qid='".$qid."'"; $query2=mysql_query($sql2); $row2=mysql_fetch_array($query2); $total=$row2[total]; $sql3="select * from answer where qid='".$qid."' order by aid"; $query3=mysql_query($sql3); if(mysql_num_rows($query3) > 0){ while($row3=mysql_fetch_array($query3)){ $percent=round(($row3[acount]/$total)*100,2); echo "<h4 style='color:red; font:12px verdana; '>$row3[atitle] : $row3[acount] ($percent %)</h4>"; } } } ?> Download ton b m ngun ca bi ny ti y.