<?php
/**
* 投票調查
*
*
* */
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.="<div id=\"{$row2['id']}\"><input type=\"text\" class=\"text-box\" name=\"poll[{$row2['id']}]\" maxlength=\"60\" size=\"30\" value=\"{$row2['opdata']}\">";
if($i>2){
$potion.="<input type=\"button\" value=\"刪除\" onClick=\"del({$row2['id']})\">";
}
$potion.="</div>";
}
$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 "<br>";
$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的專欄