VoteController.php 2.5 KB
<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Db;

class VoteController extends AdminBaseController{

  //投票列表
  public function voteList(){

    $data = Db::name('vote') -> where('pid',0) -> select();
    $this -> assign('data',$data);
    return $this -> fetch();

  }

  //添加投票
  public function voteadd(){

    $title = input('post.title');
    $options = input('post.xuan');
    $radio = input('post.radio');

    $option = explode(',', $options);
    $arr['title'] = $title;
    $arr['pid'] = 0;
    $arr['radio'] = $radio;
    $arr['time'] = date('y-m-d H:i:s');
    $arr['image'] = input('post.img');
    $id = Db::name('vote') -> insertGetId($arr);
    foreach ($option as $key => $value) {
      $data['pid'] = $id;
      $data['options'] = $value;
      $data['votes'] = 0;
      Db::name('vote') -> insert($data);
    }
    return $id;

  }

  //当点击编辑时
  public function votebian(){

    $id = input('post.id');
    $data['title'] = Db::name('vote') -> where('id',$id) -> find();
    $shuju = Db::name('vote') -> where('pid',$id) -> select();
    $data['option'] = $shuju;
    return json_encode($data);

  }

  //更新数据
  public function voteupdate(){

    $id = input('post.id');
    $title = input('post.title');
    $radio = input('post.radio');
    $arr['id'] = $id;
    $arr['title'] = $title;
    $arr['radio'] = $radio;
    $arr['image'] =input('post.image');
    $updata = Db::name('vote') -> update($arr);

    $xid = input('post.xid');
    $xshu = input('post.xshu');
    $xid = explode(',',$xid);
    $xshu = explode(',',$xshu);
    foreach ($xid as $key => $value) {
      Db::name('vote') -> where('id',$value) ->update(['options'=>$xshu[$key]]);
    }

    return true;

  }

  //删除数据
  public function votedel(){

    $id = input('post.id');
    Db::name('vote') -> delete($id);
    Db::name('vote') -> where('pid',$id) ->delete();
    Db::name('mevo') -> where('vid',$id) -> delete();
    if($id){
      return true;
    }else{
      return false;
    }

  }


  //投票结果页
  public function index(){

    $data = Db::name('vote') -> where('pid',0) -> select();
    $this -> assign('data',$data);
    return $this -> fetch();

  }

  //投票查看
  public function results(){

    $id = input('post.id');
    $arr['title'] = Db::name('vote') -> where('id',$id) -> find();
    $arr['xuan'] = Db::name('vote') -> where('pid',$id) -> select();
    return json_encode($arr);

  }



}




 ?>