【项目】问答系统-代码-前台-首页-个人主页
<?phpclass MemberControl extends CommonControl{public function __construct(){$this->top_cate();$this->_left_info();}////默认会员中心public function index(){$uid = $this->_GET('uid', 'intval');$w
·
<?php
class MemberControl extends CommonControl{
public function __construct(){
$this->top_cate();
$this->_left_info();
}
//
//默认会员中心
public function index(){
$uid = $this->_GET('uid', 'intval');
$where = array('uid'=>$uid);
//我的提问
$field = 'content,title,answer,time,asid,ask.cid';
$ask = K('ask')->join('category')->where($where)->field($field)->order('time DESC')->limit(5)->select();
$this->assign('ask', $ask);
//我的回答
$field = 'answer.content,title,ask.answer,answer.time,ask.asid,ask.cid';
$answer = K('answerInfo')->field($field)->where($where)->order('time DESC')->limit(5)->select();
// p($answer);
$this->assign('answer', $answer);
$this->display();
}
//我的提问
public function my_ask(){
$uid = $this->_GET('uid', 'intval');
$field = 'content,title,answer,time,asid,ask.cid';
$where = array(
'uid' => $uid,
'solve' => 0
);
if($this->_GET('w', 'intval') == 1){
$where['solve'] = 1;
}//来判断是否 被解决
$db = M('ask');
$kdb = K('ask')->join('category');
$noSolvePage = new page($db->where($where)->count(), 5, 5, 2);
$noSolve = $kdb->field($field)->where($where)->order('time DESC')->select($noSolvePage->limit());
$this->assign("noSolve", $noSolve);
$this->assign("noSolvePage", $noSolvePage->show());
$this->display();
}
/**
* 我的回答
*/
public function my_answer(){
$uid = $this->_GET('uid', 'intval');
$db = M("answer");
$where = array('uid'=>$uid);
$field = 'answer.content,title,ask.answer,answer.time,ask.asid,ask.cid';
if($this->_GET('w', 'intval') == 1){
$where['accept'] = 1;
}
$page = new page($db->where($where)->count(), 5, 5, 2);
$answer = K('answerInfo')->where($where)->field($field)->order('time DESC')->select($page->limit());
$this->assign('answer', $answer);
$this->assign('page', $page->show());
$this->display();
}
/**
* 我的等级
*/
public function my_level(){
$uid = $this->_GET('uid', 'intval');
$user = M('user')->where(array('uid'=>$uid))->field('exp')->find();
$level = $this->exp_to_level($user);
$nextExp = C('LV'. ($level+1)) - $user['exp'];
$nextExp = ($nextExp<0) ? 0 : $nextExp;
$this->assign('level', $level);
$this->assign('nextExp', $nextExp);
$levelExp = array();
for ($i=0; $i < 21; $i++) {
$levelExp[$i] = C('LV'. $i);
}
$this->assign('exp', $user['exp']);
$this->assign('levelExp', $levelExp);
$this->display();
}
/**
* 我的金币
*/
public function my_point(){
$this->display();
}
/**
* 我的头像
*/
public function my_face(){
$uid = $this->_GET('uid', 'intval');
$db = M('user');
$where = array('uid'=>$uid);
if(IS_POST){
//$this->error('本演示已关闭上传头像功能');
$upload = new upload();
$uploadInfo = $upload->upload();
$oldFace = $db->where($where)->getField('face');//旧的图片地址
$oldFace = './' . substr($oldFace, strpos($oldFace, 'upload'));
if(is_file($oldFace)){
if(!unlink($oldFace))
$this->error('没有权限!');
}
$face = './'. '/' . $uploadInfo[0]['path'];
$db->where($where)->save(array('face'=>$face));
$this->success('上传成功!');
}
$user = $db->where($where)->field('face')->find();
$this->assign('face', $this->face($user));
$this->display();
}
//左侧用户信息
private function _left_info(){
$uid = $this->_GET('uid', 'intval');
$field = 'face,username,point,exp,ask,answer,accept';
$member = M('user')->where(array('uid'=>$uid))->field($field)->find();
if(!empty($member)){
$member['face'] = $this->face($member);
$member['ratio'] = $this->ratio($member);
$member['lv'] = $this->exp_to_level($member);
$this->assign('member', $member);
} else {
$this->error('用户不存在!');
}
//第三人称
$rank = isset($_SESSION['uid']) && $uid == $_SESSION['uid'] ? '我' : 'TA';
$this->assign('rank', $rank);
}
}
<?php
//公共的 控制器
class CommonControl extends Control{
public function assign_data(){
$this->right_info();
$this->eve_star();
$this->his_star();
$this->helper();
$this->top_cate();
}
//右侧用户的登录信息
//
public function right_info(){
$uid= $this->_SESSION('uid','intval');
if($uid){
$field='face,exp,point,answer,ask,accept';
$userInfo=M('user')->where(array('uid'=>$uid))->field($field)->find();
$userInfo['face']=$this->face($userInfo);
$userInfo['lv']=$this->exp_to_level($userInfo);
$userInfo['ratio']=$this->ratio($userInfo);
$this->assign('userInfo',$userInfo);
}
}
//本日回答最多的人
public function eve_star(){
$zero=strtotime(date('Y-m-d'));
$field='face,username,user.uid,exp,answer,user.accept';
$eveStar=K('answer')->where(array('time'=>array('gt'=>$zero)))->field($field)->group('username')->order('COUNT(username) DESC')->find();
if(!empty($eveStar)){
$eveStar['face']=$this->face($eveStar);
$eveStar['lv']=$this->exp_to_level($eveStar);
$eveStar['ratio']=$this->ratio($eveStar);
}
$this->assign('eveStar',$eveStar);
}
//历史回答最多的人
//
public function his_star(){
$field='face,username,user.uid,exp,answer,user.accept';
$hisStar=K('answer')->field($field)->order('answer DESC')->find();
if(!empty($hisStar)){
$hisStar['face']=$this->face($hisStar);
$hisStar['lv']=$this->exp_to_level($hisStar);
$hisStar['ratio']=$this->ratio($hisStar);
}
$this->assign('hisStar',$hisStar);
}
//助人光荣榜
public function helper(){
$field='uid,username,accept';
$helper=M('user')->field($field)->order('accept DESC')->limit(10)->select();
$this->assign('helper',$helper);
}
public function top_cate(){
//顶级分类
$topCate=M('category')->where(array('pid'=>0))->select();
$this->assign('topCate',$topCate);
//累积提问
$askNum=M('ask')->count();
$this->assign('askNum',$askNum);
}
//获得父级分类
public function father_cate($arr,$pid){
$array=array();
foreach ($arr as $v) {
if($v['cid']==$pid){
$array[]=$v;
$array=array_merge($array,$this->father_cate($arr,$v['pid']));
}
}
return $array;
}
//经验转换等级
public function exp_to_level($user){
$exp =$user['exp'];
for($i=0;$i<21;$i++){
if($exp<=C('LV'.$i)){
return $i;
}
}
if($exp>C('LV20')){
return 20;
}
}
public function face($user){
if(!empty($user['face'])){
return $user['face'];
}
return "./Index/Tpl/Public/Images/noface.gif";
}
/**
* 采纳率换算
*/
public function ratio($user){
if(!empty($user) && $user['answer']){
$num = $user['accept'] / $user['answer'];
$ratio = (sprintf("%.2f", $num)) * 100;
} else {
$ratio = 0;
}
return $ratio;
}
}
个人主页

我的首页

我的提问

我的回答

我的等级

我的金币

上传头像

更多推荐
所有评论(0)