Bài viết Code php bình chọn

Thảo luận trong 'PHP' bắt đầu bởi white.smut, 26 Tháng mười hai 2012.

  1. Offline

    white.smut

    • Administrator

    • Loading: |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||] 99% Completed - Error: Disconnected. Please try again!
    Số bài viết:
    842
    Đã được thích:
    425
    Điểm thành tích:
    450
    Chúng ta sẽ có những phần sau:
    Phần câu hỏi:
    PHP:
        mysqlcreate table question(qid INT(10UNSIGNED NOT NULLAUTO_INCREMENT,
        -> 
    qtitle VARCHAR(255NOT NULL,
        -> 
    qdate DATE NOT NULL DEFAULT '0000-00-00',
        -> 
    PRIMARY KEY(qid));
    Phần trả lời:
    PHP:
        mysqlcreate table answer(aid INT(10UNSIGNED NOT NULLAUTO_INCREMENT,
        -> 
    qid INT(10UNSIGNED NOT NULL,
        -> 
    atitle VARCHAR(255NOT NULL,
        -> 
    acount INT(10NOT NULL DEFAULT '0',
        -> 
    PRIMARY KEY(aid));
    Nhập dữ liệu bằng CSDL:
    PHP:
        mysqlInsert into question(qtitle,qdatevalues("Ban thay website the nao ?","
        2009-10-10"
    );
        
    mysqlInsert into answer(qid,atitle,acountvalues("1","Nhin rat dep",0);
        
    mysqlInsert into answer(qid,atitle,acountvalues("1","Nhin Dep",0);
        
    mysqlInsert into answer(qid,atitle,acountvalues("1","Nhin Cung duoc",0);
        
    mysqlInsert into answer(qid,atitle,acountvalues("1","Nhin qua xau",0);
    Trang bình chọn (poll.php):
    PHP:
        <?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)
        {
        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>";
        }
        
    ?>
    Trang kết quả (result.php):
    PHP:
        <?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>";
      }
    }
        }
        
    ?>
    Chúc bạn thành công!
    hongoctrien thích bài này.

Chia sẻ trang này

Advertising: Linux system admin | nukeviet | nukeviet 4 | Upload ảnh miễn phí