Home  >  Article  >  Backend Development  >  php voting (survey)_PHP tutorial

php voting (survey)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:49:08789browse

/**
* Poll
*
*
**/ 
include "isLogin.php"; 
include "../conn/config.inc.php"; 
/*action操作初始化*/ 
if(emptyempty($_REQUEST['action'])){ 
    $_REQUEST['action']='list'; 
}else { 
    $_REQUEST['action']=trim($_REQUEST['action']); 

 
if($_REQUEST['action']=='add'){ 
    $tpl->assign('submitButton','添加'); 
    $tpl->assign('tmess','添加投票选项'); 
    $tpl->assign('act','insert'); 
    $tpl->display('admin/addpoll.tpl'); 
}elseif ($_REQUEST['action']=='insert'){ 
    $title=$_POST['title']; 
    $desc=$_POST['desc']; 
    $addtime=time(); 
    $poll=$_POST['poll']; 
 
    $sql="insert into  poll (title,des,addtime) values ('$title','$desc','$addtime')"; 
    $rs=$db->query($sql); 
    $lastid=$db->insert_id(); 
 
   foreach ($poll as $v){ 
   $sql2="insert into poll_option (pollid,opdata) values ('$lastid','$v')"; 
   $rs2=$db->query($sql2); 
     } 
    if($rs&&$rs2){ 
        echo "<script>location.href='poll.php?action=polllist'</script>"; 
    }else{ 
        echo "<script>alert('失败!');history.go(-1)</script>"; 
    } 

//投票列表 
elseif ($_REQUEST['action']=='polllist'){ 
//  $sql="select * from poll order by id desc"; 
//  $rs=$db->query($sql); 
//  $poll=$db->fetch_array($rs); 
    $poll=getpoll(); 
    $tpl->assign('poll',$poll); 
    $tpl->assign('tmess','投票列表'); 
    $tpl->display('admin/polllist.tpl'); 

//删除 
elseif ($_REQUEST['action']=='del'){ 
    $id=$_GET['id']; 
     $sql="delete from poll where id={$id}"; 
     $sql2="delete from poll_option where pollid={$id}"; 
//echo  $sql="delete from poll INNER JOIN poll_option ON poll_option.pollid=poll.id where id={$id}"; 
    $rs=$db->query($sql); 
    $rs2=$db->query($sql2); 
    if($rs&&$rs2){ 
        echo "<script>location.href='poll.php?action=polllist'</script>"; 
    }else{ 
        echo "<script>alert('失败!');history.go(-1)</script>"; 
    } 

elseif ($_REQUEST['action']=='edit'){ 
    $id=intval($_GET['id']); 
//  $sql="SELECT p.id, p.title, p.des, p.addtime, po.id, po.pollid, po.opdata 
//         FROM poll AS p 
//         LEFT JOIN poll_option AS po ON p.id = po.pollid 
//         WHERE p.id ={$id}"; 
    $sql="select * from poll where id={$id}"; 
    $rs=$db->query($sql); 
    $row=$db->fetch_array($rs); 
    $sql2="select* from poll_option where pollid={$id} order by id ASC"; 
    $rs2=$db->query($sql2); 
    $i=0; 
    $potion=''; 
    while ($row2=$db->fetch_array($rs2)){ 
        $i++; 
        $potion.="

"; 
        if($i>2){ 
        $potion.=""; 
        } 
        $potion.="
"; 
    } 
    $tpl->assign('pp',$potion); 
    $tpl->assign('post',$row); 
    $tpl->assign('tmess','更新投票'); 
    $tpl->assign('submitButton','更新'); 
    $tpl->assign('act','update'); 
    $tpl->display('admin/editpoll.tpl'); 

//更新投票信息  www.2cto.com
elseif ($_REQUEST['action']=='update'){ 
     
    $id=$_POST['id']; 
    $title=$_POST['title']; 
    $desc=$_POST['desc']; 
    $addtime=time(); 
    $poll=$_POST['poll']; 
    $sql="update poll SET title='{$title}',des='{$desc}',addtime='{$addtime}' where id='{$id}'"; 
    $rs=$db->query($sql); 
    foreach ($poll as $k=> $v){ 
//      echo "ID:是".$k."值是:".$v; 
//      echo "
"; 
        $sql2="update poll_option SET opdata='{$v}' where id='{$k}' and pollid={$id} "; 
        $rs2=$db->query($sql2); 
    } 
    if($rs&&$rs2){ 
        echo "<script>location.href='poll.php?action=polllist'</script>"; 
    }else{ 
        echo "<script>alert('更新失败!');history.go(-1)</script>"; 
    } 

 
 
 
 
//==============投票相关函数================= 
//获取投票信息 
function  getpoll(){ 
    global $db; 
    $sql="select * from poll order by id desc"; 
    $rs=$db->query($sql); 
    $data=array(); 
    while ($row=$db->fetch_array($rs)){ 
        $data[]=$row; 
    } 
    return $data; 
}
 
?> 

摘自 chaojie2009的专栏

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/478383.htmlTechArticle?php /*** Poll * * **/ include isLogin.php; include ../conn/config.inc.php; /*action操作初始化*/ if(emptyempty($_REQUEST[action])){ $_REQUEST[action]=list; }else { $_...
Statement:
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